Use a writer context for the online alembic migrations

``DBConnection`` class does not retrieve the URL string
correctly and cannot connect to the database. This patch
replaces it with default writer context that any database
operation transaction uses in Neutron.

Closes-Bug: #2015847

Change-Id: I60523690bc170ec4ba81312ada0f2d83542cae67
This commit is contained in:
Rodolfo Alonso Hernandez 2023-04-20 02:13:05 +02:00 committed by Rodolfo Alonso
parent 3002072511
commit 82029c2c51
1 changed files with 14 additions and 4 deletions

View File

@ -13,14 +13,16 @@
# under the License.
from alembic import context
from neutron_lib import context as n_context
from neutron_lib.db import api as db_api
from neutron_lib.db import model_base
from oslo_config import cfg
from oslo_db import options as db_options
import sqlalchemy as sa
from sqlalchemy import event # noqa
from neutron.db.migration.alembic_migrations import external
from neutron.db.migration import autogen
from neutron.db.migration.connection import DBConnection
from neutron.db.migration.models import head # noqa
try:
@ -53,6 +55,13 @@ def set_mysql_engine():
model_base.BASEV2.__table_args__['mysql_engine'])
def setup_conf():
conf = cfg.CONF
conf.register_opts(db_options.database_opts, 'database')
conf.set_override('connection', neutron_config.database.connection,
group='database')
def include_object(object_, name, type_, reflected, compare_to):
if type_ == 'table' and name in external.TABLES:
return False
@ -101,11 +110,12 @@ def run_migrations_online():
and associate a connection with the context.
"""
setup_conf()
set_mysql_engine()
connection = config.attributes.get('connection')
with DBConnection(neutron_config.database.connection, connection) as conn:
admin_ctx = n_context.get_admin_context()
with db_api.CONTEXT_WRITER.using(admin_ctx) as session:
context.configure(
connection=conn,
connection=session.connection(),
target_metadata=target_metadata,
include_object=include_object,
process_revision_directives=autogen.process_revision_directives