54 lines
1.8 KiB
Python
54 lines
1.8 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:
|
|
# Sin deshabilitar los loggers que ya existen: si no, una migración dentro de la suite apaga
|
|
# los de la aplicación (p. ej. `app.services.alertas`) y las pruebas que leen el log fallan
|
|
# según el orden en que corren (backlog O13).
|
|
fileConfig(config.config_file_name, disable_existing_loggers=False)
|
|
|
|
# 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()
|