From 823625406465d55ba609c1461b857be0227b63b2 Mon Sep 17 00:00:00 2001 From: Tobias Urdin Date: Tue, 27 Aug 2024 17:04:16 +0200 Subject: [PATCH] Replace deprecated LegacyEngineFacade LegacyEngineFacade was deprecated in oslo.db 1.12.0 which was released in 2015. Change-Id: Icf08ac7cc7144e9bdd7d8bd6e124d84cab172e07 --- trove/cmd/common.py | 2 +- trove/cmd/manage.py | 2 +- trove/cmd/status.py | 2 +- trove/configuration/models.py | 6 ++--- trove/datastore/models.py | 6 ++--- trove/db/sqlalchemy/api.py | 36 ++++++++++++++++-------------- trove/db/sqlalchemy/session.py | 26 +++++++++++---------- trove/tests/unittests/util/util.py | 2 +- 8 files changed, 43 insertions(+), 39 deletions(-) diff --git a/trove/cmd/common.py b/trove/cmd/common.py index 1267ed9fe4..0eb0cbd9d9 100644 --- a/trove/cmd/common.py +++ b/trove/cmd/common.py @@ -43,7 +43,7 @@ def initialize(extra_opts=None, pre_logging=None): # Initialize Trove database. from trove.db import get_db_api - get_db_api().configure_db(conf) + get_db_api().configure_db() return conf # May be used by other scripts diff --git a/trove/cmd/manage.py b/trove/cmd/manage.py index 2ea9705295..fb560649d2 100644 --- a/trove/cmd/manage.py +++ b/trove/cmd/manage.py @@ -82,7 +82,7 @@ class Commands(object): def db_recreate(self, repo_path): """Drops the database and recreates it.""" - self.db_api.drop_db(CONF) + self.db_api.drop_db() self.db_sync(repo_path) def db_load_datastore_config_parameters(self, diff --git a/trove/cmd/status.py b/trove/cmd/status.py index 76045d6ea4..16abec359d 100644 --- a/trove/cmd/status.py +++ b/trove/cmd/status.py @@ -39,7 +39,7 @@ class Checks(upgradecheck.UpgradeCommands): """ db_api = db.get_db_api() - db_api.configure_db(cfg.CONF) + db_api.configure_db() with DBInstance.query() as query: query = query.filter(DBInstance.task_status != InstanceTasks.NONE) diff --git a/trove/configuration/models.py b/trove/configuration/models.py index 983e512bb8..65810be244 100644 --- a/trove/configuration/models.py +++ b/trove/configuration/models.py @@ -336,7 +336,7 @@ def create_or_update_datastore_configuration_parameter(name, data_type, max_size, min_size): - get_db_api().configure_db(CONF) + get_db_api().configure_db() datastore_version = dstore_models.DatastoreVersion.load_by_uuid( datastore_version_id) try: @@ -362,7 +362,7 @@ def create_or_update_datastore_configuration_parameter(name, def load_datastore_configuration_parameters(datastore, datastore_version, config_file, version_number=None): - get_db_api().configure_db(CONF) + get_db_api().configure_db() (ds, ds_v) = dstore_models.get_datastore_version( type=datastore, version=datastore_version, return_inactive=True, version_number=version_number) @@ -381,7 +381,7 @@ def load_datastore_configuration_parameters(datastore, datastore_version, def remove_datastore_configuration_parameters(datastore, datastore_version, version_number=None): - get_db_api().configure_db(CONF) + get_db_api().configure_db() (ds, ds_version) = dstore_models.get_datastore_version( type=datastore, version=datastore_version, return_inactive=True, version_number=version_number) diff --git a/trove/datastore/models.py b/trove/datastore/models.py index 1a0916fb8f..65c7dab20d 100644 --- a/trove/datastore/models.py +++ b/trove/datastore/models.py @@ -589,7 +589,7 @@ def get_datastore_or_version(datastore=None, datastore_version=None): def update_datastore(name, default_version): - db_api.configure_db(CONF) + db_api.configure_db() try: datastore = DBDatastore.find_by(name=name) except exception.ModelNotFoundError: @@ -614,7 +614,7 @@ def update_datastore_version(datastore, name, manager, image_id, image_tags, repl_strategy=None, version=None, new_name=None): """Create or update datastore version.""" version = version or name - db_api.configure_db(CONF) + db_api.configure_db() datastore = Datastore.load(datastore) try: ds_version = DBDatastoreVersion.find_by(datastore_id=datastore.id, @@ -655,7 +655,7 @@ class DatastoreVersionMetadata(object): Helper to find a datastore version id for a given datastore and datastore version name. """ - db_api.configure_db(CONF) + db_api.configure_db() db_ds_record = DBDatastore.find_by( name=datastore_name ) diff --git a/trove/db/sqlalchemy/api.py b/trove/db/sqlalchemy/api.py index 0a28da4cc7..c21cd67ea8 100644 --- a/trove/db/sqlalchemy/api.py +++ b/trove/db/sqlalchemy/api.py @@ -124,18 +124,18 @@ def update_all(query_func, model, conditions, values): query.session.commit() -def configure_db(options, *plugins): - session.configure_db(options) - configure_db_for_plugins(options, *plugins) +def configure_db(*plugins): + session.configure_db() + configure_db_for_plugins(*plugins) -def configure_db_for_plugins(options, *plugins): +def configure_db_for_plugins(*plugins): for plugin in plugins: - session.configure_db(options, models_mapper=plugin.mapper) + session.configure_db(models_mapper=plugin.mapper) -def drop_db(options): - session.drop_db(options) +def drop_db(): + session.drop_db() def clean_db(): @@ -196,14 +196,16 @@ def _migrate_legacy_database(config): raise exception.BadRequest(message) -def _configure_alembic(options): +def _configure_alembic(conf=None): + if conf is None: + conf = conf.CONF alembic_ini = Path(__file__).joinpath('..', 'alembic.ini').resolve() if alembic_ini.exists(): # alembic configuration config = alembic_config.Config(alembic_ini) # override the database configuration from the file config.set_main_option('sqlalchemy.url', - options['database']['connection']) + conf['database']['connection']) # override the logger configuration from the file # https://stackoverflow.com/a/42691781/613428 config.attributes['configure_logger'] = False @@ -213,8 +215,8 @@ def _configure_alembic(options): return None -def db_sync(options, version=None, repo_path=None): - config = _configure_alembic(options) +def db_sync(conf=None, version=None, repo_path=None): + config = _configure_alembic(conf=conf) if config: # Check the version if version is None: @@ -233,8 +235,8 @@ def db_sync(options, version=None, repo_path=None): 'no longer supported') -def db_upgrade(options, version=None, repo_path=None): - config = _configure_alembic(options) +def db_upgrade(conf=None, version=None, repo_path=None): + config = _configure_alembic(conf=conf) if config: # Check the version if version is None: @@ -253,10 +255,10 @@ def db_upgrade(options, version=None, repo_path=None): 'no longer supported') -def db_reset(options, *plugins): - drop_db(options) - db_sync(options) - configure_db(options) +def db_reset(*plugins): + drop_db() + db_sync() + configure_db() def _base_query(cls): diff --git a/trove/db/sqlalchemy/session.py b/trove/db/sqlalchemy/session.py index 3a833705ef..e9fff0545f 100644 --- a/trove/db/sqlalchemy/session.py +++ b/trove/db/sqlalchemy/session.py @@ -15,7 +15,7 @@ import contextlib -from oslo_db.sqlalchemy import session as db_session +from oslo_db.sqlalchemy import enginefacade from oslo_log import log as logging from sqlalchemy import MetaData @@ -30,8 +30,8 @@ LOG = logging.getLogger(__name__) CONF = cfg.CONF -def configure_db(options, models_mapper=None): - facade = _create_facade(options) +def configure_db(models_mapper=None): + facade = _create_facade() if models_mapper: models_mapper.map(facade) else: @@ -69,10 +69,11 @@ def configure_db(options, models_mapper=None): mappers.map(get_engine(), models) -def _create_facade(options): +def _create_facade(): global _FACADE if _FACADE is None: - _FACADE = db_session.EngineFacade.from_config(options) + ctx = enginefacade.transaction_context() + _FACADE = ctx.writer return _FACADE @@ -88,13 +89,15 @@ def get_facade(): return _FACADE -def get_engine(use_slave=False): - _create_facade(CONF) - return _FACADE.get_engine(use_slave=use_slave) +def get_engine(): + _create_facade() + return _FACADE.get_engine() def get_session(**kwargs): - return get_facade().get_session(**kwargs) + facade = _create_facade() + sessionmaker = facade.get_sessionmaker() + return sessionmaker(**kwargs) def raw_query(model, **kwargs): @@ -114,9 +117,8 @@ def clean_db(): trans.commit() -def drop_db(options): - if options: - _create_facade(options) +def drop_db(): + _create_facade() engine = get_engine() meta = MetaData() meta.bind = engine diff --git a/trove/tests/unittests/util/util.py b/trove/tests/unittests/util/util.py index 19af403c1f..407fd45da5 100644 --- a/trove/tests/unittests/util/util.py +++ b/trove/tests/unittests/util/util.py @@ -32,7 +32,7 @@ def init_db(): "database") db_api = get_db_api() db_api.db_sync(CONF) - session.configure_db(CONF) + session.configure_db() DB_SETUP = True