|
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 |
|
6 import config as app_config |
|
7 import models |
|
8 |
|
9 # this is the Alembic Config object, which provides |
|
10 # access to the values within the .ini file in use. |
|
11 config = context.config |
|
12 |
|
13 # Interpret the config file for Python logging. |
|
14 # This line sets up loggers basically. |
|
15 fileConfig(config.config_file_name) |
|
16 |
|
17 # add your model's MetaData object here |
|
18 # for 'autogenerate' support |
|
19 # from myapp import mymodel |
|
20 # target_metadata = mymodel.Base.metadata |
|
21 target_metadata = models.Base.metadata |
|
22 |
|
23 # other values from the config, defined by the needs of env.py, |
|
24 # can be acquired: |
|
25 # my_important_option = config.get_main_option("my_important_option") |
|
26 # ... etc. |
|
27 |
|
28 |
|
29 def run_migrations_offline(): |
|
30 """Run migrations in 'offline' mode. |
|
31 |
|
32 This configures the context with just a URL |
|
33 and not an Engine, though an Engine is acceptable |
|
34 here as well. By skipping the Engine creation |
|
35 we don't even need a DBAPI to be available. |
|
36 |
|
37 Calls to context.execute() here emit the given string to the |
|
38 script output. |
|
39 |
|
40 """ |
|
41 url = config.get_main_option("sqlalchemy.url") |
|
42 context.configure(url=url, target_metadata=target_metadata) |
|
43 |
|
44 with context.begin_transaction(): |
|
45 context.run_migrations() |
|
46 |
|
47 |
|
48 def run_migrations_online(): |
|
49 """Run migrations in 'online' mode. |
|
50 |
|
51 In this scenario we need to create an Engine |
|
52 and associate a connection with the context. |
|
53 |
|
54 """ |
|
55 alembic_config = config.get_section(config.config_ini_section) |
|
56 alembic_config['sqlalchemy.url'] = app_config.CONN_STR |
|
57 |
|
58 engine = engine_from_config( |
|
59 alembic_config, |
|
60 prefix='sqlalchemy.', |
|
61 poolclass=pool.NullPool) |
|
62 |
|
63 connection = engine.connect() |
|
64 context.configure( |
|
65 connection=connection, |
|
66 target_metadata=target_metadata |
|
67 ) |
|
68 |
|
69 try: |
|
70 with context.begin_transaction(): |
|
71 context.run_migrations() |
|
72 finally: |
|
73 connection.close() |
|
74 |
|
75 if context.is_offline_mode(): |
|
76 run_migrations_offline() |
|
77 else: |
|
78 run_migrations_online() |