Switch to using enginefacade

Closes-Bug: #2067345
Change-Id: If9a2c96628cfcb819fee5e19f872ea015979b30f
This commit is contained in:
Mohammed Naser 2024-08-28 19:16:07 -04:00 committed by scrungus
parent 8d09169a8a
commit 0ce2c41404
6 changed files with 437 additions and 377 deletions

View File

@ -12,6 +12,7 @@
from eventlet.green import threading from eventlet.green import threading
from oslo_context import context from oslo_context import context
from oslo_db.sqlalchemy import enginefacade
from magnum.common import policy from magnum.common import policy
@ -20,6 +21,7 @@ import magnum.conf
CONF = magnum.conf.CONF CONF = magnum.conf.CONF
@enginefacade.transaction_context_provider
class RequestContext(context.RequestContext): class RequestContext(context.RequestContext):
"""Extends security contexts from the OpenStack common library.""" """Extends security contexts from the OpenStack common library."""

View File

@ -13,8 +13,8 @@
from logging import config as log_config from logging import config as log_config
from alembic import context from alembic import context
from oslo_db.sqlalchemy import enginefacade
from magnum.db.sqlalchemy import api as sqla_api
from magnum.db.sqlalchemy import models from magnum.db.sqlalchemy import models
# this is the Alembic Config object, which provides # this is the Alembic Config object, which provides
@ -43,7 +43,7 @@ def run_migrations_online():
and associate a connection with the context. and associate a connection with the context.
""" """
engine = sqla_api.get_engine() engine = enginefacade.writer.get_engine()
with engine.connect() as connection: with engine.connect() as connection:
context.configure(connection=connection, context.configure(connection=connection,
target_metadata=target_metadata) target_metadata=target_metadata)

File diff suppressed because it is too large Load Diff

View File

@ -87,15 +87,6 @@ class MagnumBase(models.TimestampMixin,
d[c.name] = self[c.name] d[c.name] = self[c.name]
return d return d
def save(self, session=None):
import magnum.db.sqlalchemy.api as db_api
if session is None:
session = db_api.get_session()
with session.begin():
super(MagnumBase, self).save(session)
Base = declarative_base(cls=MagnumBase) Base = declarative_base(cls=MagnumBase)

View File

@ -16,10 +16,10 @@
"""Magnum DB test base class.""" """Magnum DB test base class."""
import fixtures import fixtures
from oslo_db.sqlalchemy import enginefacade
import magnum.conf import magnum.conf
from magnum.db import api as dbapi from magnum.db import api as dbapi
from magnum.db.sqlalchemy import api as sqla_api
from magnum.db.sqlalchemy import migration from magnum.db.sqlalchemy import migration
from magnum.db.sqlalchemy import models from magnum.db.sqlalchemy import models
from magnum.tests import base from magnum.tests import base
@ -32,16 +32,15 @@ _DB_CACHE = None
class Database(fixtures.Fixture): class Database(fixtures.Fixture):
def __init__(self, db_api, db_migrate, sql_connection): def __init__(self, engine, db_migrate, sql_connection):
self.sql_connection = sql_connection self.sql_connection = sql_connection
self.engine = db_api.get_engine() self.engine = engine
self.engine.dispose() self.engine.dispose()
conn = self.engine.connect() with self.engine.connect() as conn:
self.setup_sqlite(db_migrate) self.setup_sqlite(db_migrate)
self.post_migrations() self.post_migrations()
self._DB = "".join(line for line in conn.connection.iterdump())
self._DB = "".join(line for line in conn.connection.iterdump())
self.engine.dispose() self.engine.dispose()
def setup_sqlite(self, db_migrate): def setup_sqlite(self, db_migrate):
@ -50,9 +49,10 @@ class Database(fixtures.Fixture):
models.Base.metadata.create_all(self.engine) models.Base.metadata.create_all(self.engine)
db_migrate.stamp('head') db_migrate.stamp('head')
def _setUp(self): def setUp(self):
conn = self.engine.connect() super(Database, self).setUp()
conn.connection.executescript(self._DB) with self.engine.connect() as conn:
conn.connection.executescript(self._DB)
self.addCleanup(self.engine.dispose) self.addCleanup(self.engine.dispose)
def post_migrations(self): def post_migrations(self):
@ -68,6 +68,8 @@ class DbTestCase(base.TestCase):
global _DB_CACHE global _DB_CACHE
if not _DB_CACHE: if not _DB_CACHE:
_DB_CACHE = Database(sqla_api, migration, engine = enginefacade.writer.get_engine()
_DB_CACHE = Database(engine, migration,
sql_connection=CONF.database.connection) sql_connection=CONF.database.connection)
engine.dispose()
self.useFixture(_DB_CACHE) self.useFixture(_DB_CACHE)

View File

@ -26,16 +26,22 @@ class SqlAlchemyCustomTypesTestCase(base.DbTestCase):
# Create ClusterTemplate w/o labels # Create ClusterTemplate w/o labels
cluster_template1_id = uuidutils.generate_uuid() cluster_template1_id = uuidutils.generate_uuid()
self.dbapi.create_cluster_template({'uuid': cluster_template1_id}) self.dbapi.create_cluster_template({'uuid': cluster_template1_id})
cluster_template1 = sa_api.model_query( with sa_api._session_for_read() as session:
models.ClusterTemplate).filter_by(uuid=cluster_template1_id).one() cluster_template1 = (session.query(
models.ClusterTemplate)
.filter_by(uuid=cluster_template1_id)
.one())
self.assertEqual({}, cluster_template1.labels) self.assertEqual({}, cluster_template1.labels)
# Create ClusterTemplate with labels # Create ClusterTemplate with labels
cluster_template2_id = uuidutils.generate_uuid() cluster_template2_id = uuidutils.generate_uuid()
self.dbapi.create_cluster_template( self.dbapi.create_cluster_template(
{'uuid': cluster_template2_id, 'labels': {'bar': 'foo'}}) {'uuid': cluster_template2_id, 'labels': {'bar': 'foo'}})
cluster_template2 = sa_api.model_query( with sa_api._session_for_read() as session:
models.ClusterTemplate).filter_by(uuid=cluster_template2_id).one() cluster_template2 = (session.query(
models.ClusterTemplate)
.filter_by(uuid=cluster_template2_id)
.one())
self.assertEqual('foo', cluster_template2.labels['bar']) self.assertEqual('foo', cluster_template2.labels['bar'])
def test_JSONEncodedDict_type_check(self): def test_JSONEncodedDict_type_check(self):
@ -48,8 +54,9 @@ class SqlAlchemyCustomTypesTestCase(base.DbTestCase):
# Create nodegroup w/o node_addresses # Create nodegroup w/o node_addresses
nodegroup1_id = uuidutils.generate_uuid() nodegroup1_id = uuidutils.generate_uuid()
self.dbapi.create_nodegroup({'uuid': nodegroup1_id}) self.dbapi.create_nodegroup({'uuid': nodegroup1_id})
nodegroup1 = sa_api.model_query( with sa_api._session_for_read() as session:
models.NodeGroup).filter_by(uuid=nodegroup1_id).one() nodegroup1 = session.query(
models.NodeGroup).filter_by(uuid=nodegroup1_id).one()
self.assertEqual([], nodegroup1.node_addresses) self.assertEqual([], nodegroup1.node_addresses)
# Create nodegroup with node_addresses # Create nodegroup with node_addresses
@ -59,8 +66,9 @@ class SqlAlchemyCustomTypesTestCase(base.DbTestCase):
'node_addresses': ['mynode_address1', 'node_addresses': ['mynode_address1',
'mynode_address2'] 'mynode_address2']
}) })
nodegroup2 = sa_api.model_query( with sa_api._session_for_read() as session:
models.NodeGroup).filter_by(uuid=nodegroup2_id).one() nodegroup2 = session.query(
models.NodeGroup).filter_by(uuid=nodegroup2_id).one()
self.assertEqual(['mynode_address1', 'mynode_address2'], self.assertEqual(['mynode_address1', 'mynode_address2'],
nodegroup2.node_addresses) nodegroup2.node_addresses)