Skip to content

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

refactor(flashback): parse table name (#3305)

Yilong Liaaff99b4c871
diff --git a/plugin/db/mysql/rollback.go b/plugin/db/mysql/rollback.go
index f6b622abeb..adae10ba84 100644
--- a/plugin/db/mysql/rollback.go
+++ b/plugin/db/mysql/rollback.go
@@ -2,13 +2,19 @@ package mysql
import (
"fmt"
+ "regexp"
"strings"
"github.com/pkg/errors"
)
+var (
+ // The (?s) is a modifier that makes "." to match the new line character, which is a valid character in the MySQL database and name.
+ reDatabaseTable = regexp.MustCompile("(?s)`(.+)`\\.`(.+)`")
+)
+
// GetRollbackSQL generates the rollback SQL for the list of binlog events in the reversed order.
-func (txn BinlogTransaction) GetRollbackSQL(columnNames []string) (string, error) {
+func (txn BinlogTransaction) GetRollbackSQL(tables map[string][]string) (string, error) {
if len(txn) == 0 {
return "", nil
}
@@ -20,7 +26,7 @@ func (txn BinlogTransaction) GetRollbackSQL(columnNames []string) (string, error
if e.Type != WriteRowsEventType && e.Type != DeleteRowsEventType && e.Type != UpdateRowsEventType {
continue
}
- sql, err := e.getRollbackSQL(columnNames)
+ sql, err := e.getRollbackSQL(tables)
if err != nil {
return "", err
}
@@ -29,12 +35,22 @@ func (txn BinlogTransaction) GetRollbackSQL(columnNames []string) (string, error
return strings.Join(sqlList, "\n"), nil
}
-func (e *BinlogEvent) getRollbackSQL(columnNames []string) (string, error) {
+func (e *BinlogEvent) getRollbackSQL(tables map[string][]string) (string, error) {
// 1. Remove the "### " prefix of each line.
// mysqlbinlog output is separated by "\n", ref https://sourcegraph.com/github.com/mysql/mysql-server@a246bad76b9271cb4333634e954040a970222e0a/-/blob/sql/log_event.cc?L2398
body := strings.Split(e.Body, "\n")
body = replaceAllPrefix(body, "### ", "")
+ matches := reDatabaseTable.FindStringSubmatch(e.Body)
+ if len(matches) != 3 {
+ return "", errors.Errorf("failed to match database and table names in binlog event %q", e.Body)
+ }
+ tableName := matches[2]
+ columnNames, ok := tables[tableName]
+ if !ok {
+ return "", errors.Errorf("table %s does not exist in the provided table map", tableName)
+ }
+
// 2. Switch "DELETE FROM" and "INSERT INTO".
// 3. Replace "WHERE" and "SET" with each other.
// 4. Replace "@i" with the column names.
diff --git a/plugin/db/mysql/rollback_test.go b/plugin/db/mysql/rollback_test.go
index a9d51664c1..b1f8299f35 100644
--- a/plugin/db/mysql/rollback_test.go
+++ b/plugin/db/mysql/rollback_test.go
@@ -10,14 +10,14 @@ func TestGetRollbackSQL(t *testing.T) {
tests := []struct {
name string
txn BinlogTransaction
- columnNames []string
+ tableMap map[string][]string
rollbackSQL string
err bool
}{
{
name: "empty",
txn: BinlogTransaction{},
- columnNames: []string{"what", "ever"},
+ tableMap: map[string][]string{},
rollbackSQL: "",
err: false,
},
@@ -58,7 +58,9 @@ BEGIN
`,
},
},
- columnNames: []string{"id", "name", "balance"},
+ tableMap: map[string][]string{
+ "user": {"id", "name", "balance"},
+ },
rollbackSQL: `DELETE FROM ` + "`binlog_test`.`user`" + `
WHERE
` + "`id`" + `=1 AND
@@ -120,7 +122,9 @@ BEGIN
`,
},
},
- columnNames: []string{"id", "name", "balance"},
+ tableMap: map[string][]string{
+ "user": {"id", "name", "balance"},
+ },
rollbackSQL: `UPDATE ` + "`binlog_test`.`user`" + `
SET
` + "`id`" + `=2,
@@ -177,7 +181,9 @@ DELIMITER ;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;`,
},
},
- columnNames: []string{"id", "name", "balance"},
+ tableMap: map[string][]string{
+ "user": {"id", "name", "balance"},
+ },
rollbackSQL: `INSERT INTO ` + "`binlog_test`.`user`" + `
SET
` + "`id`" + `=1,
@@ -203,7 +209,9 @@ SET
### @3=0`,
},
},
- columnNames: []string{"id", "name", "balance", "new_field"},
+ tableMap: map[string][]string{
+ "user": {"id", "name", "balance", "new_column"},
+ },
rollbackSQL: "",
err: true,
},
@@ -212,7 +220,7 @@ SET
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
a := require.New(t)
- sql, err := test.txn.GetRollbackSQL(test.columnNames)
+ sql, err := test.txn.GetRollbackSQL(test.tableMap)
if test.err {
a.Error(err)
} else {
diff --git a/tests/rollback_test.go b/tests/rollback_test.go
index 8c06c16cc9..c85432acf1 100644
--- a/tests/rollback_test.go
+++ b/tests/rollback_test.go
@@ -83,8 +83,11 @@ func TestRollback(t *testing.T) {
a.NoError(err)
a.Equal(2, len(txList))
var rollbackSQLList []string
+ tableMap := map[string][]string{
+ "user": {"id", "name", "balance"},
+ }
for _, tx := range txList {
- sql, err := tx.GetRollbackSQL([]string{"id", "name", "balance"})
+ sql, err := tx.GetRollbackSQL(tableMap)
a.NoError(err)
rollbackSQLList = append(rollbackSQLList, sql)
}