fix(db): add CHECK constraint for userId XOR guestToken invariant (#23) #26

Open
hermes wants to merge 1 commit from fix/order-user-guest-check-constraint into master

1 commit

Author SHA1 Message Date
Hermes Agent
928a995298 fix(db): add CHECK constraint for userId XOR guestToken invariant
Some checks failed
CI / Lint, type check, unit tests, coverage (pull_request) Successful in 3m9s
CI / E2E browser tests (pull_request) Failing after 2m31s
The Order entity Javadoc states "Either userId or guestToken is set;
never both, never neither" but only the @PrePersist lifecycle callback
enforced this in Java. A stray INSERT (admin tooling, manual SQL) could
violate it silently — creating an order with neither set (invisible to
both JWT and guest lookup paths) or both set (ambiguous ownership).

Changes:
  - V13 Flyway migration adds a CHECK constraint on the orders table:
    CHECK ((user_id IS NULL) <> (guest_token IS NULL))
    This evaluates TRUE when exactly one column is NULL (the other is
    set), and FALSE when both are NULL or both are set.
  - Standard SQL CHECK constraint, supported by both H2 (tests/dev) and
    PostgreSQL (prod).

Verified:
  - Flyway migration check passes (V13 is next available version)
  - ./gradlew :backend:test — BUILD SUCCESSFUL (all tests pass)
  - H2 accepts the constraint at Flyway migration time

Closes #23
2026-07-18 11:35:22 +00:00