gitscope
← marzneshin / v0.5.4 / app/db/migrations/env.py
https://dev.kronoz.win/r/marzneshin.git
app/db/migrations/env.py · Python
Blame Raw
 1from logging.config import fileConfig
 2
 3from alembic import context
 4from sqlalchemy import engine_from_config
 5from sqlalchemy import pool
 6
 7from app.config.env import SQLALCHEMY_DATABASE_URL
 8from app.db.base import Base
 9
10# this is the Alembic Config object, which provides
11# access to the values within the .ini file in use.
12config = context.config
13config.set_main_option("sqlalchemy.url", SQLALCHEMY_DATABASE_URL)
14
15# Interpret the config file for Python logging.
16# This line sets up loggers basically.
17if config.config_file_name is not None:
18    fileConfig(config.config_file_name)
19
20# add your model's MetaData object here
21# for 'autogenerate' support
22# from myapp import mymodel
23# target_metadata = mymodel.Base.metadata
24target_metadata = Base.metadata
25
26# other values from the config, defined by the needs of env.py,
27# can be acquired:
28# my_important_option = config.get_main_option("my_important_option")
29# ... etc.
30
31
32def run_migrations_offline() -> None:
33    """Run migrations in 'offline' mode.
34
35    This configures the context with just a URL
36    and not an Engine, though an Engine is acceptable
37    here as well.  By skipping the Engine creation
38    we don't even need a DBAPI to be available.
39
40    Calls to context.execute() here emit the given string to the
41    script output.
42
43    """
44    url = config.get_main_option("sqlalchemy.url")
45    context.configure(
46        url=url,
47        target_metadata=target_metadata,
48        literal_binds=True,
49        dialect_opts={"paramstyle": "named"},
50    )
51
52    with context.begin_transaction():
53        context.run_migrations()
54
55
56def run_migrations_online() -> None:
57    """Run migrations in 'online' mode.
58
59    In this scenario we need to create an Engine
60    and associate a connection with the context.
61
62    """
63    connectable = engine_from_config(
64        config.get_section(config.config_ini_section),
65        prefix="sqlalchemy.",
66        poolclass=pool.NullPool,
67    )
68
69    with connectable.connect() as connection:
70        context.configure(
71            connection=connection, target_metadata=target_metadata
72        )
73
74        with context.begin_transaction():
75            context.run_migrations()
76
77
78if context.is_offline_mode():
79    run_migrations_offline()
80else:
81    run_migrations_online()