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: deparse postgresql drop sequence statement (#3499)
h3n4l04e64d0d45d8
diff --git a/plugin/parser/ast/drop_sequence_stmt.go b/plugin/parser/ast/drop_sequence_stmt.gonew file mode 100644index 0000000000..4eccb8f68c--- /dev/null+++ b/plugin/parser/ast/drop_sequence_stmt.go@@ -0,0 +1,10 @@+package ast++// DropSequenceStmt is the struct for drop sequence statement.+type DropSequenceStmt struct {+ ddl++ IfExists bool+ SequenceNameList []SequenceNameDef+ Behavior DropBehavior+}diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex 58080dc6e7..beb8cb207c 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -318,6 +318,23 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, errdropSchema.SchemaList = append(dropSchema.SchemaList, strNode.String_.Str)}return dropSchema, nil+ case pgquery.ObjectType_OBJECT_SEQUENCE:+ dropSequence := &ast.DropSequenceStmt{+ IfExists: in.DropStmt.MissingOk,+ Behavior: convertDropBehavior(in.DropStmt.Behavior),+ }+ for _, sequence := range in.DropStmt.Objects {+ list, ok := sequence.Node.(*pgquery.Node_List)+ if !ok {+ return nil, parser.NewConvertErrorf("expected List but found %t", sequence.Node)+ }+ sequenceDef, err := convertListToSequenceNameDef(list)+ if err != nil {+ return nil, err+ }+ dropSequence.SequenceNameList = append(dropSequence.SequenceNameList, sequenceDef)+ }+ return dropSequence, nil}case *pgquery.Node_DropdbStmt:return &ast.DropDatabaseStmt{@@ -414,7 +431,6 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, errreturn nil, parser.NewConvertErrorf("unsupported option %s", defElemNode.DefElem.Defname)}}-return createSeqStmt, nilcase *pgquery.Node_AlterObjectSchemaStmt:switch in.AlterObjectSchemaStmt.ObjectType {@@ -833,6 +849,26 @@ func convertSetOperation(t pgquery.SetOperation) (ast.SetOperationType, error) {}}+func convertListToSequenceNameDef(in *pgquery.Node_List) (ast.SequenceNameDef, error) {+ stringList, err := convertListToStringList(in)+ if err != nil {+ return ast.SequenceNameDef{}, err+ }+ switch len(in.List.Items) {+ case 2:+ return ast.SequenceNameDef{+ Schema: stringList[0],+ Name: stringList[1],+ }, nil+ case 1:+ return ast.SequenceNameDef{+ Name: stringList[0],+ }, nil+ default:+ return ast.SequenceNameDef{}, parser.NewConvertErrorf("expected length is 1 or 2, but found %d", len(in.List.Items))+ }+}+func convertListToTableDef(in *pgquery.Node_List, tableType ast.TableType) (*ast.TableDef, error) {stringList, err := convertListToStringList(in)if err != nil {diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex e541d9312e..9b976a3259 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -2481,3 +2481,54 @@ func TestCreateSequence(t *testing.T) {}runTests(t, tests)}++func TestDropSequence(t *testing.T) {+ tests := []testData{+ {+ stmt: `DROP SEQUENCE public.tbl_seq_id_seq;`,+ want: []ast.Node{+ &ast.DropSequenceStmt{+ IfExists: false,+ Behavior: ast.DropBehaviorRestrict,+ SequenceNameList: []ast.SequenceNameDef{+ {+ Schema: "public",+ Name: "tbl_seq_id_seq",+ },+ },+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: `DROP SEQUENCE public.tbl_seq_id_seq;`,+ LastLine: 1,+ },+ },+ },+ {+ stmt: `DROP SEQUENCE IF EXISTS tbl_seq1, public.tbl_seq2 CASCADE;`,+ want: []ast.Node{+ &ast.DropSequenceStmt{+ IfExists: true,+ Behavior: ast.DropBehaviorCascade,+ SequenceNameList: []ast.SequenceNameDef{+ {+ Name: "tbl_seq1",+ },+ {+ Schema: "public",+ Name: "tbl_seq2",+ },+ },+ },+ },+ statementList: []parser.SingleSQL{+ {+ Text: `DROP SEQUENCE IF EXISTS tbl_seq1, public.tbl_seq2 CASCADE;`,+ LastLine: 1,+ },+ },+ },+ }+ runTests(t, tests)+}