aboutsummaryrefslogtreecommitdiff
path: root/alembic
diff options
context:
space:
mode:
authorTolmachev Igor <me@igorek.dev>2026-03-22 19:56:47 +0300
committerTolmachev Igor <me@igorek.dev>2026-03-22 20:19:27 +0300
commit536d022e8a55f6e53f01dfb7e0fae2ef24385aad (patch)
tree721d7195db7fc12c725b27301f77e345df64d1b4 /alembic
downloadvpn_manager_bot-536d022e8a55f6e53f01dfb7e0fae2ef24385aad.tar.gz
vpn_manager_bot-536d022e8a55f6e53f01dfb7e0fae2ef24385aad.zip
Init project
Diffstat (limited to 'alembic')
-rw-r--r--alembic/env.py93
-rw-r--r--alembic/script.py.mako28
2 files changed, 121 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 @@
1import asyncio
2from logging.config import fileConfig
3
4from sqlalchemy import pool
5from sqlalchemy.engine import Connection
6from sqlalchemy.ext.asyncio import async_engine_from_config
7
8import models
9from alembic import context
10from 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.
14config = context.config
15config.set_main_option("sqlalchemy.url", database_url)
16
17# Interpret the config file for Python logging.
18# This line sets up loggers basically.
19if 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
26target_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
34def 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
59def 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
66async 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
84def run_migrations_online() -> None:
85 """Run migrations in 'online' mode."""
86
87 asyncio.run(run_async_migrations())
88
89
90if context.is_offline_mode():
91 run_migrations_offline()
92else:
93 run_migrations_online()
diff --git a/alembic/script.py.mako b/alembic/script.py.mako
new file mode 100644
index 0000000..1101630
--- /dev/null
+++ b/alembic/script.py.mako
@@ -0,0 +1,28 @@
1"""${message}
2
3Revision ID: ${up_revision}
4Revises: ${down_revision | comma,n}
5Create Date: ${create_date}
6
7"""
8from typing import Sequence, Union
9
10from alembic import op
11import sqlalchemy as sa
12${imports if imports else ""}
13
14# revision identifiers, used by Alembic.
15revision: str = ${repr(up_revision)}
16down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
17branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
18depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
19
20
21def upgrade() -> None:
22 """Upgrade schema."""
23 ${upgrades if upgrades else "pass"}
24
25
26def downgrade() -> None:
27 """Downgrade schema."""
28 ${downgrades if downgrades else "pass"}