Newer
Older
gn-synapse / alembic / env.py
"""Alembic env: URL берём из настроек Synapse, метаданные — из app.models."""

from alembic import context
from sqlalchemy import pool

from app.config import get_settings
from app.database import engine

# Импорт моделей здесь обязателен: Base.metadata должен знать все таблицы.
# from app import models  # включить в задаче #34 (схема БД) — пока моделей нет

config = context.config
target_metadata = __import__("app").database.Base.metadata


def run_migrations_offline() -> None:
    context.configure(
        url=get_settings().database_url,
        target_metadata=target_metadata,
        literal_binds=True,
        dialect_opts={"paramstyle": "named"},
    )
    with context.begin_transaction():
        context.run_migrations()


def run_migrations_online() -> None:
    connectable = engine
    with connectable.connect() as connection:
        context.configure(connection=connection, target_metadata=target_metadata)
        with context.begin_transaction():
            context.run_migrations()


if context.is_offline_mode():
    run_migrations_offline()
else:
    run_migrations_online()