feat: Make create_constraint enforce index uniqueness#4542
Closed
Ludv1gL wants to merge 1 commit intoclockworklabs:masterfrom
Closed
feat: Make create_constraint enforce index uniqueness#4542Ludv1gL wants to merge 1 commit intoclockworklabs:masterfrom
Ludv1gL wants to merge 1 commit intoclockworklabs:masterfrom
Conversation
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>
5 tasks
Collaborator
|
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. |
Contributor
Author
|
Superseded — pushed these changes to #4465 instead. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
SameKeyEntry::count(),iter_duplicates(),check_and_into_unique()onMultiMap/HashIndexfor duplicate detection via existing index infra. Addfrom_non_unique()/into_non_unique()for lossless conversion between unique and non-unique index types. AddTypedIndex::make_unique()/make_non_unique()/iter_duplicates()viadefine_uniqueness_conversions!macro covering all 36 variant pairs.take_pointer_map()/restore_pointer_map()/has_unique_index()for pointer map lifecycle management.create_constraint→create_st_constraint(metadata-only, used bycreate_table). Newcreate_constraintcallscreate_st_constraintthen makes index unique on both tx and commit tables withcan_mergecheck. Same pattern fordrop_constraint/drop_st_constraint. EnrichPendingSchemaChange::ConstraintAddedwithIndexIdandPointerMapfor correct rollback.CheckAddUniqueConstraintValidprecheck — duplicate detection now happens insidecreate_constraintusing the index directly. Remove deadAddUniqueConstrainterror variant.Test plan
cargo test -p spacetimedb-table— 99 tests passcargo test -p spacetimedb-datastore --features test— 89 tests pass (6 new)cargo test -p spacetimedb-schema -- auto_migrate— 12 tests passcargo build -p spacetimedb-core— compiles clean🤖 Generated with Claude Code