Files
plm-lottery/apps/lottery/migrations/0002_audit_append_only_triggers.py
T
davideandClaude Sonnet 5.5 0eba7bcfe6 A2: lottery models and database constraints (WEB-04, ADR-10, ADR-15, ADR-22, DAT-76..79, AUD-47)
LotteryConfig (single row, parameters and the three pause switches), Round
(single active round, own rules, split within the pool), Participation (one
bet per player and round, unique draw position), Movement (signed ledger,
idempotency key per player), ChainTransaction and an append-only AuditEntry
enforced by the model and by SQLite triggers. Every foreign key is PROTECT.
One test per constraint, each checked to hit the intended constraint.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 12:13:10 +01:00

25 lines
922 B
Python

"""The audit log is append-only in the database too (AUD-47).
Whatever the code does — a raw `UPDATE`, a `QuerySet._raw_delete`, a shell session — a row of
`lottery_auditentry` can only be inserted. SQLite syntax; PostgreSQL gets its own migration the
day it replaces SQLite (ADR-55).
"""
from django.db import migrations
CREATE = [
"""CREATE TRIGGER lottery_audit_no_update BEFORE UPDATE ON lottery_auditentry
BEGIN SELECT RAISE(ABORT, 'audit log is append-only'); END""",
"""CREATE TRIGGER lottery_audit_no_delete BEFORE DELETE ON lottery_auditentry
BEGIN SELECT RAISE(ABORT, 'audit log is append-only'); END""",
]
DROP = [
"DROP TRIGGER IF EXISTS lottery_audit_no_update",
"DROP TRIGGER IF EXISTS lottery_audit_no_delete",
]
class Migration(migrations.Migration):
dependencies = [("lottery", "0001_initial")]
operations = [migrations.RunSQL(sql=CREATE, reverse_sql=DROP)]