from __future__ import annotations import sqlite3 from flask_migrate import Migrate from flask_sqlalchemy import SQLAlchemy from sqlalchemy import MetaData, event from sqlalchemy.engine import Engine NAMING_CONVENTION = { "ix": "ix_%(column_0_label)s", "uq": "uq_%(table_name)s_%(column_0_name)s", "ck": "ck_%(table_name)s_%(constraint_name)s", "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", "pk": "pk_%(table_name)s", } metadata = MetaData(naming_convention=NAMING_CONVENTION) db = SQLAlchemy(metadata=metadata) migrate = Migrate(compare_type=True, render_as_batch=True) @event.listens_for(Engine, "connect") def configure_sqlite_connection(dbapi_connection: object, _connection_record: object) -> None: if not isinstance(dbapi_connection, sqlite3.Connection): return cursor = dbapi_connection.cursor() try: cursor.execute("PRAGMA foreign_keys = ON;") cursor.execute("PRAGMA busy_timeout = 5000;") cursor.execute("PRAGMA temp_store = MEMORY;") cursor.execute("PRAGMA synchronous = NORMAL;") cursor.execute("PRAGMA journal_mode = WAL;") finally: cursor.close()