| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
from logging import config as logging_config |
| 16 |
|
| 17 |
from alembic import context |
| 18 |
from neutron_lib.db import model_base |
| 19 |
from oslo_config import cfg |
| 20 |
from oslo_db.sqlalchemy import session |
| 21 |
import sqlalchemy as sa |
| 22 |
from sqlalchemy import event |
| 23 |
|
| 24 |
from neutron_dynamic_routing.db.migration.models import head |
| 25 |
|
| 26 |
|
| 27 |
MYSQL_ENGINE = None |
| 28 |
DR_VERSION_TABLE = 'alembic_version_dr' |
| 29 |
config = context.config |
| 30 |
neutron_config = config.neutron_config |
| 31 |
logging_config.fileConfig(config.config_file_name) |
| 32 |
target_metadata = model_base.BASEV2.metadata |
| 33 |
|
| 34 |
|
| 35 |
def set_mysql_engine(): |
| 36 |
try: |
| 37 |
mysql_engine = neutron_config.command.mysql_engine |
| 38 |
except cfg.NoSuchOptError: |
| 39 |
mysql_engine = None |
| 40 |
|
| 41 |
global MYSQL_ENGINE |
| 42 |
MYSQL_ENGINE = (mysql_engine or |
| 43 |
model_base.BASEV2.__table_args__['mysql_engine']) |
| 44 |
|
| 45 |
|
| 46 |
def run_migrations_offline(): |
| 47 |
set_mysql_engine() |
| 48 |
|
| 49 |
kwargs = dict() |
| 50 |
if neutron_config.database.connection: |
| 51 |
kwargs['url'] = neutron_config.database.connection |
| 52 |
else: |
| 53 |
kwargs['dialect_name'] = neutron_config.database.engine |
| 54 |
kwargs['version_table'] = DR_VERSION_TABLE |
| 55 |
context.configure(**kwargs) |
| 56 |
|
| 57 |
with context.begin_transaction(): |
| 58 |
context.run_migrations() |
| 59 |
|
| 60 |
|
| 61 |
@event.listens_for(sa.Table, 'after_parent_attach') |
| 62 |
def set_storage_engine(target, parent): |
| 63 |
if MYSQL_ENGINE: |
| 64 |
target.kwargs['mysql_engine'] = MYSQL_ENGINE |
| 65 |
|
| 66 |
|
| 67 |
def run_migrations_online(): |
| 68 |
set_mysql_engine() |
| 69 |
engine = session.create_engine(neutron_config.database.connection) |
| 70 |
|
| 71 |
connection = engine.connect() |
| 72 |
context.configure( |
| 73 |
connection=connection, |
| 74 |
target_metadata=target_metadata, |
| 75 |
version_table=DR_VERSION_TABLE |
| 76 |
) |
| 77 |
try: |
| 78 |
with context.begin_transaction(): |
| 79 |
context.run_migrations() |
| 80 |
finally: |
| 81 |
connection.close() |
| 82 |
engine.dispose() |
| 83 |
|
| 84 |
|
| 85 |
if context.is_offline_mode(): |
| 86 |
run_migrations_offline() |
| 87 |
else: |
| 88 |
run_migrations_online() |