event listens for sqlalchemy

This commit is contained in:
IluaAir
2025-08-06 23:38:17 +03:00
parent acb3eefcbe
commit 93cf7b2d24
5 changed files with 33 additions and 14 deletions

View File

@@ -1,8 +1,7 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import engine_from_config, event
from sqlalchemy import pool
from alembic import context
from src.core.database import Base
@@ -67,6 +66,14 @@ def run_migrations_online() -> None:
poolclass=pool.NullPool,
)
# Enable foreign keys for SQLite in migrations
@event.listens_for(connectable, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
print("⚙️ Enabling PRAGMA foreign_keys=ON for Alembic")
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON")
cursor.close()
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)