Preserve CHECK constraints when transforming a table#766
Open
apoorva-01 wants to merge 1 commit into
Open
Conversation
.transform() rebuilds from PRAGMA metadata, which doesn't expose CHECK constraints, so they were silently dropped. Pull them from the schema and re-add them to the rebuilt table.
7a46fa5 to
265f850
Compare
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.
.transform()quietly drops a table's CHECK constraints. They only live in the storedCREATE TABLESQL, and transform rebuilds from PRAGMA metadata, which doesn't expose them.This pulls the CHECK expressions out of the schema and re-adds them. A rename rewrites the columns a constraint references, and dropping a referenced column drops its constraint, so transform never emits a table that won't build. Column-level checks come back as table-level (identical in SQLite). It uses a small quote/paren-aware tokenizer, so no new dependency; the one edge it can't see is a column whose name also appears as a function call (
lengthalongsidelength(x)), left as a documented limitation.I scoped this to CHECK constraints. Comments and column-level UNIQUE are follow-ups (comment placement gets ambiguous on rename/reorder; UNIQUE is the separate auto-index case), happy to take either. Tests cover round-trip, rename, drop and parser edges and fail without the fix. Didn't touch the changelog since you compile it at release.
Closes #762
📚 Documentation preview 📚: https://sqlite-utils--766.org.readthedocs.build/en/766/