Share a single connection with all systems during migrations
The purpose of the Alembic env.py file is so that an application may define the optimal approach to database connectivity for an application as it runs migrations. Here, we ensure that the connectivity comes from the same system as that of the api.py module itself, e.g. enginefacade, and we also ensure that the same transaction/connection used for migrations is shared with the ORM session, so that ORM operations inline with schema operations share the same transactional context. Change-Id: Ie2d28d2b04e452c13d16e561b781f82cf2b75e18 Story: #2000099
This commit is contained in:
committed by
Matthew Treinish
parent
b8671a116c
commit
53fe95bb24
@@ -16,7 +16,8 @@
|
||||
from __future__ import with_statement
|
||||
from alembic import context
|
||||
from logging.config import fileConfig # noqa
|
||||
from oslo.db.sqlalchemy import session
|
||||
|
||||
from subunit2sql.db import api as db_api
|
||||
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
@@ -73,8 +74,12 @@ def run_migrations_online():
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
engine = session.create_engine(subunit2sql_config.database.connection)
|
||||
|
||||
facade = db_api._create_facade_lazily()
|
||||
engine = facade.get_engine()
|
||||
connection = engine.connect()
|
||||
facade._session_maker.configure(bind=connection)
|
||||
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
try:
|
||||
@@ -82,6 +87,7 @@ def run_migrations_online():
|
||||
context.run_migrations()
|
||||
finally:
|
||||
connection.close()
|
||||
facade._session_maker.configure(bind=engine)
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
|
||||
Reference in New Issue
Block a user