feat: Allow adding unique constraints to existing tables#4465
Open
Ludv1gL wants to merge 1 commit intoclockworklabs:masterfrom
Open
feat: Allow adding unique constraints to existing tables#4465Ludv1gL wants to merge 1 commit intoclockworklabs:masterfrom
Ludv1gL wants to merge 1 commit intoclockworklabs:masterfrom
Conversation
…lidation precheck Instead of hard-rejecting #[unique] or #[primary_key] on existing columns, scan for duplicates first. If data is clean, allow the constraint. If duplicates exist, return a detailed error showing which values clash. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
|
Interesting, thank you for filing this! We'll work on getting it reviewed. |
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
Adding
#[unique]or#[primary_key]to an existing column currently triggersAutoMigrateError::AddUniqueConstraint, forcing a full database clear to apply the schema change. This PR makes it a non-breaking migration by validating existing data first:Changes
auto_migrate.rs: Replace hardAddUniqueConstrainterror withCheckAddUniqueConstraintValidprecheck +AddConstraintmigration stepupdate.rs: Implement precheck (full table scan, project constrained columns, count duplicates) andAddConstraintstep executionrelational_db.rs: Exposecreate_constraint()(counterpart to existingdrop_constraint())traits.rs/datastore.rs: Addcreate_constraint_mut_txtoMutTxDatastoretraitmut_tx.rs: Makecreate_constraintpublicformatter.rs: Format the newAddConstraintstepSafety
MutTx— no window for concurrent duplicate insertsauto_migrate_indexes()already handles adding the backing btree index (withis_unique=truefrom the new schema). The constraint step only adds metadata.Example error output
Test plan
auto_migratetests passcargo checkpasses forspacetimedb-schemaandspacetimedb-coreAddUniqueConstrainterror test is updated#[unique]to existing column with clean data → succeeds#[unique]to existing column with duplicates → fails with detailed error🤖 Generated with Claude Code