Skip to content

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

chore: use a single connection to execute migration in the lifetime of a MySQL driver (#3267)

Yilong Li82f118f9d174
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index 99e933d633..62f530cf22 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -38,6 +38,10 @@ type Driver struct {
resourceDir string
binlogDir string
db *sql.DB
+ // conn 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
replayedBinlogBytes *common.CountingReader
restoredBackupBytes *common.CountingReader
@@ -51,7 +55,7 @@ func newDriver(dc db.DriverConfig) db.Driver {
}
// Open opens a MySQL driver.
-func (driver *Driver) Open(_ context.Context, dbType db.Type, connCfg db.ConnectionConfig, connCtx db.ConnectionContext) (db.Driver, error) {
+func (driver *Driver) Open(ctx context.Context, dbType db.Type, connCfg db.ConnectionConfig, connCtx db.ConnectionContext) (db.Driver, error) {
protocol := "tcp"
if strings.HasPrefix(connCfg.Host, "/") {
protocol = "unix"
@@ -90,8 +94,13 @@ func (driver *Driver) Open(_ context.Context, dbType db.Type, connCfg db.Connect
if err != nil {
return nil, err
}
+ conn, err := db.Conn(ctx)
+ if err != nil {
+ return nil, err
+ }
driver.dbType = dbType
driver.db = db
+ driver.conn = conn
driver.connectionCtx = connCtx
driver.connCfg = connCfg
@@ -156,7 +165,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string) error {
return err
}
transformedStatement := buf.String()
- tx, err := driver.db.BeginTx(ctx, nil)
+ tx, err := driver.conn.BeginTx(ctx, nil)
if err != nil {
return err
}