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(pg): support CTE (WITH clause) in prior backup statement generation (#19326)
rebelice99ec962dffc2
diff --git a/backend/plugin/parser/pg/backup.go b/backend/plugin/parser/pg/backup.goindex 19b152cc84..e7f6c4f255 100644--- a/backend/plugin/parser/pg/backup.go+++ b/backend/plugin/parser/pg/backup.go@@ -135,6 +135,14 @@ func generateSQLForTable(statementInfoList []statementInfo, targetSchema string,return nil, errors.Wrap(err, "failed to write to buffer")}+ // In PostgreSQL, WITH clause must appear once before the first SELECT.+ cteClause := extractCTE(statementInfoList[0].tree)+ if cteClause != "" {+ if _, err := fmt.Fprintf(&buf, "%s\n", cteClause); err != nil {+ return nil, errors.Wrap(err, "failed to write to buffer")+ }+ }+for i, item := range statementInfoList {if i != 0 {if _, err := buf.WriteString("\n UNION\n"); err != nil {@@ -177,6 +185,24 @@ func generateSQLForTable(statementInfoList []statementInfo, targetSchema string,}, nil}+func extractCTE(ctx antlr.ParserRuleContext) string {+ switch node := ctx.(type) {+ case *parser.UpdatestmtContext:+ if optWith := node.Opt_with_clause(); optWith != nil {+ if withClause := optWith.With_clause(); withClause != nil {+ return node.GetParser().GetTokenStream().GetTextFromRuleContext(withClause)+ }+ }+ case *parser.DeletestmtContext:+ if optWith := node.Opt_with_clause(); optWith != nil {+ if withClause := optWith.With_clause(); withClause != nil {+ return node.GetParser().GetTokenStream().GetTextFromRuleContext(withClause)+ }+ }+ }+ return ""+}+func writeSuffixSelectClause(buf *strings.Builder, tree antlr.Tree) error {extractor := &suffixSelectClauseExtractor{buf: buf,diff --git a/backend/plugin/parser/pg/test-data/test_backup.yaml b/backend/plugin/parser/pg/test-data/test_backup.yamlindex 9b8a643812..e81bb6bac6 100644--- a/backend/plugin/parser/pg/test-data/test_backup.yaml+++ b/backend/plugin/parser/pg/test-data/test_backup.yaml@@ -174,3 +174,48 @@endposition:line: 3column: 20+- input: WITH cte AS (SELECT a FROM test2) UPDATE test SET c = 1 FROM cte WHERE test.a = cte.a;+ result:+ - statement: |-+ CREATE TABLE "backupSchema"."rollback_test_public" AS+ WITH cte AS (SELECT a FROM test2)+ SELECT "public"."test".* FROM test, cte WHERE test.a = cte.a;+ sourceschema: public+ sourcetablename: test+ targettablename: rollback_test_public+ startposition:+ line: 1+ column: 0+ endposition:+ line: 1+ column: 86+- input: WITH cte AS (SELECT a FROM test2) DELETE FROM test USING cte WHERE test.a = cte.a;+ result:+ - statement: |-+ CREATE TABLE "backupSchema"."rollback_test_public" AS+ WITH cte AS (SELECT a FROM test2)+ SELECT "public"."test".* FROM test, cte WHERE test.a = cte.a;+ sourceschema: public+ sourcetablename: test+ targettablename: rollback_test_public+ startposition:+ line: 1+ column: 0+ endposition:+ line: 1+ column: 82+- input: WITH cte AS (SELECT a FROM test2) UPDATE test ta SET b = 1 FROM cte WHERE cte.a = ta.a;+ result:+ - statement: |-+ CREATE TABLE "backupSchema"."rollback_test_public" AS+ WITH cte AS (SELECT a FROM test2)+ SELECT "ta".* FROM test ta, cte WHERE cte.a = ta.a;+ sourceschema: public+ sourcetablename: test+ targettablename: rollback_test_public+ startposition:+ line: 1+ column: 0+ endposition:+ line: 1+ column: 87