Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(parser): parse drop schema statement (#3207)
h3n4l9fa400b97538
diff --git a/plugin/parser/ast/drop_schema_stmt.go b/plugin/parser/ast/drop_schema_stmt.gonew file mode 100644index 0000000000..29f636aa44--- /dev/null+++ b/plugin/parser/ast/drop_schema_stmt.go@@ -0,0 +1,22 @@+package ast++// DropSchemaBehavior is the type for drop schema statement.+type DropSchemaBehavior int++const (+ // 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+)++// DropSchemaStmt is the struct for drop schema statement.+type DropSchemaStmt struct {+ ddl++ IfExists bool+ SchemaList []string+ Behavior DropSchemaBehavior+}diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex df8472f5cd..86f76dbb98 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -247,6 +247,26 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, errdropView.TableList = append(dropView.TableList, viewDef)}return dropView, nil+ case 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+ }+ for _, object := range in.DropStmt.Objects {+ strNode, ok := object.Node.(*pgquery.Node_String_)+ if !ok {+ return nil, parser.NewConvertErrorf("expected String but found %t", object.Node)+ }+ dropSchema.SchemaList = append(dropSchema.SchemaList, strNode.String_.Str)+ }+ return dropSchema, nil}case *pgquery.Node_DropdbStmt:return &ast.DropDatabaseStmt{diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex f5e5f63b15..2e4bdc0634 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -1922,3 +1922,57 @@ func TestCreateSchema(t *testing.T) {}runTests(t, tests)}++func TestDropSchema(t *testing.T) {+ tests := []testData{+ {+ stmt: "DROP SCHEMA s1",+ want: []ast.Node{+ &ast.DropSchemaStmt{+ IfExists: false,+ SchemaList: []string{"s1"},+ Behavior: ast.DropSchemaBehaviorRestrict,+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: "DROP SCHEMA s1",+ LastLine: 1,+ },+ },+ },+ {+ stmt: "DROP SCHEMA s1, s2 CASCADE",+ want: []ast.Node{+ &ast.DropSchemaStmt{+ IfExists: false,+ SchemaList: []string{"s1", "s2"},+ Behavior: ast.DropSchemaBehaviorCascade,+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: "DROP SCHEMA s1, s2 CASCADE",+ LastLine: 1,+ },+ },+ },+ {+ stmt: "DROP SCHEMA IF EXISTS s1, s2 RESTRICT",+ want: []ast.Node{+ &ast.DropSchemaStmt{+ IfExists: true,+ SchemaList: []string{"s1", "s2"},+ Behavior: ast.DropSchemaBehaviorRestrict,+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: "DROP SCHEMA IF EXISTS s1, s2 RESTRICT",+ LastLine: 1,+ },+ },+ },+ }+ runTests(t, tests)+}diff --git a/plugin/parser/engine/pg/deparse.go b/plugin/parser/engine/pg/deparse.goindex 86d50439dd..4d43216c19 100644--- a/plugin/parser/engine/pg/deparse.go+++ b/plugin/parser/engine/pg/deparse.go@@ -23,6 +23,8 @@ func deparse(context parser.DeparseContext, in ast.Node, buf *strings.Builder) ereturn deparseColumnDef(context, node, buf)case *ast.CreateSchemaStmt:return deparseCreateSchema(context, node, buf)+ case *ast.DropSchemaStmt:+ return deparseDropSchema(context, node, buf)}return errors.Errorf("failed to deparse %T", in)@@ -323,6 +325,44 @@ func deparseRoleSpec(_ parser.DeparseContext, in *ast.RoleSpec, buf *strings.Buireturn nil}+func deparseDropSchema(_ parser.DeparseContext, in *ast.DropSchemaStmt, buf *strings.Builder) error {+ if in == nil {+ return nil+ }+ if _, err := buf.WriteString("DROP SCHEMA"); err != nil {+ return err+ }+ if in.IfExists {+ if _, err := buf.WriteString(" IF EXISTS"); err != nil {+ return err+ }+ }+ for idx, schema := range in.SchemaList {+ if idx >= 1 {+ if _, err := buf.WriteString(","); err != nil {+ return err+ }+ }+ if _, err := buf.WriteString(" "); err != nil {+ return err+ }+ if err := writeSurrounding(buf, schema, `"`); err != nil {+ return err+ }+ }+ switch in.Behavior {+ case ast.DropSchemaBehaviorCascade:+ if _, err := buf.WriteString(" CASCADE"); err != nil {+ return err+ }+ case ast.DropSchemaBehaviorRestrict:+ if _, err := buf.WriteString(" RESTRICT"); err != nil {+ return err+ }+ }+ return nil+}+func writeSurrounding(buf *strings.Builder, s string, enclosure string) error {if _, err := buf.WriteString(enclosure); err != nil {return errdiff --git a/plugin/parser/engine/pg/deparse_test.go b/plugin/parser/engine/pg/deparse_test.goindex 7fe834e198..3f3cbd7dcb 100644--- a/plugin/parser/engine/pg/deparse_test.go+++ b/plugin/parser/engine/pg/deparse_test.go@@ -58,7 +58,9 @@ func runDeparseTest(t *testing.T, file string, record bool) {func TestDeparse(t *testing.T) {testFileList := []string{"test_create_table_data.yaml",+ // Schema"test_create_schema_data.yaml",+ "test_drop_schema_data.yaml",}for _, test := range testFileList {runDeparseTest(t, test, false /* record */)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.yamlnew file mode 100644index 0000000000..27f8080133--- /dev/null+++ b/plugin/parser/engine/pg/test-data/test_drop_schema_data.yaml@@ -0,0 +1,5 @@+- stmt: drop schema myschema+ want: DROP SCHEMA "myschema" RESTRICT++- stmt: drop schema if exists s1, s2 cascade+ want: DROP SCHEMA IF EXISTS "s1", "s2" CASCADE