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