1
|
|
|
import os |
2
|
|
|
from logging.config import fileConfig |
3
|
|
|
|
4
|
|
|
from sqlalchemy import engine_from_config |
5
|
|
|
from sqlalchemy import pool |
6
|
|
|
|
7
|
|
|
from alembic import context |
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
|
|
|
if config.config_file_name is not None: |
16
|
|
|
fileConfig(config.config_file_name) |
17
|
|
|
|
18
|
|
|
# add your model's MetaData object here |
19
|
|
|
# for 'autogenerate' support |
20
|
|
|
# from myapp import mymodel |
21
|
|
|
# target_metadata = mymodel.Base.metadata |
22
|
|
|
target_metadata = None |
23
|
|
|
|
24
|
|
|
# other values from the config, defined by the needs of env.py, |
25
|
|
|
# can be acquired: |
26
|
|
|
# my_important_option = config.get_main_option("my_important_option") |
27
|
|
|
# ... etc. |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def run_migrations_offline() -> None: |
31
|
|
|
"""Run migrations in 'offline' mode. |
32
|
|
|
|
33
|
|
|
This configures the context with just a URL |
34
|
|
|
and not an Engine, though an Engine is acceptable |
35
|
|
|
here as well. By skipping the Engine creation |
36
|
|
|
we don't even need a DBAPI to be available. |
37
|
|
|
|
38
|
|
|
Calls to context.execute() here emit the given string to the |
39
|
|
|
script output. |
40
|
|
|
|
41
|
|
|
""" |
42
|
|
|
url = os.getenv("DATABASE_URL", config.get_main_option("sqlalchemy.url")) |
43
|
|
|
context.configure( |
44
|
|
|
url=url, |
45
|
|
|
target_metadata=target_metadata, |
46
|
|
|
literal_binds=True, |
47
|
|
|
dialect_opts={"paramstyle": "named"}, |
48
|
|
|
) |
49
|
|
|
|
50
|
|
|
with context.begin_transaction(): |
51
|
|
|
context.run_migrations() |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def run_migrations_online() -> None: |
55
|
|
|
"""Run migrations in 'online' mode. |
56
|
|
|
|
57
|
|
|
In this scenario we need to create an Engine |
58
|
|
|
and associate a connection with the context. |
59
|
|
|
|
60
|
|
|
""" |
61
|
|
|
connectable = engine_from_config( |
62
|
|
|
config.get_section(config.config_ini_section), |
63
|
|
|
prefix="sqlalchemy.", |
64
|
|
|
poolclass=pool.NullPool, |
65
|
|
|
) |
66
|
|
|
|
67
|
|
|
with connectable.connect() as connection: |
68
|
|
|
context.configure( |
69
|
|
|
connection=connection, target_metadata=target_metadata |
70
|
|
|
) |
71
|
|
|
|
72
|
|
|
with context.begin_transaction(): |
73
|
|
|
context.run_migrations() |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
if context.is_offline_mode(): |
77
|
|
|
run_migrations_offline() |
78
|
|
|
else: |
79
|
|
|
run_migrations_online() |
80
|
|
|
|