Remove support for multiple database from migration.py

This is part of a series of patches to remove noise from the
db migration in placement so that if/when we switch to alembic
there isn't confusing stuff in the way.

In this change the code for database migrations is adjusted to
no longer support multiple databases. While that support was
needed in nova, it is not in placement.

Change-Id: Idcf5f9919fda96b3cd88775ca68a13f20b6eefd2
This commit is contained in:
Chris Dent 2018-10-02 14:32:40 +01:00
parent 427feb7f92
commit 69490714ec
3 changed files with 30 additions and 46 deletions

View File

@ -1,7 +1,7 @@
[db_settings]
# Used to identify which repository this database is versioned under.
# You can use the name of your project.
repository_id=nova_api
repository_id=placement
# The name of the database table used to track the schema version.
# This name shouldn't already be used by your project.

View File

@ -23,86 +23,70 @@ from oslo_log import log as logging
import sqlalchemy
from placement import db_api as placement_db
from placement import exception
from placement.i18n import _
INIT_VERSION = {}
INIT_VERSION['placement'] = 0
_REPOSITORY = {}
INIT_VERSION = 0
_REPOSITORY = None
LOG = logging.getLogger(__name__)
def get_engine(database='placement', context=None):
if database == 'placement':
return placement_db.get_placement_engine()
def get_engine(context=None):
return placement_db.get_placement_engine()
def db_sync(version=None, database='placement', context=None):
def db_sync(version=None, context=None):
if version is not None:
try:
version = int(version)
except ValueError:
raise exception.NovaException(_("version should be an integer"))
# Let ValueError raise
version = int(version)
current_version = db_version(database, context=context)
repository = _find_migrate_repo(database)
current_version = db_version(context=context)
repository = _find_migrate_repo()
if version is None or version > current_version:
return versioning_api.upgrade(get_engine(database, context=context),
return versioning_api.upgrade(get_engine(context=context),
repository, version)
else:
return versioning_api.downgrade(get_engine(database, context=context),
return versioning_api.downgrade(get_engine(context=context),
repository, version)
def db_version(database='placement', context=None):
repository = _find_migrate_repo(database)
def db_version(context=None):
repository = _find_migrate_repo()
try:
return versioning_api.db_version(get_engine(database, context=context),
return versioning_api.db_version(get_engine(context=context),
repository)
except versioning_exceptions.DatabaseNotControlledError as exc:
meta = sqlalchemy.MetaData()
engine = get_engine(database, context=context)
engine = get_engine(context=context)
meta.reflect(bind=engine)
tables = meta.tables
if len(tables) == 0:
db_version_control(INIT_VERSION[database],
database,
context=context)
db_version_control(INIT_VERSION, context=context)
return versioning_api.db_version(
get_engine(database, context=context), repository)
get_engine(context=context), repository)
else:
LOG.exception(exc)
# Some pre-Essex DB's may not be version controlled.
# Require them to upgrade using Essex first.
raise exception.NovaException(
_("Upgrade DB using Essex release first."))
raise exc
def db_initial_version(database='placement'):
return INIT_VERSION[database]
def db_initial_version():
return INIT_VERSION
def db_version_control(version=None, database='placement', context=None):
repository = _find_migrate_repo(database)
versioning_api.version_control(get_engine(database, context=context),
def db_version_control(version=None, context=None):
repository = _find_migrate_repo()
versioning_api.version_control(get_engine(context=context),
repository,
version)
return version
def _find_migrate_repo(database='placement'):
def _find_migrate_repo():
"""Get the path for the migrate repository."""
global _REPOSITORY
rel_path = 'migrate_repo'
if database == 'api' or database == 'placement':
# NOTE(cdent): For the time being the placement database (if
# it is being used) is a replica (in structure) of the api
# database.
rel_path = os.path.join('api_migrations', 'migrate_repo')
rel_path = os.path.join('api_migrations', 'migrate_repo')
path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
rel_path)
assert os.path.exists(path)
if _REPOSITORY.get(database) is None:
_REPOSITORY[database] = Repository(path)
return _REPOSITORY[database]
if _REPOSITORY is None:
_REPOSITORY = Repository(path)
return _REPOSITORY

View File

@ -47,7 +47,7 @@ class Database(fixtures.Fixture):
if not db_schema:
engine = self.get_engine()
conn = engine.connect()
migration.db_sync(database="placement")
migration.db_sync()
db_schema = "".join(line for line in conn.connection.iterdump())
engine.dispose()