|
1
|
|
|
from __future__ import with_statement |
|
2
|
|
|
from alembic import context |
|
3
|
|
|
from sqlalchemy import engine_from_config, pool |
|
4
|
|
|
from logging.config import fileConfig |
|
5
|
|
|
import logging |
|
6
|
|
|
|
|
7
|
|
|
# this is the Alembic Config object, which provides |
|
8
|
|
|
# access to the values within the .ini file in use. |
|
9
|
|
|
config = context.config |
|
10
|
|
|
|
|
11
|
|
|
# Interpret the config file for Python logging. |
|
12
|
|
|
# This line sets up loggers basically. |
|
13
|
|
|
fileConfig(config.config_file_name) |
|
14
|
|
|
logger = logging.getLogger('alembic.env') |
|
15
|
|
|
|
|
16
|
|
|
# add your model's MetaData object here |
|
17
|
|
|
# for 'autogenerate' support |
|
18
|
|
|
# from myapp import mymodel |
|
19
|
|
|
# target_metadata = mymodel.Base.metadata |
|
20
|
|
|
from flask import current_app |
|
21
|
|
|
config.set_main_option('sqlalchemy.url', |
|
22
|
|
|
current_app.config.get('SQLALCHEMY_DATABASE_URI')) |
|
23
|
|
|
target_metadata = current_app.extensions['migrate'].db.metadata |
|
24
|
|
|
|
|
25
|
|
|
# other values from the config, defined by the needs of env.py, |
|
26
|
|
|
# can be acquired: |
|
27
|
|
|
# my_important_option = config.get_main_option("my_important_option") |
|
28
|
|
|
# ... etc. |
|
29
|
|
|
|
|
30
|
|
|
# from https://gist.github.com/utek/6163250 |
|
31
|
|
|
def exclude_tables_from_config(config_): |
|
32
|
|
|
tables_ = config_.get("tables", None) |
|
33
|
|
|
if tables_ is not None: |
|
34
|
|
|
tables = tables_.split(",") |
|
35
|
|
|
return tables |
|
36
|
|
|
|
|
37
|
|
|
exclude_tables = exclude_tables_from_config(config.get_section('alembic:exclude')) |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
def include_object(object, name, type_, reflected, compare_to): |
|
41
|
|
|
if type_ == "table" and name in exclude_tables: |
|
42
|
|
|
return False |
|
43
|
|
|
else: |
|
44
|
|
|
return True |
|
45
|
|
|
|
|
46
|
|
|
def run_migrations_offline(): |
|
47
|
|
|
"""Run migrations in 'offline' mode. |
|
48
|
|
|
|
|
49
|
|
|
This configures the context with just a URL |
|
50
|
|
|
and not an Engine, though an Engine is acceptable |
|
51
|
|
|
here as well. By skipping the Engine creation |
|
52
|
|
|
we don't even need a DBAPI to be available. |
|
53
|
|
|
|
|
54
|
|
|
Calls to context.execute() here emit the given string to the |
|
55
|
|
|
script output. |
|
56
|
|
|
|
|
57
|
|
|
""" |
|
58
|
|
|
url = config.get_main_option("sqlalchemy.url") |
|
59
|
|
|
context.configure(url=url, include_object=include_object) |
|
60
|
|
|
|
|
61
|
|
|
with context.begin_transaction(): |
|
62
|
|
|
context.run_migrations() |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
def run_migrations_online(): |
|
66
|
|
|
"""Run migrations in 'online' mode. |
|
67
|
|
|
|
|
68
|
|
|
In this scenario we need to create an Engine |
|
69
|
|
|
and associate a connection with the context. |
|
70
|
|
|
|
|
71
|
|
|
""" |
|
72
|
|
|
|
|
73
|
|
|
# this callback is used to prevent an auto-migration from being generated |
|
74
|
|
|
# when there are no changes to the schema |
|
75
|
|
|
# reference: http://alembic.readthedocs.org/en/latest/cookbook.html |
|
76
|
|
|
def process_revision_directives(context, revision, directives): |
|
77
|
|
|
if getattr(config.cmd_opts, 'autogenerate', False): |
|
78
|
|
|
script = directives[0] |
|
79
|
|
|
if script.upgrade_ops.is_empty(): |
|
80
|
|
|
directives[:] = [] |
|
81
|
|
|
logger.info('No changes in schema detected.') |
|
82
|
|
|
|
|
83
|
|
|
engine = engine_from_config(config.get_section(config.config_ini_section), |
|
84
|
|
|
prefix='sqlalchemy.', |
|
85
|
|
|
poolclass=pool.NullPool) |
|
86
|
|
|
|
|
87
|
|
|
connection = engine.connect() |
|
88
|
|
|
context.configure(connection=connection, |
|
89
|
|
|
target_metadata=target_metadata, |
|
90
|
|
|
process_revision_directives=process_revision_directives, |
|
91
|
|
|
**current_app.extensions['migrate'].configure_args, |
|
92
|
|
|
include_object=include_object) |
|
93
|
|
|
|
|
94
|
|
|
try: |
|
95
|
|
|
with context.begin_transaction(): |
|
96
|
|
|
context.run_migrations() |
|
97
|
|
|
finally: |
|
98
|
|
|
connection.close() |
|
99
|
|
|
|
|
100
|
|
|
if context.is_offline_mode(): |
|
101
|
|
|
run_migrations_offline() |
|
102
|
|
|
else: |
|
103
|
|
|
run_migrations_online() |
|
104
|
|
|
|