Skip to content

fix: handle DROP TABLE CASCADE for dependent views in schema replay#4419

Open
wucm667 wants to merge 1 commit intosqlc-dev:mainfrom
wucm667:fix/drop-table-cascade-view-replay-4416
Open

fix: handle DROP TABLE CASCADE for dependent views in schema replay#4419
wucm667 wants to merge 1 commit intosqlc-dev:mainfrom
wucm667:fix/drop-table-cascade-view-replay-4416

Conversation

@wucm667
Copy link
Copy Markdown

@wucm667 wucm667 commented Apr 29, 2026

Problem

When sqlc generate replays a PostgreSQL schema containing DROP TABLE ... CASCADE, dependent views remain in the internal catalog. This causes subsequent CREATE VIEW statements with the same name to fail with relation "vw_reference_rates" already exists.

Minimal repro (Playground):

CREATE TABLE reference_rates (id BIGSERIAL PRIMARY KEY);
CREATE VIEW vw_reference_rates AS SELECT * FROM reference_rates;
DROP TABLE reference_rates CASCADE;
ALTER TABLE reference_rates_new RENAME TO reference_rates;
CREATE VIEW vw_reference_rates AS SELECT * FROM reference_rates;  -- fails

In real Postgres, DROP TABLE ... CASCADE drops both the table and dependent views. In sqlc's replay, the view persists.

Root Cause

  1. ast.DropTableStmt lacked a Behavior field, so the parser never captured whether CASCADE or RESTRICT was specified.
  2. dropTable() in the catalog only removed the table and linked types — it never checked for or removed dependent views.
  3. Views stored in the catalog had no dependency tracking.

Fix

1. Capture CASCADE/RESTRICT in the AST (internal/sql/ast/drop_table_stmt.go)

Added Behavior DropBehavior field.

2. Parser extracts Behavior (internal/engine/postgresql/parse.go)

When converting DropStmt for OBJECT_TABLE, OBJECT_VIEW, or OBJECT_MATVIEW, pass through n.Behavior.

3. Track view dependencies (internal/sql/catalog/view.go)

Added DependsOnTables []*ast.TableName to Table struct. The createView function now walks the SELECT query AST to extract all RangeVar (table reference) nodes, populating dependencies at view creation time.

4. CASCADE drops dependent views (internal/sql/catalog/table.go)

When Behavior == 2 (DROP_CASCADE in pg_query_go protobuf), dropTable scans 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

go test -v -count=1 ./internal/engine/postgresql/... -run "TestDropTableCascade|TestUpdateErrors"

All existing TestUpdateErrors cases pass; new CASCADE tests pass.

Fixes #4416

Signed-off-by: wucm667 <stevenwucongmin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL schema parser does not drop dependent views for DROP TABLE ... CASCADE

1 participant