-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
32 lines (25 loc) · 927 Bytes
/
main.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
import logging
from alembic import context
from sqlalchemy import create_engine
config = context.config
logger = logging.getLogger("alembic.env")
def get_engine():
"""Creates an engine based on Alembic config."""
db_url = config.get_main_option("sqlalchemy.url")
if not db_url:
logger.error("No database URL found in Alembic config.")
raise RuntimeError("Database URL missing")
return create_engine(db_url)
def run_migrations():
"""Executes migrations in online mode."""
engine = get_engine()
with engine.connect() as connection:
context.configure(connection=connection, target_metadata=None)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
context.configure(url=config.get_main_option("sqlalchemy.url"))
with context.begin_transaction():
context.run_migrations()
else:
run_migrations()