Merge "Replace deprecated LegacyEngineFacade"

This commit is contained in:
Zuul 2024-10-14 13:41:10 +00:00 committed by Gerrit Code Review
commit e28298000b
8 changed files with 43 additions and 39 deletions

View File

@ -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

View File

@ -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,

View File

@ -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)

View File

@ -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)

View File

@ -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
)

View File

@ -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):

View File

@ -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

View File

@ -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