Debian Codegraph @

networking_l2gw/db/migration/alembic_migrations/env.py

raw
1 # Copyright 2015 OpenStack Foundation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # 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, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14 #
15
16 from logging import config as logging_config
17
18 from alembic import context
19 from neutron_lib.db import model_base
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 from neutron.db.migration.alembic_migrations import external
26 from neutron.db.migration.models import head # noqa
27
28 MYSQL_ENGINE = None
29 L2GW_VERSION_TABLE = 'l2gw_alembic_version'
30 config = context.config
31 neutron_config = config.neutron_config
32 logging_config.fileConfig(config.config_file_name)
33 target_metadata = model_base.BASEV2.metadata
34
35
36 def set_mysql_engine():
37 try:
38 mysql_engine = neutron_config.command.mysql_engine
39 except cfg.NoSuchOptError:
40 mysql_engine = None
41
42 global MYSQL_ENGINE
43 MYSQL_ENGINE = (mysql_engine or
44 model_base.BASEV2.__table_args__['mysql_engine'])
45
46
47 def include_object(object, name, type_, reflected, compare_to):
48 if type_ == 'table' and name in external.TABLES:
49 return False
50 else:
51 return True
52
53
54 def run_migrations_offline():
55 set_mysql_engine()
56
57 kwargs = dict()
58 if neutron_config.database.connection:
59 kwargs['url'] = neutron_config.database.connection
60 else:
61 kwargs['dialect_name'] = neutron_config.database.engine
62 kwargs['include_object'] = include_object
63 kwargs['version_table'] = L2GW_VERSION_TABLE
64 context.configure(**kwargs)
65
66 with context.begin_transaction():
67 context.run_migrations()
68
69
70 @event.listens_for(sa.Table, 'after_parent_attach')
71 def set_storage_engine(target, parent):
72 if MYSQL_ENGINE:
73 target.kwargs['mysql_engine'] = MYSQL_ENGINE
74
75
76 def run_migrations_online():
77 set_mysql_engine()
78 engine = session.create_engine(neutron_config.database.connection)
79
80 connection = engine.connect()
81 context.configure(
82 connection=connection,
83 target_metadata=target_metadata,
84 include_object=include_object,
85 version_table=L2GW_VERSION_TABLE
86 )
87
88 try:
89 with context.begin_transaction():
90 context.run_migrations()
91 finally:
92 connection.close()
93 engine.dispose()
94
95
96 if context.is_offline_mode():
97 run_migrations_offline()
98 else:
99 run_migrations_online()