Debian Codegraph @

neutron_fwaas/db/migration/alembic_migrations/env.py

raw symbols SCIP
1 # Copyright 2014 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 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
25 MYSQL_ENGINE = None
26 FWAAS_VERSION_TABLE = 'alembic_version_fwaas'
27 config = context.config
28 neutron_config = config.neutron_config
29 logging_config.fileConfig(config.config_file_name)
30 target_metadata = model_base.BASEV2.metadata
31
32
33 def set_mysql_engine():
34 try:
35 mysql_engine = neutron_config.command.mysql_engine
36 except cfg.NoSuchOptError:
37 mysql_engine = None
38
39 global MYSQL_ENGINE
40 MYSQL_ENGINE = (mysql_engine or
41 model_base.BASEV2.__table_args__['mysql_engine'])
42
43
44 def run_migrations_offline():
45 set_mysql_engine()
46
47 kwargs = dict()
48 if neutron_config.database.connection:
49 kwargs['url'] = neutron_config.database.connection
50 else:
51 kwargs['dialect_name'] = neutron_config.database.engine
52 kwargs['version_table'] = FWAAS_VERSION_TABLE
53 context.configure(**kwargs)
54
55 with context.begin_transaction():
56 context.run_migrations()
57
58
59 @event.listens_for(sa.Table, 'after_parent_attach')
60 def set_storage_engine(target, parent):
61 if MYSQL_ENGINE:
62 target.kwargs['mysql_engine'] = MYSQL_ENGINE
63
64
65 def run_migrations_online():
66 set_mysql_engine()
67 engine = session.create_engine(neutron_config.database.connection)
68
69 connection = engine.connect()
70 context.configure(
71 connection=connection,
72 target_metadata=target_metadata,
73 version_table=FWAAS_VERSION_TABLE
74 )
75 try:
76 with context.begin_transaction():
77 context.run_migrations()
78 finally:
79 connection.close()
80 engine.dispose()
81
82
83 if context.is_offline_mode():
84 run_migrations_offline()
85 else:
86 run_migrations_online()