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: backup in one table for large scale update statements (#13456)
rebelice1576395a2b7c
diff --git a/backend/plugin/parser/pg/backup.go b/backend/plugin/parser/pg/backup.goindex 3b6d6d6d3a..184123f6ac 100644--- a/backend/plugin/parser/pg/backup.go+++ b/backend/plugin/parser/pg/backup.go@@ -19,6 +19,7 @@ import (const (maxTableNameLength = 63+ maxMixedDMLCount = 5)type TableReference struct {@@ -61,6 +62,86 @@ func TransformDMLToSelect(_ context.Context, _ base.TransformContext, statement}func generateSQL(statementInfoList []statementInfo, targetSchema string, tablePrefix string) ([]base.BackupStatement, error) {+ if len(statementInfoList) <= maxMixedDMLCount {+ return generateSQLMixedDML(statementInfoList, targetSchema, tablePrefix)+ }+ return generateSQLInOneTable(statementInfoList, targetSchema, tablePrefix)+}++func generateSQLInOneTable(statementInfoList []statementInfo, targetSchema string, tablePrefix string) ([]base.BackupStatement, error) {+ table := statementInfoList[0].table++ for _, item := range statementInfoList {+ if !equalTable(item.table, table) {+ return nil, errors.Errorf("prior backup cannot handle statements on different tables more than %d", maxMixedDMLCount)+ }+ }++ targetTable := fmt.Sprintf("%s_%s", tablePrefix, table.Table)+ targetTable, _ = common.TruncateString(targetTable, maxTableNameLength)+ var buf strings.Builder+ if _, err := fmt.Fprintf(&buf, `CREATE TABLE "%s"."%s" AS`+"\n", targetSchema, targetTable); 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 ALL\n"); err != nil {+ return nil, errors.Wrap(err, "failed to write to buffer")+ }+ }+ if table.Alias != "" {+ if _, err := fmt.Fprintf(&buf, ` SELECT "%s".* `, table.Alias); err != nil {+ return nil, errors.Wrap(err, "failed to write to buffer")+ }+ } else {+ if _, err := fmt.Fprintf(&buf, ` SELECT %s.* `, table.String()); err != nil {+ return nil, errors.Wrap(err, "failed to write to buffer")+ }+ }++ if err := writeSuffixSelectClause(&buf, item.tree); err != nil {+ return nil, errors.Wrap(err, "failed to write string with new line")+ }+ }++ if _, err := buf.WriteString(";"); err != nil {+ return nil, errors.Wrap(err, "failed to write to buffer")+ }++ return []base.BackupStatement{+ {+ Statement: buf.String(),+ SourceSchema: table.Schema,+ SourceTableName: table.Table,+ TargetTableName: targetTable,+ StartPosition: &storebp.Position{+ Line: int32(statementInfoList[0].tree.GetStart().GetLine()),+ Column: int32(statementInfoList[0].tree.GetStart().GetColumn()),+ },+ EndPosition: &storebp.Position{+ Line: int32(statementInfoList[len(statementInfoList)-1].tree.GetStop().GetLine()),+ Column: int32(statementInfoList[len(statementInfoList)-1].tree.GetStop().GetColumn()),+ },+ },+ }, nil+}++func equalTable(a, b *TableReference) bool {+ if a == nil || b == nil {+ return false+ }++ if a.Database != "" && b.Database != "" && a.Database != b.Database {+ return false+ }+ if a.Schema != "" && b.Schema != "" && a.Schema != b.Schema {+ return false+ }+ return a.Table == b.Table+}++func generateSQLMixedDML(statementInfoList []statementInfo, targetSchema string, tablePrefix string) ([]base.BackupStatement, error) {var result []base.BackupStatementoffsetLength := 1if len(statementInfoList) > 1 {diff --git a/backend/plugin/parser/pg/test-data/test_backup.yaml b/backend/plugin/parser/pg/test-data/test_backup.yamlindex 72862fe5e6..31810a03c1 100644--- a/backend/plugin/parser/pg/test-data/test_backup.yaml+++ b/backend/plugin/parser/pg/test-data/test_backup.yaml@@ -1,3 +1,36 @@+- input: |-+ UPDATE test SET test.c1 = 1 WHERE test.b1 = 1;+ UPDATE test SET test.c1 = 2 WHERE test.b1 = 2;+ UPDATE test SET test.c1 = 3 WHERE test.b1 = 3;+ UPDATE test SET test.c1 = 4 WHERE test.b1 = 4;+ UPDATE test SET test.c1 = 5 WHERE test.b1 = 5;+ UPDATE test SET test.c1 = 6 WHERE test.b1 = 6;+ UPDATE test SET test.c1 = 7 WHERE test.b1 = 7;+ result:+ - statement: |-+ CREATE TABLE "backupSchema"."rollback_test" AS+ SELECT "test".* FROM test WHERE test.b1 = 1+ UNION ALL+ SELECT "test".* FROM test WHERE test.b1 = 2+ UNION ALL+ SELECT "test".* FROM test WHERE test.b1 = 3+ UNION ALL+ SELECT "test".* FROM test WHERE test.b1 = 4+ UNION ALL+ SELECT "test".* FROM test WHERE test.b1 = 5+ UNION ALL+ SELECT "test".* FROM test WHERE test.b1 = 6+ UNION ALL+ SELECT "test".* FROM test WHERE test.b1 = 7;+ sourceschema: ""+ sourcetablename: test+ targettablename: rollback_test+ startposition:+ line: 1+ column: 0+ endposition:+ line: 7+ column: 44- input: |-DELETE FROM tUSING test