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(tsql): reject CTE targets and mismatched CTE scopes in prior backup
rebelice3eba1f28b161
diff --git a/backend/plugin/parser/tsql/backup.go b/backend/plugin/parser/tsql/backup.goindex 45d8e43f4c..2cc2ac77b7 100644--- a/backend/plugin/parser/tsql/backup.go+++ b/backend/plugin/parser/tsql/backup.go@@ -178,43 +178,33 @@ func generateSQLForTable(statementInfoList []statementInfo, targetDatabase strin}, nil}-// backupCTEClause merges the WITH clauses of every statement in the group into-// one clause for the backup SELECT INTO. T-SQL only allows a CTE list at the-// top of a statement, so a CTE referenced inside a UNION branch must be-// hoisted here. The same CTE name across statements must carry the same-// definition, because the merged clause can only bind it once.+// backupCTEClause returns the WITH clause to place ahead of the backup SELECT+// INTO. T-SQL only allows a CTE list at the top of a statement, so a CTE+// referenced inside a UNION branch must be hoisted to the shared clause. The+// hoisted clause becomes the name scope of every branch, so it is only safe+// when each statement in the group carries the same clause text; a branch+// written without that clause could otherwise resolve a base table name to a+// CTE.func backupCTEClause(statementInfoList []statementInfo) (string, error) {- var ctes []string- definitions := make(map[string]string)- for _, item := range statementInfoList {+ var clause string+ for i, item := range statementInfoList {withClause := dmlWithClause(item.node)- if withClause == nil || withClause.CTEs == nil {+ var text string+ if withClause != nil {+ if withClause.XmlNamespaces != nil {+ return "", errors.New("prior backup does not support WITH XMLNAMESPACES")+ }+ text = sourceFromLoc(item.statement, withClause.Loc)+ }+ if i == 0 {+ clause = textcontinue}- if withClause.XmlNamespaces != nil {- return "", errors.New("prior backup does not support WITH XMLNAMESPACES")- }- for _, cteNode := range withClause.CTEs.Items {- cte, ok := cteNode.(*ast.CommonTableExpr)- if !ok {- continue- }- definition := sourceFromLoc(item.statement, cte.Loc)- key := strings.ToLower(cte.Name)- if previous, ok := definitions[key]; ok {- if previous != definition {- return "", errors.Errorf("prior backup cannot handle conflicting definitions of CTE %q across statements", cte.Name)- }- continue- }- definitions[key] = definition- ctes = append(ctes, definition)+ if text != clause {+ return "", errors.New("prior backup cannot handle statements with different WITH clauses on the same table")}}- if len(ctes) == 0 {- return "", nil- }- return "WITH " + strings.Join(ctes, ",\n"), nil+ return clause, nil}func dmlWithClause(node ast.Node) *ast.WithClause {@@ -229,8 +219,9 @@ func dmlWithClause(node ast.Node) *ast.WithClause {}// isCTETarget reports whether an unqualified DML target names a CTE declared-// by the statement's own WITH clause. Such a DML writes through the CTE to its-// base table, which the backup cannot resolve, so the caller skips it.+// by the statement's own WITH clause. Such a DML writes through the CTE to a+// base table the backup cannot resolve, so the caller rejects it rather than+// letting the migration run without backup data.func isCTETarget(ref *ast.TableRef, withClause *ast.WithClause) bool {if ref == nil || withClause == nil || withClause.CTEs == nil || ref.Database != "" || ref.Schema != "" {return false@@ -303,10 +294,7 @@ func prepareTransformation(databaseName, statement string) ([]statementInfo, errcontinue}if isCTETarget(targetRef, dmlWithClause(node)) {- slog.Info("prior backup: skipping DML targeting CTE",- "cte", table.Table,- "statementType", statementType)- continue+ return nil, errors.Errorf("prior backup does not support DML targeting CTE %q, target the base table directly", table.Table)}table.StatementType = statementTypeloc := dmlNodeLoc(node)diff --git a/backend/plugin/parser/tsql/backup_test.go b/backend/plugin/parser/tsql/backup_test.goindex f4f79fa2a0..a812063384 100644--- a/backend/plugin/parser/tsql/backup_test.go+++ b/backend/plugin/parser/tsql/backup_test.go@@ -36,6 +36,7 @@ func TestBackupOmniBoundaryCases(t *testing.T) {name stringinput stringwantSQL string+ wantSQLs []stringwantErrPart string}{{@@ -100,8 +101,7 @@ func TestBackupOmniBoundaryCases(t *testing.T) {name: "delete with cte",input: "WITH c (id) AS (SELECT id FROM src), d AS (SELECT 1 AS n) DELETE FROM test WHERE id IN (SELECT id FROM c);",wantSQL: strings.Join([]string{- "WITH c (id) AS (SELECT id FROM src),",- "d AS (SELECT 1 AS n)",+ "WITH c (id) AS (SELECT id FROM src), d AS (SELECT 1 AS n)","SELECT * INTO [backupDB].[dbo].[rollback_test_db] FROM ("," SELECT [db].[dbo].[test].* FROM test WHERE id IN (SELECT id FROM c)) AS backup_table;",}, "\n"),@@ -116,27 +116,52 @@ func TestBackupOmniBoundaryCases(t *testing.T) {}, "\n"),},{- name: "cte lists merge across statements",+ name: "identical with clause across statements is hoisted once",input: strings.Join([]string{"WITH c AS (SELECT id FROM src) UPDATE test SET c1 = 1 WHERE id IN (SELECT id FROM c);",- "WITH c AS (SELECT id FROM src), d AS (SELECT id FROM other) UPDATE test SET c1 = 2 WHERE id IN (SELECT id FROM d);",+ "WITH c AS (SELECT id FROM src) UPDATE test SET c1 = 2 WHERE id IN (SELECT id FROM c);",}, "\n"),wantSQL: strings.Join([]string{- "WITH c AS (SELECT id FROM src),",- "d AS (SELECT id FROM other)",+ "WITH c AS (SELECT id FROM src)","SELECT * INTO [backupDB].[dbo].[rollback_test_db] FROM ("," SELECT [db].[dbo].[test].* FROM test WHERE id IN (SELECT id FROM c)"," UNION",- " SELECT [db].[dbo].[test].* FROM test WHERE id IN (SELECT id FROM d)) AS backup_table;",+ " SELECT [db].[dbo].[test].* FROM test WHERE id IN (SELECT id FROM c)) AS backup_table;",+ }, "\n"),+ },+ {+ name: "different with clauses on the same table rejected",+ input: strings.Join([]string{+ "WITH c AS (SELECT id FROM src) UPDATE test SET c1 = 1 WHERE id IN (SELECT id FROM c);",+ "WITH c AS (SELECT id FROM src), d AS (SELECT id FROM other) UPDATE test SET c1 = 2 WHERE id IN (SELECT id FROM d);",+ }, "\n"),+ wantErrPart: "different WITH clauses on the same table",+ },+ {+ name: "with clause would capture a base table name in another statement",+ input: strings.Join([]string{+ "WITH c AS (SELECT id FROM src) UPDATE test SET c1 = 1 WHERE id IN (SELECT id FROM c);",+ "UPDATE test SET c1 = 2 WHERE id IN (SELECT id FROM c);",}, "\n"),+ wantErrPart: "different WITH clauses on the same table",},{- name: "conflicting cte definitions rejected",+ name: "with clause on another table is scoped to its own backup",input: strings.Join([]string{"WITH c AS (SELECT id FROM src) UPDATE test SET c1 = 1 WHERE id IN (SELECT id FROM c);",- "WITH c AS (SELECT id FROM other) UPDATE test SET c1 = 2 WHERE id IN (SELECT id FROM c);",+ "UPDATE c SET c1 = 2 WHERE id = 1;",}, "\n"),- wantErrPart: `conflicting definitions of CTE "c"`,+ wantSQLs: []string{+ strings.Join([]string{+ "SELECT * INTO [backupDB].[dbo].[rollback_c_db] FROM (",+ " SELECT [db].[dbo].[c].* FROM c WHERE id = 1) AS backup_table;",+ }, "\n"),+ strings.Join([]string{+ "WITH c AS (SELECT id FROM src)",+ "SELECT * INTO [backupDB].[dbo].[rollback_test_db] FROM (",+ " SELECT [db].[dbo].[test].* FROM test WHERE id IN (SELECT id FROM c)) AS backup_table;",+ }, "\n"),+ },},{name: "xmlnamespaces rejected",@@ -154,33 +179,34 @@ func TestBackupOmniBoundaryCases(t *testing.T) {return}require.NoError(t, err)+ if tc.wantSQLs != nil {+ var got []string+ for _, item := range result {+ got = append(got, item.Statement)+ }+ slices.Sort(got)+ require.Equal(t, tc.wantSQLs, got)+ return+ }require.Len(t, result, 1)require.Equal(t, tc.wantSQL, result[0].Statement)})}}-func TestBackupSkipsCTETarget(t *testing.T) {+func TestBackupRejectsCTETarget(t *testing.T) {for name, input := range map[string]string{- "direct": "WITH c AS (SELECT id, c1 FROM test) UPDATE c SET c1 = 1 WHERE id = 1;",- "alias": "WITH c AS (SELECT id, c1 FROM test) UPDATE x SET c1 = 1 FROM c AS x WHERE x.id = 1;",- "delete": "WITH c AS (SELECT id FROM test) DELETE FROM c WHERE id = 1;",- "mixed case": "WITH C AS (SELECT id, c1 FROM test) UPDATE c SET c1 = 1 WHERE id = 1;",- "other backed": "WITH c AS (SELECT id FROM test) UPDATE c SET c1 = 1 WHERE id = 1; DELETE FROM test WHERE id = 2;",+ "direct": "WITH c AS (SELECT id, c1 FROM test) UPDATE c SET c1 = 1 WHERE id = 1;",+ "alias": "WITH c AS (SELECT id, c1 FROM test) UPDATE x SET c1 = 1 FROM c AS x WHERE x.id = 1;",+ "delete": "WITH c AS (SELECT id FROM test) DELETE FROM c WHERE id = 1;",+ "mixed case": "WITH C AS (SELECT id, c1 FROM test) UPDATE c SET c1 = 1 WHERE id = 1;",+ "after other dml": "DELETE FROM test WHERE id = 2; WITH c AS (SELECT id FROM test) UPDATE c SET c1 = 1 WHERE id = 1;",} {t.Run(name, func(t *testing.T) {result, err := TransformDMLToSelect(context.Background(), base.TransformContext{}, input, "db", "backupDB", "rollback")- require.NoError(t, err)- for _, item := range result {- require.NotEqual(t, "c", item.SourceTableName, item.Statement)- require.NotContains(t, item.Statement, "WITH", item.Statement)- }- if name == "other backed" {- require.Len(t, result, 1)- require.Equal(t, "test", result[0].SourceTableName)- } else {- require.Empty(t, result)- }+ require.Error(t, err)+ require.Contains(t, err.Error(), `does not support DML targeting CTE "c"`)+ require.Empty(t, result)})}// A schema-qualified target is a real table even when a CTE shares its name.