fix(postgresql): DROP TABLE ... CASCADE drops dependent views (#4416)#4456
Open
luongs3 wants to merge 1 commit into
Open
fix(postgresql): DROP TABLE ... CASCADE drops dependent views (#4416)#4456luongs3 wants to merge 1 commit into
luongs3 wants to merge 1 commit into
Conversation
…ev#4416) sqlc's in-memory catalog dropped the table but left views that depended on it, so a later CREATE VIEW reusing the name failed with 'relation "..." already exists' even though the SQL is valid Postgres. - ast: carry DROP behavior (RESTRICT/CASCADE) into DropTableStmt; add named DropBehavior constants matching the pg_query enum. - postgresql parser: thread n.Behavior into the DropTableStmt. - catalog: record each view's referenced tables (DependsOn) at create time, and on DROP TABLE ... CASCADE evict dependent views transitively. - RESTRICT / unspecified behavior is unchanged (views are kept). - tests: catalog unit tests covering cascade eviction (incl. views on views) and the restrict/default keep-path. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Fixes #4416.
Problem
When a Postgres schema does
DROP TABLE foo CASCADEwhere a view depends onfoo, sqlc removes the table from its in-memory catalog but leaves the dependent view behind. A laterCREATE VIEW(orCREATE TABLE) reusing that name then fails with a spuriousrelation "..." already exists, even though the SQL is valid and runs fine on real Postgres (whereCASCADEdrops the dependent view).Reproduction
sqlc generate→relation "vw_reference_rates" already exists(exit 1)Fix
internal/sql/ast: carry theDROPbehavior (RESTRICT/CASCADE) intoDropTableStmt, and add namedDropBehaviorconstants matching thepg_queryenum (UNDEFINED=0,RESTRICT=1,CASCADE=2).internal/engine/postgresql/parse.go: threadn.Behaviorinto the constructedDropTableStmt.internal/sql/catalog: record each view's referenced tables (DependsOn) atcreateViewtime by walking the view query for*ast.RangeVarnodes; onDROP TABLE ... CASCADE, evict dependent views transitively (a view may depend on another view).RESTRICTand unspecified behavior are unchanged — dependent views are kept, preserving today's behavior.Tests
Added catalog unit tests covering:
DROP TABLE ... CASCADEevicts a dependent view, including views-on-views (transitive).DROP TABLE ... RESTRICTand bareDROP TABLEkeep the dependent view and preserve its recorded dependency.go test ./internal/sql/catalog/... ./internal/engine/postgresql/...passes;gofmt/go vetclean.