Skip to content

storage(schema): enrollment_event permits a non-version-change event with one version reference #51

Closed

Description

@TusanHomichi

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions