Skip to content

fix(codegen): temporal current-version UNIQUE; valid_to CHECK#81

Merged
hyperpolymath merged 1 commit into
mainfrom
fix/temporal-unique-index-check
May 14, 2026
Merged

fix(codegen): temporal current-version UNIQUE; valid_to CHECK#81
hyperpolymath merged 1 commit into
mainfrom
fix/temporal-unique-index-check

Conversation

@hyperpolymath
Copy link
Copy Markdown
Owner

Summary

Per V-L2-H1: two concurrent writers could both leave valid_to=NULL for the same entity (the partial index wasn't UNIQUE), and there was no ordering check on the validity interval.

  • Promote idx_temporal_current to CREATE UNIQUE INDEX ... WHERE valid_to IS NULL.
  • Add CHECK (valid_to IS NULL OR valid_to >= valid_from).

Closes

Test plan

  • cargo clippy --all-targets -- -D warnings clean
  • cargo test --lib --bins 37/37 (1 new: test_temporal_table_has_unique_partial_index_and_valid_to_check)

Closes #41.

`verisimdb_temporal_versions` had a non-unique partial index on
`(entity_id, table_name) WHERE valid_to IS NULL`. Two concurrent
writers could both successfully insert a row with `valid_to=NULL` for
the same entity, leaving two "current" versions and breaking the
point-in-time query semantics. There was also no constraint that
`valid_to` (when set) couldn't precede `valid_from`.

  - Promote the partial index to `CREATE UNIQUE INDEX` so the storage
    layer enforces "at most one current version per (entity, table)".
  - Add `CHECK (valid_to IS NULL OR valid_to >= valid_from)` so a
    version interval can't be backwards.

Test `test_temporal_table_has_unique_partial_index_and_valid_to_check`
asserts both clauses appear in the emitted DDL with temporal enabled.

`cargo clippy --all-targets -- -D warnings` clean; 37 unit tests pass.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@hyperpolymath hyperpolymath merged commit a4dc26e into main May 14, 2026
16 of 18 checks passed
@hyperpolymath hyperpolymath deleted the fix/temporal-unique-index-check branch May 14, 2026 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

V-L2-H1: temporal versions need UNIQUE partial index + valid_to CHECK

1 participant