- Base común con convención de nombres; mixins ColumnasBase (id, created_at, created_by), Tenant (tenant_id) e Historico (valid_from/valid_to) - Alembic lee DATABASE_URL del entorno y usa el engine de la app (WAL, claves foráneas) - tests: la migración crea las tres tablas y coincide con los modelos; convenciones de columnas y snake_case; la sesión rechaza borrados
51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
"""Entorno de Alembic: lee DATABASE_URL del entorno y usa `Base.metadata` de app.models.
|
|
|
|
`render_as_batch=True` porque SQLite no soporta ALTER TABLE completo.
|
|
"""
|
|
|
|
from logging.config import fileConfig
|
|
|
|
from alembic import context
|
|
|
|
from app.config import get_settings
|
|
from app.db import crear_engine
|
|
from app.models import Base
|
|
|
|
config = context.config
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
# Una URL fijada por código (tests) gana sobre DATABASE_URL del entorno.
|
|
if not config.get_main_option("sqlalchemy.url"):
|
|
config.set_main_option("sqlalchemy.url", get_settings().database_url)
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
context.configure(
|
|
url=config.get_main_option("sqlalchemy.url"),
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
render_as_batch=True,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def run_migrations_online() -> None:
|
|
# Mismo engine que la app: WAL y claves foráneas también durante la migración.
|
|
connectable = crear_engine(config.get_main_option("sqlalchemy.url"))
|
|
with connectable.connect() as connection:
|
|
context.configure(
|
|
connection=connection, target_metadata=target_metadata, render_as_batch=True
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|