Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: stop treating DROP VIEW as DROP TABLE for risk and compatibility checks (#19266)
Vincent Huangc633fadf2bad
diff --git a/backend/plugin/advisor/pg/advisor_migration_compatibility.go b/backend/plugin/advisor/pg/advisor_migration_compatibility.goindex 600b2bf070..ca76ee56c2 100644--- a/backend/plugin/advisor/pg/advisor_migration_compatibility.go+++ b/backend/plugin/advisor/pg/advisor_migration_compatibility.go@@ -140,7 +140,7 @@ func (r *compatibilityRule) handleDropdbstmt(ctx antlr.ParserRuleContext) {})}-// handleDropstmt handles DROP TABLE/VIEW+// handleDropstmt handles DROP TABLE/MATERIALIZED VIEWfunc (r *compatibilityRule) handleDropstmt(ctx antlr.ParserRuleContext) {dropstmtCtx, ok := ctx.(*parser.DropstmtContext)if !ok {@@ -151,21 +151,25 @@ func (r *compatibilityRule) handleDropstmt(ctx antlr.ParserRuleContext) {return}- // Check if this is DROP TABLE or DROP VIEW- if dropstmtCtx.Object_type_any_name() != nil {- objType := dropstmtCtx.Object_type_any_name()- if objType.TABLE() != nil || objType.VIEW() != nil {- r.AddAdvice(&storepb.Advice{- Status: r.level,- Code: advisorcode.CompatibilityDropTable.Int32(),- Title: r.title,- Content: fmt.Sprintf(`"%s" may cause incompatibility with the existing data and code`, getTextFromTokens(r.tokens, dropstmtCtx)),- StartPosition: &storepb.Position{- Line: int32(dropstmtCtx.GetStart().GetLine()),- Column: 0,- },- })- }+ if dropstmtCtx.Object_type_any_name() == nil {+ return+ }+ objType := dropstmtCtx.Object_type_any_name()++ // Flag DROP TABLE and DROP MATERIALIZED VIEW (data loss risk).+ // Skip non-materialized DROP VIEW — views hold no data.+ isMaterializedView := objType.VIEW() != nil && objType.MATERIALIZED() != nil+ if objType.TABLE() != nil || isMaterializedView {+ r.AddAdvice(&storepb.Advice{+ Status: r.level,+ Code: advisorcode.CompatibilityDropTable.Int32(),+ Title: r.title,+ Content: fmt.Sprintf(`"%s" may cause incompatibility with the existing data and code`, getTextFromTokens(r.tokens, dropstmtCtx)),+ StartPosition: &storepb.Position{+ Line: int32(dropstmtCtx.GetStart().GetLine()),+ Column: 0,+ },+ })}}diff --git a/backend/plugin/advisor/pg/test/schema_backward_compatibility.yaml b/backend/plugin/advisor/pg/test/schema_backward_compatibility.yamlindex 2fa6d4a2db..49fba69574 100644--- a/backend/plugin/advisor/pg/test/schema_backward_compatibility.yaml+++ b/backend/plugin/advisor/pg/test/schema_backward_compatibility.yaml@@ -9,3 +9,31 @@line: 1column: 0endposition: null+- statement: DROP VIEW my_view+ want:+- statement: DROP VIEW IF EXISTS my_view+ want:+- statement: DROP VIEW public.my_view+ want:+- statement: DROP VIEW my_view; CREATE OR REPLACE VIEW my_view AS SELECT 1+ want:+- statement: DROP TABLE tech_book+ want:+ - status: 2+ code: 103+ title: SCHEMA_BACKWARD_COMPATIBILITY+ content: '"DROP TABLE tech_book" may cause incompatibility with the existing data and code'+ startposition:+ line: 1+ column: 0+ endposition: null+- statement: DROP MATERIALIZED VIEW my_mat_view+ want:+ - status: 2+ code: 103+ title: SCHEMA_BACKWARD_COMPATIBILITY+ content: '"DROP MATERIALIZED VIEW my_mat_view" may cause incompatibility with the existing data and code'+ startposition:+ line: 1+ column: 0+ endposition: nulldiff --git a/backend/plugin/parser/pg/statement_type_antlr.go b/backend/plugin/parser/pg/statement_type_antlr.goindex 28335a7326..2d8684c2c9 100644--- a/backend/plugin/parser/pg/statement_type_antlr.go+++ b/backend/plugin/parser/pg/statement_type_antlr.go@@ -272,11 +272,11 @@ func getDropStatementType(ctx *parser.DropstmtContext) storepb.StatementType {return storepb.StatementType_DROP_TABLE}if objType.VIEW() != nil {- // Legacy compatibility: PostgreSQL treats both regular views and materialized views- // as DROP_TABLE instead of DROP_VIEW. This matches the behavior of the legacy- // pg_query_go parser to maintain consistency in statement type reporting.- // Note: Redshift follows the same pattern.- return storepb.StatementType_DROP_TABLE+ if objType.MATERIALIZED() != nil {+ // DROP MATERIALIZED VIEW holds data — treat as DROP_TABLE for risk assessment.+ return storepb.StatementType_DROP_TABLE+ }+ return storepb.StatementType_DROP_VIEW}if objType.INDEX() != nil {return storepb.StatementType_DROP_INDEXdiff --git a/backend/plugin/parser/pg/test-data/test_statement_type.yaml b/backend/plugin/parser/pg/test-data/test_statement_type.yamlindex 3b0249e947..c24056b5f0 100644--- a/backend/plugin/parser/pg/test-data/test_statement_type.yaml+++ b/backend/plugin/parser/pg/test-data/test_statement_type.yaml@@ -42,7 +42,7 @@- DROP_TRIGGER- statement: DROP VIEW v1;want:- - DROP_TABLE+ - DROP_VIEW- statement: DROP FUNCTION func1();want:- DROP_FUNCTION@@ -123,3 +123,6 @@- statement: ALTER VIEW v_old RENAME TO v_new;want:- ALTER_VIEW+- statement: DROP MATERIALIZED VIEW mv1;+ want:+ - DROP_TABLEdiff --git a/backend/plugin/parser/redshift/statement_type.go b/backend/plugin/parser/redshift/statement_type.goindex 0997565ad8..5f15dfa004 100644--- a/backend/plugin/parser/redshift/statement_type.go+++ b/backend/plugin/parser/redshift/statement_type.go@@ -311,10 +311,11 @@ func getDropStatementType(ctx *parser.DropstmtContext) storepb.StatementType {return storepb.StatementType_DROP_TABLE}if objType.VIEW() != nil {- // Legacy compatibility: Redshift (like PostgreSQL) treats both regular views- // and materialized views as DROP_TABLE instead of DROP_VIEW to maintain- // consistency with the legacy parser behavior.- return storepb.StatementType_DROP_TABLE+ if objType.MATERIALIZED() != nil {+ // DROP MATERIALIZED VIEW holds data — treat as DROP_TABLE for risk assessment.+ return storepb.StatementType_DROP_TABLE+ }+ return storepb.StatementType_DROP_VIEW}if objType.INDEX() != nil {return storepb.StatementType_DROP_INDEX