fix: handle DROP TABLE CASCADE for dependent views in schema replay#4419
Open
wucm667 wants to merge 1 commit intosqlc-dev:mainfrom
Open
fix: handle DROP TABLE CASCADE for dependent views in schema replay#4419wucm667 wants to merge 1 commit intosqlc-dev:mainfrom
wucm667 wants to merge 1 commit intosqlc-dev:mainfrom
Conversation
Signed-off-by: wucm667 <stevenwucongmin@gmail.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.
Problem
When
sqlc generatereplays a PostgreSQL schema containingDROP TABLE ... CASCADE, dependent views remain in the internal catalog. This causes subsequentCREATE VIEWstatements with the same name to fail withrelation "vw_reference_rates" already exists.Minimal repro (Playground):
In real Postgres,
DROP TABLE ... CASCADEdrops both the table and dependent views. In sqlc's replay, the view persists.Root Cause
ast.DropTableStmtlacked aBehaviorfield, so the parser never captured whetherCASCADEorRESTRICTwas specified.dropTable()in the catalog only removed the table and linked types — it never checked for or removed dependent views.Fix
1. Capture CASCADE/RESTRICT in the AST (
internal/sql/ast/drop_table_stmt.go)Added
Behavior DropBehaviorfield.2. Parser extracts Behavior (
internal/engine/postgresql/parse.go)When converting
DropStmtforOBJECT_TABLE,OBJECT_VIEW, orOBJECT_MATVIEW, pass throughn.Behavior.3. Track view dependencies (
internal/sql/catalog/view.go)Added
DependsOnTables []*ast.TableNametoTablestruct. ThecreateViewfunction now walks the SELECT query AST to extract allRangeVar(table reference) nodes, populating dependencies at view creation time.4. CASCADE drops dependent views (
internal/sql/catalog/table.go)When
Behavior == 2(DROP_CASCADEin pg_query_go protobuf),dropTablescans all tables/views in the schema and removes any that depend on the dropped table.5. Tests (
internal/engine/postgresql/catalog_test.go)TestDropTableCascadeViewRecreate: verifies CASCADE removes dependent views.TestDropTableCascadeWithoutCascadeFails: verifies without CASCADE, views remain in catalog.Testing
All existing
TestUpdateErrorscases pass; new CASCADE tests pass.Fixes #4416