Debian Codegraph @

neutron_vpnaas/db/migration/alembic_migrations/env.py

raw
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 from neutron_vpnaas.db.migration import alembic_migrations
26
27
28 MYSQL_ENGINE = None
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'] = alembic_migrations.VPNAAS_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=alembic_migrations.VPNAAS_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()