Description
Evidence
crates/consolebook-server/migrations/0006_lifecycle.sql constrains lifecycle events with
CHECK ((kind = 'version_change')
= (from_program_version_id IS NOT NULL AND to_program_version_id IS NOT NULL)),For withdraw, complete, and reinstate the left side is false, so the right side must be false: at least one of the two references must be NULL. A row with exactly one reference set (for example withdraw with from_program_version_id set and to_program_version_id NULL) satisfies the constraint, although docs/domain-model.md (Enrollment) and the trainee packet format (docs/formats/trainee-packet.md, #48) state that only a version change names versions.
Impact
None observed: lifecycle::record_enrollment_event writes both references for a version change and neither otherwise, and the packet verifier (#50) refuses such an event when it meets one in a packet. The gap is that the schema, not only the service, should make the shape impossible, per the invariant discipline in AGENTS.md and docs/domain-model.md ("Database invariants to enforce").
Proposed fix
A migration that rebuilds enrollment_event with
CHECK (CASE kind
WHEN 'version_change'
THEN from_program_version_id IS NOT NULL AND to_program_version_id IS NOT NULL
ELSE from_program_version_id IS NULL AND to_program_version_id IS NULL
END)SQLite cannot alter a CHECK in place, so this is a table rebuild (copy, drop, rename, recreate the index and triggers), with a test that the one-reference shape is refused at insert. Found while tightening the packet verifier in #50; Refs #48.
Activity