Initial commit

This commit is contained in:
2025-12-31 00:40:41 +03:00
parent a615f43983
commit d4898869fa
10 changed files with 258 additions and 35 deletions
+37 -13
View File
@@ -1,23 +1,47 @@
import asyncio
from logging.config import fileConfig
from sqlalchemy import pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import async_engine_from_config
from sqlalchemy import Connection
from sqlalchemy.ext.asyncio import create_async_engine
from alembic import context
from trudex.infrastructure.database.models import Base
from trudex.infrastructure.utils.config import Config
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = None
# add your model's MetaData object here
# for 'autogenerate' support
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
db_config = Config.from_toml("config.toml").database
def run_migrations_offline() -> None:
url = config.get_main_option("sqlalchemy.url")
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
context.configure(
url=url,
url=db_config.url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
@@ -27,7 +51,7 @@ def run_migrations_offline() -> None:
context.run_migrations()
def do_run_migrations(connection: Connection) -> None:
def do_run_migrations(connection: Connection):
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
@@ -35,11 +59,11 @@ def do_run_migrations(connection: Connection) -> None:
async def run_async_migrations() -> None:
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
"""In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = create_async_engine(db_config.url)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
@@ -48,7 +72,7 @@ async def run_async_migrations() -> None:
def run_migrations_online() -> None:
import asyncio
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())
+6 -5
View File
@@ -1,19 +1,20 @@
${message}
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
from typing import Sequence, Union
"""
from collections.abc import Sequence
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
down_revision: str | None = ${repr(down_revision)}
branch_labels: str | Sequence[str] | None = ${repr(branch_labels)}
depends_on: str | Sequence[str] | None = ${repr(depends_on)}
def upgrade() -> None:
+39
View File
@@ -0,0 +1,39 @@
"""initial
Revision ID: 409f04b7b544
Revises:
Create Date: 2025-12-31 00:38:10.367405
"""
from collections.abc import Sequence
from alembic import op
import sqlalchemy as sa
revision: str = '409f04b7b544'
down_revision: str | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('users',
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('username', sa.String(length=32), nullable=True),
sa.Column('first_name', sa.String(length=64), nullable=False),
sa.Column('last_name', sa.String(length=64), nullable=True),
sa.Column('group', sa.Integer(), nullable=True),
sa.Column('is_admin', sa.Boolean(), nullable=False),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=False),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('users')
# ### end Alembic commands ###