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
pull from: fix/order-user-guest-check-constraint
merge into: jocke:master
jocke:master
jocke:fix/guest-payment-pending-review
jocke:fix/guest-order-rate-limiting
jocke:fix/guest-token-session-storage
jocke:fix/persist-amount-paid
jocke:fix/guest-qr-scannability
jocke:feature/guest-checkout
jocke:fix/swish-qr-scannability
jocke:feature/swish-qr-payment
jocke:develop
jocke:feature/auto-version-from-tag
jocke:fix/admin-table-styling
jocke:feature/expired-token-logout
jocke:chore/dockerfile-self-contained
jocke:chore/pre-commit-coverage-thresholds
jocke:feature/umami-analytics
jocke:refactor/admin-fulfillment
jocke:feature/admin-fulfillment-tracking
jocke:feature/account-settings-dropdown
jocke:feature/cancel-edit-pending-orders
1 commit
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
928a995298 |
fix(db): add CHECK constraint for userId XOR guestToken invariant
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
|