From 4e4bffc3d11aceac200026cd8228b20ff91f0426 Mon Sep 17 00:00:00 2001 From: Armando Migliaccio Date: Wed, 30 Nov 2016 20:24:35 -0800 Subject: [PATCH] Sync up bare minimum from neutron.db.api into lib This is enough to make the lib's version of the context to work seamlessly. Change-Id: Ibb1e16e50b6372f4271e0925f36c0ee87b86c2cf --- neutron_lib/db/_api.py | 36 +++++++----------------------- neutron_lib/tests/unit/db/_base.py | 2 +- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/neutron_lib/db/_api.py b/neutron_lib/db/_api.py index e681b37ba..ee1d56c1d 100644 --- a/neutron_lib/db/_api.py +++ b/neutron_lib/db/_api.py @@ -10,37 +10,17 @@ # License for the specific language governing permissions and limitations # under the License. -""" -TEMPORARY: use the old EngineFacade and lazy init. - -TODO(HenryG): replace this file with the new db/api.py from neutron. -""" - -from oslo_config import cfg -from oslo_db.sqlalchemy import session - -_FACADE = None +from oslo_db.sqlalchemy import enginefacade -def _create_facade_lazily(): - global _FACADE - - # NOTE: This is going to change with bug 1520719 - if _FACADE is None: - _FACADE = session.EngineFacade.from_config(cfg.CONF, sqlite_fk=True) - - return _FACADE - - -def get_engine(): - """Helper method to grab engine.""" - facade = _create_facade_lazily() - return facade.get_engine() +context_manager = enginefacade.transaction_context() +context_manager.configure(sqlite_fk=True) +# TODO(akamyshnikova): when all places in the code, which use sessions/ +# connections will be updated, this won't be needed def get_session(autocommit=True, expire_on_commit=False, use_slave=False): """Helper method to grab session.""" - facade = _create_facade_lazily() - return facade.get_session(autocommit=autocommit, - expire_on_commit=expire_on_commit, - use_slave=use_slave) + return context_manager.get_legacy_facade().get_session( + autocommit=autocommit, expire_on_commit=expire_on_commit, + use_slave=use_slave) diff --git a/neutron_lib/tests/unit/db/_base.py b/neutron_lib/tests/unit/db/_base.py index 77ba2c329..92c041a85 100644 --- a/neutron_lib/tests/unit/db/_base.py +++ b/neutron_lib/tests/unit/db/_base.py @@ -30,7 +30,7 @@ class SqlFixture(fixtures.Fixture): def _setUp(self): # Register all data models - engine = db_api.get_engine() + engine = db_api.context_manager.get_legacy_facade().get_engine() if not SqlFixture._TABLES_ESTABLISHED: model_base.BASEV2.metadata.create_all(engine) SqlFixture._TABLES_ESTABLISHED = True