Run child tables cascade from run.id

Migration ccb21eacf5d5 deletes rows whose run is gone, then adds foreign keys
with ON DELETE CASCADE from run_node, run_metric and run_artifact to run.id.
Deleting runs is now the one statement. A node or metric written after its
run was deleted is dropped with an info line. Migrations run with foreign keys
off, since a batch rebuild of run would otherwise cascade into every child row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgBfkm4rCmtd1T4vHWjEPx
This commit is contained in:
2026-10-15 10:32:33 +02:00
co-authored by Claude Opus 5.5
parent ffa044724b
commit b4d7ffb982
6 changed files with 104 additions and 25 deletions
+27 -13
View File
@@ -66,8 +66,9 @@ def _sqlite_pragmas(dbapi_connection: Any, _record: Any) -> None:
# Commit without waiting for the platter. A power cut can lose the last
# transactions; it cannot corrupt the file.
cursor.execute("PRAGMA synchronous=NORMAL")
# Off by default in SQLite, and `ondelete="CASCADE"` on the OAuth tables is
# the whole of revoking a client.
# Off by default in SQLite, and `ondelete="CASCADE"` is the whole of
# revoking an OAuth client and of deleting a run's nodes, metrics and
# artifacts with it.
cursor.execute("PRAGMA foreign_keys=ON")
# A second writer waits rather than raising "database is locked".
cursor.execute("PRAGMA busy_timeout=30000")
@@ -109,17 +110,30 @@ def _alembic_config(connection: Any) -> Config:
def migrate(engine: Engine) -> None:
"""Bring the schema up to date, creating it if there is nothing there."""
with engine.begin() as connection:
config = _alembic_config(connection)
tables = inspect(connection).get_table_names()
if not tables:
# Nothing to upgrade from. Building the schema from the models is
# both faster and exactly what the revisions would have produced.
SQLModel.metadata.create_all(connection)
command.stamp(config, "head")
logger.info("created the database schema")
return
command.upgrade(config, "head")
with engine.connect() as connection:
# Foreign keys off while the schema changes. A batch migration rebuilds
# a table by dropping it, and dropping `run` with them on cascades into
# every run's nodes, metrics and artifacts. SQLite ignores the pragma
# inside a transaction, so it is set before the migration's opens and
# reset after it closes.
connection.exec_driver_sql("PRAGMA foreign_keys=OFF")
connection.commit()
try:
with connection.begin():
config = _alembic_config(connection)
tables = inspect(connection).get_table_names()
if not tables:
# Nothing to upgrade from. Building the schema from the
# models is both faster and exactly what the revisions
# would have produced.
SQLModel.metadata.create_all(connection)
command.stamp(config, "head")
logger.info("created the database schema")
return
command.upgrade(config, "head")
finally:
connection.exec_driver_sql("PRAGMA foreign_keys=ON")
connection.commit()
def init_db(session: Session) -> None: