Skip to content

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

feat(flashback): store thread ID for MySQL DML task (#3266)

Yilong Lic24087708a8b
diff --git a/api/task.go b/api/task.go
index 28c8af0e34..609e11b5a9 100644
--- a/api/task.go
+++ b/api/task.go
@@ -137,6 +137,10 @@ type TaskDatabaseDataUpdatePayload struct {
Statement string `json:"statement,omitempty"`
SchemaVersion string `json:"schemaVersion,omitempty"`
VCSPushEvent *vcs.PushEvent `json:"pushEvent,omitempty"`
+
+ // ThreadID is the ID of the connection executing the migration.
+ // MySQL rollback SQL related.
+ ThreadID string `json:"threadID,omitempty"`
}
// TaskDatabaseBackupPayload is the task payload for database backup.
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index 62f530cf22..1d7da26ad1 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -38,10 +38,10 @@ type Driver struct {
resourceDir string
binlogDir string
db *sql.DB
- // conn is used to execute migrations.
+ // migrationConn is used to execute migrations.
// Use a single connection for executing migrations in the lifetime of the driver can keep the thread ID unchanged.
// So that it's easy to get the thread ID for rollback SQL.
- conn *sql.Conn
+ migrationConn *sql.Conn
replayedBinlogBytes *common.CountingReader
restoredBackupBytes *common.CountingReader
@@ -100,7 +100,7 @@ func (driver *Driver) Open(ctx context.Context, dbType db.Type, connCfg db.Conne
}
driver.dbType = dbType
driver.db = db
- driver.conn = conn
+ driver.migrationConn = conn
driver.connectionCtx = connCtx
driver.connCfg = connCfg
@@ -165,7 +165,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string) error {
return err
}
transformedStatement := buf.String()
- tx, err := driver.conn.BeginTx(ctx, nil)
+ tx, err := driver.migrationConn.BeginTx(ctx, nil)
if err != nil {
return err
}
@@ -182,6 +182,15 @@ func (driver *Driver) Execute(ctx context.Context, statement string) error {
return err
}
+// GetMigrationConnID gets the ID of the connection executing migrations.
+func (driver *Driver) GetMigrationConnID(ctx context.Context) (string, error) {
+ var id string
+ if err := driver.migrationConn.QueryRowContext(ctx, "SELECT CONNECTION_ID();").Scan(&id); err != nil {
+ return "", errors.Wrap(err, "failed to get the connection ID")
+ }
+ return id, nil
+}
+
// Query queries a SQL statement.
func (driver *Driver) Query(ctx context.Context, statement string, limit int, readOnly bool) ([]interface{}, error) {
return util.Query(ctx, driver.dbType, driver.db, statement, limit, readOnly)
diff --git a/server/task_executor.go b/server/task_executor.go
index 8a40ff6c8c..448aaaf745 100644
--- a/server/task_executor.go
+++ b/server/task_executor.go
@@ -15,9 +15,11 @@ import (
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/db/mysql"
"github.com/bytebase/bytebase/plugin/parser"
"github.com/bytebase/bytebase/plugin/parser/transform"
vcsPlugin "github.com/bytebase/bytebase/plugin/vcs"
+ "github.com/bytebase/bytebase/store"
)
// TaskExecutor is the task executor.
@@ -114,7 +116,7 @@ func preMigration(ctx context.Context, server *Server, task *api.Task, migration
if mi.Type != db.Baseline && statement == "" {
return nil, errors.Errorf("empty statement")
}
- // We will force migration for baseline and migrate type of migrations.
+ // We will force migration for baseline, migrate and data type of migrations.
// This usually happens when the previous attempt fails and the client retries the migration.
// We also force migration for VCS migrations, which is usually a modified file to correct a former wrong migration commit.
if mi.Type == db.Baseline || mi.Type == db.Migrate || mi.Type == db.Data {
@@ -150,13 +152,50 @@ func executeMigration(ctx context.Context, server *Server, task *api.Task, state
return 0, "", common.Errorf(common.MigrationSchemaMissing, "missing migration schema for instance %q", task.Instance.Name)
}
+ if task.Type == api.TaskDatabaseDataUpdate && task.Instance.Engine == db.MySQL {
+ if err := updateTaskPayloadForMySQLThreadID(ctx, driver, task, server.store); err != nil {
+ return 0, "", errors.Wrap(err, "failed to update the task payload for MySQL rollback SQL")
+ }
+ }
+
migrationID, schema, err = driver.ExecuteMigration(ctx, mi, statement)
if err != nil {
return 0, "", err
}
+
return migrationID, schema, nil
}
+func updateTaskPayloadForMySQLThreadID(ctx context.Context, driver db.Driver, task *api.Task, store *store.Store) error {
+ mysqlDriver, ok := driver.(*mysql.Driver)
+ if !ok {
+ return errors.Errorf("failed to cast driver to mysql.Driver")
+ }
+ payload := &api.TaskDatabaseDataUpdatePayload{}
+ if err := json.Unmarshal([]byte(task.Payload), payload); err != nil {
+ return errors.Wrap(err, "invalid database data update payload")
+ }
+ connID, err := mysqlDriver.GetMigrationConnID(ctx)
+ if err != nil {
+ return err
+ }
+ payload.ThreadID = connID
+ payloadBytes, err := json.Marshal(payload)
+ if err != nil {
+ return errors.Wrap(err, "failed to marshal task payload")
+ }
+ payloadString := string(payloadBytes)
+ patch := &api.TaskPatch{
+ ID: task.ID,
+ UpdaterID: api.SystemBotID,
+ Payload: &payloadString,
+ }
+ if _, err := store.PatchTask(ctx, patch); err != nil {
+ return errors.Wrapf(err, "failed to patch task %d with the MySQL thread ID", task.ID)
+ }
+ return nil
+}
+
func postMigration(ctx context.Context, server *Server, task *api.Task, vcsPushEvent *vcsPlugin.PushEvent, mi *db.MigrationInfo, migrationID int64, schema string) (bool, *api.TaskRunResultPayload, error) {
databaseName := task.Database.Name
issue, err := findIssueByTask(ctx, server, task)