Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: support deparse drop table statements (#3215)

rebelice407164492a9c
diff --git a/plugin/parser/engine/pg/deparse.go b/plugin/parser/engine/pg/deparse.go
index 6b0a5f7f8c..a4b0f325e4 100644
--- a/plugin/parser/engine/pg/deparse.go
+++ b/plugin/parser/engine/pg/deparse.go
@@ -20,6 +20,11 @@ func deparse(context parser.DeparseContext, in ast.Node, buf *strings.Builder) e
return err
}
return buf.WriteByte(';')
+ case *ast.DropTableStmt:
+ if err := deparseDropTable(context, node, buf); err != nil {
+ return err
+ }
+ return buf.WriteByte(';')
case *ast.AlterTableStmt:
if err := deparseAlterTable(context, node, buf); err != nil {
return err
@@ -43,6 +48,27 @@ func deparse(context parser.DeparseContext, in ast.Node, buf *strings.Builder) e
return errors.Errorf("failed to deparse %T", in)
}
+func deparseDropTable(context parser.DeparseContext, in *ast.DropTableStmt, buf *strings.Builder) error {
+ if err := context.WriteIndent(buf, parser.DeparseIndentString); err != nil {
+ return err
+ }
+
+ if _, err := buf.WriteString("DROP TABLE "); err != nil {
+ return err
+ }
+ for i, table := range in.TableList {
+ if i != 0 {
+ if _, err := buf.WriteString(", "); err != nil {
+ return err
+ }
+ }
+ if err := deparseTableDef(parser.DeparseContext{IndentLevel: 0}, table, buf); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
func deparseAlterTable(context parser.DeparseContext, in *ast.AlterTableStmt, buf *strings.Builder) error {
if _, err := buf.WriteString("ALTER TABLE "); err != nil {
return err
diff --git a/plugin/parser/engine/pg/deparse_test.go b/plugin/parser/engine/pg/deparse_test.go
index 5bb922a8fc..0d944602ec 100644
--- a/plugin/parser/engine/pg/deparse_test.go
+++ b/plugin/parser/engine/pg/deparse_test.go
@@ -57,8 +57,10 @@ func runDeparseTest(t *testing.T, file string, record bool) {
func TestDeparse(t *testing.T) {
testFileList := []string{
+ // Table
"test_create_table_data.yaml",
"test_alter_table_data.yaml",
+ "test_drop_table_data.yaml",
// Schema
"test_create_schema_data.yaml",
"test_drop_schema_data.yaml",
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.yaml
new file mode 100644
index 0000000000..b179821f28
--- /dev/null
+++ b/plugin/parser/engine/pg/test-data/test_drop_table_data.yaml
@@ -0,0 +1,4 @@
+- stmt: drop table t
+ want: DROP TABLE "t";
+- stmt: drop table public.t, t1
+ want: DROP TABLE "public"."t", "t1";