Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore(parser): add drop behavior for drop table statements in parser/deparser/differ (#3338)
rebeliceb05a41e5df4e
diff --git a/plugin/parser/ast/drop_schema_stmt.go b/plugin/parser/ast/drop_schema_stmt.goindex 29f636aa44..87d8db7a22 100644--- a/plugin/parser/ast/drop_schema_stmt.go+++ b/plugin/parser/ast/drop_schema_stmt.go@@ -1,15 +1,15 @@package ast-// DropSchemaBehavior is the type for drop schema statement.-type DropSchemaBehavior int+// DropBehavior is the type for drop object statements.+type DropBehavior intconst (- // DropSchemaBehaviorNone is the default type for drop schema statement.- DropSchemaBehaviorNone DropSchemaBehavior = iota- // DropSchemaBehaviorCascade is the type for drop schema statement with cascade.- DropSchemaBehaviorCascade- // DropSchemaBehaviorRestrict is the type for drop schema statement with restrict.- DropSchemaBehaviorRestrict+ // DropBehaviorNone is the default type for drop object statements.+ DropBehaviorNone DropBehavior = iota+ // DropBehaviorCascade is the type for drop object statements with cascade.+ DropBehaviorCascade+ // DropBehaviorRestrict is the type for drop object statement with restrict.+ DropBehaviorRestrict)// DropSchemaStmt is the struct for drop schema statement.@@ -18,5 +18,5 @@ type DropSchemaStmt struct {IfExists boolSchemaList []string- Behavior DropSchemaBehavior+ Behavior DropBehavior}diff --git a/plugin/parser/ast/drop_table_stmt.go b/plugin/parser/ast/drop_table_stmt.goindex 2b9aa99f6a..c977245b56 100644--- a/plugin/parser/ast/drop_table_stmt.go+++ b/plugin/parser/ast/drop_table_stmt.go@@ -4,5 +4,7 @@ package asttype DropTableStmt struct {ddl+ IfExists bool+ Behavior DropBehaviorTableList []*TableDef}diff --git a/plugin/parser/differ/pg/differ.go b/plugin/parser/differ/pg/differ.goindex 5749be0b2a..8376afea35 100644--- a/plugin/parser/differ/pg/differ.go+++ b/plugin/parser/differ/pg/differ.go@@ -416,8 +416,9 @@ func dropTable(m schemaMap) *ast.DropTableStmt {tableDefList = append(tableDefList, table.createTable.Name)}return &ast.DropTableStmt{+ IfExists: true,TableList: tableDefList,- // TODO(rebelice): add CASCADE here.+ Behavior: ast.DropBehaviorCascade,}}@@ -443,7 +444,7 @@ func dropSchema(m schemaMap) *ast.DropSchemaStmt {return &ast.DropSchemaStmt{IfExists: true,SchemaList: schemaNameList,- Behavior: ast.DropSchemaBehaviorCascade,+ Behavior: ast.DropBehaviorCascade,}}diff --git a/plugin/parser/differ/pg/test-data/test_differ_data.yaml b/plugin/parser/differ/pg/test-data/test_differ_data.yamlindex 854b248f84..e452df7208 100644--- a/plugin/parser/differ/pg/test-data/test_differ_data.yaml+++ b/plugin/parser/differ/pg/test-data/test_differ_data.yaml@@ -69,7 +69,7 @@create table s2.t2(a int);ALTER TABLE "s1"."t2"DROP COLUMN "b";- DROP TABLE "s1"."t1", "s2"."t4";+ DROP TABLE IF EXISTS "s1"."t1", "s2"."t4" CASCADE;- oldSchema: |create table public.t1(a int,diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex c259c70de9..0cf1f9b0a7 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -265,7 +265,10 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err}return dropIndex, nilcase pgquery.ObjectType_OBJECT_TABLE:- dropTable := &ast.DropTableStmt{}+ dropTable := &ast.DropTableStmt{+ IfExists: in.DropStmt.MissingOk,+ Behavior: convertDropBehavior(in.DropStmt.Behavior),+ }for _, object := range in.DropStmt.Objects {list, ok := object.Node.(*pgquery.Node_List)if !ok {@@ -279,7 +282,10 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err}return dropTable, nilcase pgquery.ObjectType_OBJECT_VIEW:- dropView := &ast.DropTableStmt{}+ dropView := &ast.DropTableStmt{+ IfExists: in.DropStmt.MissingOk,+ Behavior: convertDropBehavior(in.DropStmt.Behavior),+ }for _, object := range in.DropStmt.Objects {list, ok := object.Node.(*pgquery.Node_List)if !ok {@@ -295,14 +301,7 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, errcase pgquery.ObjectType_OBJECT_SCHEMA:dropSchema := &ast.DropSchemaStmt{IfExists: in.DropStmt.MissingOk,- }- switch in.DropStmt.Behavior {- case pgquery.DropBehavior_DROP_CASCADE:- dropSchema.Behavior = ast.DropSchemaBehaviorCascade- case pgquery.DropBehavior_DROP_RESTRICT:- dropSchema.Behavior = ast.DropSchemaBehaviorRestrict- default:- dropSchema.Behavior = ast.DropSchemaBehaviorNone+ Behavior: convertDropBehavior(in.DropStmt.Behavior),}for _, object := range in.DropStmt.Objects {strNode, ok := object.Node.(*pgquery.Node_String_)@@ -487,6 +486,17 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, errreturn nil, nil}+func convertDropBehavior(behavior pgquery.DropBehavior) ast.DropBehavior {+ switch behavior {+ case pgquery.DropBehavior_DROP_CASCADE:+ return ast.DropBehaviorCascade+ case pgquery.DropBehavior_DROP_RESTRICT:+ return ast.DropBehaviorRestrict+ default:+ return ast.DropBehaviorNone+ }+}+func convertRoleSpec(in *pgquery.RoleSpec) (*ast.RoleSpec, error) {if in == nil {return nil, nildiff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex 8cdc678835..c3cf5d356a 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -1171,9 +1171,11 @@ func TestPGDropColumnStmt(t *testing.T) {func TestPGDropTableStmt(t *testing.T) {tests := []testData{{- stmt: "DROP TABLE tech_book, xschema.user",+ stmt: "DROP TABLE IF EXISTS tech_book, xschema.user CASCADE",want: []ast.Node{&ast.DropTableStmt{+ IfExists: true,+ Behavior: ast.DropBehaviorCascade,TableList: []*ast.TableDef{{Type: ast.TableTypeBaseTable,@@ -1189,15 +1191,16 @@ func TestPGDropTableStmt(t *testing.T) {},statementList: []parser.SingleSQL{{- Text: "DROP TABLE tech_book, xschema.user",+ Text: "DROP TABLE IF EXISTS tech_book, xschema.user CASCADE",LastLine: 1,},},},{- stmt: "DROP VIEW tech_book, xschema.user",+ stmt: "DROP VIEW tech_book, xschema.user RESTRICT",want: []ast.Node{&ast.DropTableStmt{+ Behavior: ast.DropBehaviorRestrict,TableList: []*ast.TableDef{{Type: ast.TableTypeView,@@ -1213,7 +1216,7 @@ func TestPGDropTableStmt(t *testing.T) {},statementList: []parser.SingleSQL{{- Text: "DROP VIEW tech_book, xschema.user",+ Text: "DROP VIEW tech_book, xschema.user RESTRICT",LastLine: 1,},},@@ -2014,7 +2017,7 @@ func TestDropSchema(t *testing.T) {&ast.DropSchemaStmt{IfExists: false,SchemaList: []string{"s1"},- Behavior: ast.DropSchemaBehaviorRestrict,+ Behavior: ast.DropBehaviorRestrict,},},statementList: []parser.SingleSQL{@@ -2030,7 +2033,7 @@ func TestDropSchema(t *testing.T) {&ast.DropSchemaStmt{IfExists: false,SchemaList: []string{"s1", "s2"},- Behavior: ast.DropSchemaBehaviorCascade,+ Behavior: ast.DropBehaviorCascade,},},statementList: []parser.SingleSQL{@@ -2046,7 +2049,7 @@ func TestDropSchema(t *testing.T) {&ast.DropSchemaStmt{IfExists: true,SchemaList: []string{"s1", "s2"},- Behavior: ast.DropSchemaBehaviorRestrict,+ Behavior: ast.DropBehaviorRestrict,},},statementList: []parser.SingleSQL{diff --git a/plugin/parser/engine/pg/deparse.go b/plugin/parser/engine/pg/deparse.goindex 109f65ff9a..0c89a7ce78 100644--- a/plugin/parser/engine/pg/deparse.go+++ b/plugin/parser/engine/pg/deparse.go@@ -188,6 +188,11 @@ func deparseDropTable(context parser.DeparseContext, in *ast.DropTableStmt, bufif _, err := buf.WriteString("DROP TABLE "); err != nil {return err}+ if in.IfExists {+ if _, err := buf.WriteString("IF EXISTS "); err != nil {+ return err+ }+ }for i, table := range in.TableList {if i != 0 {if _, err := buf.WriteString(", "); err != nil {@@ -198,7 +203,7 @@ func deparseDropTable(context parser.DeparseContext, in *ast.DropTableStmt, bufreturn err}}- return nil+ return deparseDropBehavior(context, in.Behavior, buf)}func deparseAlterTable(context parser.DeparseContext, in *ast.AlterTableStmt, buf *strings.Builder) error {@@ -802,15 +807,14 @@ func deparseDropSchema(_ parser.DeparseContext, in *ast.DropSchemaStmt, buf *strreturn err}}- switch in.Behavior {- case ast.DropSchemaBehaviorCascade:+ return deparseDropBehavior(parser.DeparseContext{}, in.Behavior, buf)+}++func deparseDropBehavior(_ parser.DeparseContext, behavior ast.DropBehavior, buf *strings.Builder) error {+ if behavior == ast.DropBehaviorCascade {if _, err := buf.WriteString(" CASCADE"); err != nil {return err}- case ast.DropSchemaBehaviorRestrict:- if _, err := buf.WriteString(" RESTRICT"); err != nil {- return err- }}return nil}diff --git a/plugin/parser/engine/pg/test-data/test_drop_schema_data.yaml b/plugin/parser/engine/pg/test-data/test_drop_schema_data.yamlindex 10770fb73b..58acd08d3c 100644--- a/plugin/parser/engine/pg/test-data/test_drop_schema_data.yaml+++ b/plugin/parser/engine/pg/test-data/test_drop_schema_data.yaml@@ -1,4 +1,4 @@- stmt: drop schema myschema- want: DROP SCHEMA "myschema" RESTRICT;+ want: DROP SCHEMA "myschema";- stmt: drop schema if exists s1, s2 cascadewant: DROP SCHEMA IF EXISTS "s1", "s2" CASCADE;diff --git a/plugin/parser/engine/pg/test-data/test_drop_table_data.yaml b/plugin/parser/engine/pg/test-data/test_drop_table_data.yamlindex b179821f28..92c526d77a 100644--- a/plugin/parser/engine/pg/test-data/test_drop_table_data.yaml+++ b/plugin/parser/engine/pg/test-data/test_drop_table_data.yaml@@ -2,3 +2,7 @@want: DROP TABLE "t";- stmt: drop table public.t, t1want: DROP TABLE "public"."t", "t1";+- stmt: drop table if exists public.t cascade;+ want: DROP TABLE IF EXISTS "public"."t" CASCADE;+- stmt: drop table public.t restrict+ want: DROP TABLE "public"."t";