diff options
| author | Tolmachev Igor <me@igorek.dev> | 2026-03-22 19:56:47 +0300 |
|---|---|---|
| committer | Tolmachev Igor <me@igorek.dev> | 2026-03-22 20:19:27 +0300 |
| commit | 536d022e8a55f6e53f01dfb7e0fae2ef24385aad (patch) | |
| tree | 721d7195db7fc12c725b27301f77e345df64d1b4 /alembic/env.py | |
| download | vpn_manager_bot-536d022e8a55f6e53f01dfb7e0fae2ef24385aad.tar.gz vpn_manager_bot-536d022e8a55f6e53f01dfb7e0fae2ef24385aad.zip | |
Init project
Diffstat (limited to 'alembic/env.py')
| -rw-r--r-- | alembic/env.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/alembic/env.py b/alembic/env.py new file mode 100644 index 0000000..ff0b27f --- /dev/null +++ b/alembic/env.py | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | import asyncio | ||
| 2 | from logging.config import fileConfig | ||
| 3 | |||
| 4 | from sqlalchemy import pool | ||
| 5 | from sqlalchemy.engine import Connection | ||
| 6 | from sqlalchemy.ext.asyncio import async_engine_from_config | ||
| 7 | |||
| 8 | import models | ||
| 9 | from alembic import context | ||
| 10 | from settings import database_url | ||
| 11 | |||
| 12 | # this is the Alembic Config object, which provides | ||
| 13 | # access to the values within the .ini file in use. | ||
| 14 | config = context.config | ||
| 15 | config.set_main_option("sqlalchemy.url", database_url) | ||
| 16 | |||
| 17 | # Interpret the config file for Python logging. | ||
| 18 | # This line sets up loggers basically. | ||
| 19 | if config.config_file_name is not None: | ||
| 20 | fileConfig(config.config_file_name) | ||
| 21 | |||
| 22 | # add your model's MetaData object here | ||
| 23 | # for 'autogenerate' support | ||
| 24 | # from myapp import mymodel | ||
| 25 | # target_metadata = mymodel.Base.metadata | ||
| 26 | target_metadata = models.BaseTable.metadata | ||
| 27 | |||
| 28 | # other values from the config, defined by the needs of env.py, | ||
| 29 | # can be acquired: | ||
| 30 | # my_important_option = config.get_main_option("my_important_option") | ||
| 31 | # ... etc. | ||
| 32 | |||
| 33 | |||
| 34 | def run_migrations_offline() -> None: | ||
| 35 | """Run migrations in 'offline' mode. | ||
| 36 | |||
| 37 | This configures the context with just a URL | ||
| 38 | and not an Engine, though an Engine is acceptable | ||
| 39 | here as well. By skipping the Engine creation | ||
| 40 | we don't even need a DBAPI to be available. | ||
| 41 | |||
| 42 | Calls to context.execute() here emit the given string to the | ||
| 43 | script output. | ||
| 44 | |||
| 45 | """ | ||
| 46 | url = config.get_main_option("sqlalchemy.url") | ||
| 47 | context.configure( | ||
| 48 | url=url, | ||
| 49 | target_metadata=target_metadata, | ||
| 50 | literal_binds=True, | ||
| 51 | dialect_opts={"paramstyle": "named"}, | ||
| 52 | render_as_batch=True, | ||
| 53 | ) | ||
| 54 | |||
| 55 | with context.begin_transaction(): | ||
| 56 | context.run_migrations() | ||
| 57 | |||
| 58 | |||
| 59 | def do_run_migrations(connection: Connection) -> None: | ||
| 60 | context.configure(connection=connection, target_metadata=target_metadata) | ||
| 61 | |||
| 62 | with context.begin_transaction(): | ||
| 63 | context.run_migrations() | ||
| 64 | |||
| 65 | |||
| 66 | async def run_async_migrations() -> None: | ||
| 67 | """In this scenario we need to create an Engine | ||
| 68 | and associate a connection with the context. | ||
| 69 | |||
| 70 | """ | ||
| 71 | |||
| 72 | connectable = async_engine_from_config( | ||
| 73 | config.get_section(config.config_ini_section, {}), | ||
| 74 | prefix="sqlalchemy.", | ||
| 75 | poolclass=pool.NullPool, | ||
| 76 | ) | ||
| 77 | |||
| 78 | async with connectable.connect() as connection: | ||
| 79 | await connection.run_sync(do_run_migrations) | ||
| 80 | |||
| 81 | await connectable.dispose() | ||
| 82 | |||
| 83 | |||
| 84 | def run_migrations_online() -> None: | ||
| 85 | """Run migrations in 'online' mode.""" | ||
| 86 | |||
| 87 | asyncio.run(run_async_migrations()) | ||
| 88 | |||
| 89 | |||
| 90 | if context.is_offline_mode(): | ||
| 91 | run_migrations_offline() | ||
| 92 | else: | ||
| 93 | run_migrations_online() | ||
