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