Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: rollback sql equal op (#9253)

p0ny1b7213911586
diff --git a/backend/plugin/db/mysql/rollback.go b/backend/plugin/db/mysql/rollback.go
index 18cfa174a4..0573b5b830 100644
--- a/backend/plugin/db/mysql/rollback.go
+++ b/backend/plugin/db/mysql/rollback.go
@@ -240,6 +240,17 @@ func replacePrefix(line, old, new string) string {
}
func replaceColumns(columnNames []string, body []string, sepLine, lineSuffix, sectionSuffix string) ([]string, error) {
+ var equal string
+ switch sepLine {
+ case "SET":
+ equal = "="
+ case "WHERE":
+ // Use NULL-safe equal operator.
+ equal = "<=>"
+ default:
+ return nil, errors.Errorf("invalid sepLine %q", sepLine)
+ }
+
var ret []string
for i := 0; i < len(body); {
line := body[i]
@@ -261,9 +272,9 @@ func replaceColumns(columnNames []string, body []string, sepLine, lineSuffix, se
return nil, errors.Errorf("invalid value line %q, must starts with %q", line, prefix)
}
if j == len(columnNames)-1 {
- ret = append(ret, fmt.Sprintf(" `%s`=%s%s", columnNames[j], strings.TrimPrefix(line, prefix), sectionSuffix))
+ ret = append(ret, fmt.Sprintf(" `%s`%s%s%s", columnNames[j], equal, strings.TrimPrefix(line, prefix), sectionSuffix))
} else {
- ret = append(ret, fmt.Sprintf(" `%s`=%s%s", columnNames[j], strings.TrimPrefix(line, prefix), lineSuffix))
+ ret = append(ret, fmt.Sprintf(" `%s`%s%s%s", columnNames[j], equal, strings.TrimPrefix(line, prefix), lineSuffix))
}
}
i += len(columnNames)
diff --git a/backend/plugin/db/mysql/rollback_test.go b/backend/plugin/db/mysql/rollback_test.go
index 9364491c59..ec0226be90 100644
--- a/backend/plugin/db/mysql/rollback_test.go
+++ b/backend/plugin/db/mysql/rollback_test.go
@@ -63,19 +63,19 @@ BEGIN
},
rollbackSQL: `DELETE FROM ` + "`binlog_test`.`user`" + `
WHERE
- ` + "`id`" + `=1 AND
- ` + "`name`" + `='alice' AND
- ` + "`balance`" + `=100;
+ ` + "`id`" + `<=>1 AND
+ ` + "`name`" + `<=>'alice' AND
+ ` + "`balance`" + `<=>100;
DELETE FROM ` + "`binlog_test`.`user`" + `
WHERE
- ` + "`id`" + `=2 AND
- ` + "`name`" + `='bob' AND
- ` + "`balance`" + `=100;
+ ` + "`id`" + `<=>2 AND
+ ` + "`name`" + `<=>'bob' AND
+ ` + "`balance`" + `<=>100;
DELETE FROM ` + "`binlog_test`.`user`" + `
WHERE
- ` + "`id`" + `=3 AND
- ` + "`name`" + `='cindy' AND
- ` + "`balance`" + `=100;`,
+ ` + "`id`" + `<=>3 AND
+ ` + "`name`" + `<=>'cindy' AND
+ ` + "`balance`" + `<=>100;`,
err: false,
},
{
@@ -131,18 +131,18 @@ SET
` + "`name`" + `='bob',
` + "`balance`" + `=100
WHERE
- ` + "`id`" + `=2 AND
- ` + "`name`" + `='bob' AND
- ` + "`balance`" + `=110;
+ ` + "`id`" + `<=>2 AND
+ ` + "`name`" + `<=>'bob' AND
+ ` + "`balance`" + `<=>110;
UPDATE ` + "`binlog_test`.`user`" + `
SET
` + "`id`" + `=1,
` + "`name`" + `='alice',
` + "`balance`" + `=100
WHERE
- ` + "`id`" + `=1 AND
- ` + "`name`" + `='alice' AND
- ` + "`balance`" + `=90;`,
+ ` + "`id`" + `<=>1 AND
+ ` + "`name`" + `<=>'alice' AND
+ ` + "`balance`" + `<=>90;`,
err: false,
},
{