Skip to content

feat: Make create_constraint enforce index uniqueness#4542

Closed
Ludv1gL wants to merge 1 commit intoclockworklabs:masterfrom
Ludv1gL:feat/make-constraint-index-unique
Closed

feat: Make create_constraint enforce index uniqueness#4542
Ludv1gL wants to merge 1 commit intoclockworklabs:masterfrom
Ludv1gL:feat/make-constraint-index-unique

Conversation

@Ludv1gL
Copy link
Contributor

@Ludv1gL Ludv1gL commented Mar 4, 2026

Summary

Addresses review feedback on #4465. The previous implementation only inserted metadata into system tables but never converted the in-memory index from non-unique to unique.

  • Index infrastructure: Add SameKeyEntry::count(), iter_duplicates(), check_and_into_unique() on MultiMap/HashIndex for duplicate detection via existing index infra. Add from_non_unique()/into_non_unique() for lossless conversion between unique and non-unique index types. Add TypedIndex::make_unique()/make_non_unique()/iter_duplicates() via define_uniqueness_conversions! macro covering all 36 variant pairs.
  • Table: Add take_pointer_map()/restore_pointer_map()/has_unique_index() for pointer map lifecycle management.
  • Datastore: Rename create_constraintcreate_st_constraint (metadata-only, used by create_table). New create_constraint calls create_st_constraint then makes index unique on both tx and commit tables with can_merge check. Same pattern for drop_constraint/drop_st_constraint. Enrich PendingSchemaChange::ConstraintAdded with IndexId and PointerMap for correct rollback.
  • Migration: Remove CheckAddUniqueConstraintValid precheck — duplicate detection now happens inside create_constraint using the index directly. Remove dead AddUniqueConstraint error variant.
  • Tests: 6 transactionality tests covering create/drop constraint for single-col, multi-col, rollback, and duplicate rejection.

Test plan

  • cargo test -p spacetimedb-table — 99 tests pass
  • cargo test -p spacetimedb-datastore --features test — 89 tests pass (6 new)
  • cargo test -p spacetimedb-schema -- auto_migrate — 12 tests pass
  • cargo build -p spacetimedb-core — compiles clean

🤖 Generated with Claude Code

Addresses review feedback on PR clockworklabs#4465. The previous implementation only
inserted metadata into system tables but never converted the in-memory
index from non-unique to unique.

Changes:
- Add SameKeyEntry::count(), iter_duplicates(), and check_and_into_unique()
  on MultiMap/HashIndex for duplicate detection via existing index infra
- Add from_non_unique()/into_non_unique() on UniqueMap/UniqueHashIndex
  for lossless conversion between unique and non-unique index types
- Add TypedIndex::make_unique()/make_non_unique()/iter_duplicates() via
  define_uniqueness_conversions! macro covering all 36 variant pairs
- Add Table::take_pointer_map()/restore_pointer_map()/has_unique_index()
- Rename create_constraint -> create_st_constraint (metadata-only, used
  by create_table), add new create_constraint that calls create_st_constraint
  then makes index unique on both tx and commit tables with can_merge check
- Same pattern for drop_constraint/drop_st_constraint
- Enrich PendingSchemaChange::ConstraintAdded with IndexId and PointerMap
  for correct rollback (make_non_unique + restore pointer map)
- Remove CheckAddUniqueConstraintValid precheck (duplicate detection now
  happens inside create_constraint using the index directly)
- Add MutTxDatastore::create_constraint_mut_tx and RelationalDB wrapper
- Add AddConstraint formatter and remove dead AddUniqueConstraint error
- Add 6 transactionality tests for create/drop constraint

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bfops
Copy link
Collaborator

bfops commented Mar 4, 2026

Thank you for opening a PR! If this is meant to respond to feedback on #4465, then the commits should be pushed to that PR since it won't be approved + merged with unaddressed feedback.

@Ludv1gL
Copy link
Contributor Author

Ludv1gL commented Mar 4, 2026

Superseded — pushed these changes to #4465 instead.

@Ludv1gL Ludv1gL closed this Mar 4, 2026
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.

2 participants