This repository has been archived by the owner on Feb 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
env.py
75 lines (58 loc) · 2.15 KB
/
env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Stdlib
from logging.config import fileConfig
import os
import sys
# External Libraries
from alembic import context
from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
from framework.db import (
db as target_metadata,
) # noqa: E402 isort:skip pylint: disable=wrong-import-position
import framework.models # noqa: F401 E402 isort:skip pylint: disable=unused-import,wrong-import-position
from framework.settings import (
SETTINGS,
) # noqa: E402 isort:skip pylint: disable=wrong-import-position
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def get_url():
return URL(
"postgres",
username=SETTINGS["DB_USER"],
password=SETTINGS["DB_PASS"],
host=SETTINGS["DB_HOST"],
port=SETTINGS["DB_PORT"],
database=SETTINGS["DB_NAME"],
)
def run_migrations_offline():
"""
Run migrations in 'offline' mode. Run when passing the `--sql flag to alembic.
Outputs an SQL string which is used to upgrade/downgrade a database, instead of working on it through alembic.
"""
url = get_url()
context.configure(url=url, target_metadata=target_metadata, literal_binds=True)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""
Run migrations in 'online' mode.
Connects to the given database and automatically applies the wanted upgrades/downgrades.
"""
connectable = create_engine(get_url())
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()