fix(v2): enforce M1 runtime persistence invariants

This commit is contained in:
2026-07-27 19:42:41 +02:00
parent 45a526a1e9
commit 15f8794029
7 changed files with 188 additions and 39 deletions
+25 -3
View File
@@ -3,17 +3,19 @@ from __future__ import annotations
import importlib
import os
from logging.config import fileConfig
from typing import Any
from alembic import context
from sqlalchemy import create_engine, pool
from sqlalchemy import create_engine, event, pool
from sqlalchemy.engine import URL, make_url
Base = importlib.import_module("backup_tool.db.models").Base
models = importlib.import_module("backup_tool.db.models")
sqlite_runtime = importlib.import_module("backup_tool.db.sqlite")
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
target_metadata = models.Base.metadata
def configured_url() -> str:
@@ -23,6 +25,19 @@ def configured_url() -> str:
return raw_url
def busy_timeout_ms() -> int:
raw_value = os.environ.get(
"BACKUP_TOOL_SQLITE_BUSY_TIMEOUT_MS", str(sqlite_runtime.DEFAULT_BUSY_TIMEOUT_MS)
)
try:
value = int(raw_value)
except ValueError as error:
raise RuntimeError("SQLite busy timeout must be an integer") from error
if value < 1_000 or value > 120_000:
raise RuntimeError("SQLite busy timeout must be between 1000 and 120000 ms")
return value
def synchronous_url(raw_url: str) -> URL:
url = make_url(raw_url)
if url.drivername != "sqlite+aiosqlite":
@@ -44,10 +59,17 @@ def run_migrations_offline() -> None:
def run_migrations_online() -> None:
timeout = busy_timeout_ms()
engine = create_engine(
synchronous_url(configured_url()),
poolclass=pool.NullPool,
connect_args=sqlite_runtime.sqlite_connect_args(timeout),
)
@event.listens_for(engine, "connect")
def configure_sqlite(dbapi_connection: Any, _connection_record: Any) -> None:
sqlite_runtime.configure_sqlite_connection(dbapi_connection)
with engine.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata, compare_type=True)
with context.begin_transaction():