Debian Codegraph @

neutron_taas/db/migration/alembic_migration/env.py

raw
1 # Copyright 2015 Midokura SARL
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from logging import config as logging_config
16
17 from neutron_lib.db import model_base
18
19 from alembic import context
20 from oslo_config import cfg
21 from oslo_db.sqlalchemy import session
22 import sqlalchemy as sa
23 from sqlalchemy import event
24
25
26 MYSQL_ENGINE = None
27 TAAS_VERSION_TABLE = 'alembic_version_taas'
28 config = context.config
29 neutron_config = config.neutron_config
30 logging_config.fileConfig(config.config_file_name)
31 target_metadata = model_base.BASEV2.metadata
32
33
34 def set_mysql_engine():
35 try:
36 mysql_engine = neutron_config.command.mysql_engine
37 except cfg.NoSuchOptError:
38 mysql_engine = None
39
40 global MYSQL_ENGINE
41 MYSQL_ENGINE = (mysql_engine or
42 model_base.BASEV2.__table_args__['mysql_engine'])
43
44
45 def run_migrations_offline():
46 set_mysql_engine()
47
48 kwargs = {}
49 if neutron_config.database.connection:
50 kwargs['url'] = neutron_config.database.connection
51 else:
52 kwargs['dialect_name'] = neutron_config.database.engine
53 kwargs['version_table'] = TAAS_VERSION_TABLE
54 context.configure(**kwargs)
55
56 with context.begin_transaction():
57 context.run_migrations()
58
59
60 @event.listens_for(sa.Table, 'after_parent_attach')
61 def set_storage_engine(target, parent):
62 if MYSQL_ENGINE:
63 target.kwargs['mysql_engine'] = MYSQL_ENGINE
64
65
66 def run_migrations_online():
67 set_mysql_engine()
68 engine = session.create_engine(neutron_config.database.connection)
69
70 connection = engine.connect()
71 context.configure(
72 connection=connection,
73 target_metadata=target_metadata,
74 version_table=TAAS_VERSION_TABLE
75 )
76 try:
77 with context.begin_transaction():
78 context.run_migrations()
79 finally:
80 connection.close()
81 engine.dispose()
82
83
84 if context.is_offline_mode():
85 run_migrations_offline()
86 else:
87 run_migrations_online()