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(mssql): skip prior backup for DMLs targeting temp tables (BYT-9359) (#20168)
h3n4l0135e0a3348e
diff --git a/backend/plugin/parser/tsql/backup.go b/backend/plugin/parser/tsql/backup.goindex a906ea00a7..cffbf886de 100644--- a/backend/plugin/parser/tsql/backup.go+++ b/backend/plugin/parser/tsql/backup.go@@ -3,6 +3,7 @@ package tsqlimport ("context""fmt"+ "log/slog""slices""strings"@@ -220,6 +221,12 @@ func prepareTransformation(databaseName, statement string) ([]statementInfo, errif table == nil {return nil, errors.Errorf("failed to resolve DML target table")}+ if strings.HasPrefix(table.Table, "#") {+ slog.Info("prior backup: skipping DML targeting temp table",+ "table", table.Table,+ "statementType", statementType)+ continue+ }table.StatementType = statementTypeloc := dmlNodeLoc(node)dmls = append(dmls, statementInfo{@@ -512,10 +519,7 @@ func sourceFromLoc(source string, loc ast.Loc) string {if loc.Start < 0 || loc.End < 0 || loc.Start >= len(source) {return ""}- end := loc.End- if end > len(source) {- end = len(source)- }+ end := min(loc.End, len(source))return strings.TrimSpace(source[loc.Start:end])}diff --git a/backend/plugin/parser/tsql/backup_test.go b/backend/plugin/parser/tsql/backup_test.goindex 57cacf3771..d2588d96c4 100644--- a/backend/plugin/parser/tsql/backup_test.go+++ b/backend/plugin/parser/tsql/backup_test.go@@ -243,3 +243,54 @@ func TestBackupWithQuotedStrings(t *testing.T) {// The WHERE clause should be presenta.Contains(stmt2, "WHERE id = 1")}++// TestBackupSkipsTempTableTargets validates BYT-9359: prior backup must skip+// any UPDATE/DELETE whose target table is a temp table (#name local, ##name+// global). Temp tables are session-scoped, so a backup query running in a+// separate session would fail with "Invalid object name". Skipping is the+// only sane outcome — the rows can't be reconstructed across sessions and+// have no rollback semantics anyway.+func TestBackupSkipsTempTableTargets(t *testing.T) {+ tests := []struct {+ name string+ input string+ wantLen int+ wantTargetTable string // only checked when wantLen == 1+ }{+ {+ name: "update local temp table only",+ input: "UPDATE #tmp SET c1 = 1 WHERE c2 = 2;",+ wantLen: 0,+ },+ {+ name: "delete from local temp table only",+ input: "DELETE FROM #tmp WHERE c1 = 1;",+ wantLen: 0,+ },+ {+ name: "update global temp table only",+ input: "UPDATE ##gtmp SET c1 = 1 WHERE c2 = 2;",+ wantLen: 0,+ },+ {+ name: "mixed temp and real targets",+ input: strings.Join([]string{+ "UPDATE #tmp SET c1 = 1 WHERE c2 = 2;",+ "UPDATE test SET c1 = 2 WHERE c1 = 1;",+ }, "\n"),+ wantLen: 1,+ wantTargetTable: "rollback_test_db",+ },+ }++ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ result, err := TransformDMLToSelect(context.Background(), base.TransformContext{}, tc.input, "db", "backupDB", "rollback")+ require.NoError(t, err)+ require.Len(t, result, tc.wantLen)+ if tc.wantLen == 1 {+ require.Equal(t, tc.wantTargetTable, result[0].TargetTableName)+ }+ })+ }+}