fix: replace named locks with Galera-compatible row locks
This commit is contained in:
1 parent
ee07866d1c
commit
65d16c2fc6
22 files changed
+314
-365
No files matched your search
+49
-2
@@ -15,6 +15,7 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
@@ -104,12 +105,58 @@ func splitSQLStatements(content string) []string {
|
||||
return statements
|
||||
}
|
||||
|
||||
func migrateDatabase(ctx context.Context, db connSource, t tableNames, log *slog.Logger) error {
|
||||
const createLockTableSQL = "CREATE TABLE IF NOT EXISTS {locks} (" +
|
||||
"`name` VARBINARY(64) NOT NULL PRIMARY KEY, `generation` TINYINT NOT NULL) ENGINE = InnoDB"
|
||||
|
||||
func migrateDatabase(ctx context.Context, db *sql.DB, t tableNames, log *slog.Logger) error {
|
||||
migrations, err := loadMigrations(migrationFiles, t.prefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return withNamedLock(ctx, db, t.migrateLockName(), migrateLockTimeout, log, func(conn *sql.Conn) error {
|
||||
|
||||
waitCtx, cancel := context.WithTimeout(ctx, migrateLockTimeout)
|
||||
defer cancel()
|
||||
for {
|
||||
err := migrateWithLock(ctx, waitCtx, db, t, migrations, log)
|
||||
if !mysqlErrorIs(err, mysqlErrDeadlock) {
|
||||
return err
|
||||
}
|
||||
// Bootstrap DDL and a concurrent seed can conflict on Galera.
|
||||
// Reacquire the guard and re-read the durable schema version.
|
||||
select {
|
||||
case <-waitCtx.Done():
|
||||
return fmt.Errorf("retry migrations: %w", lockError(ctx, waitCtx, err))
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func migrateWithLock(ctx, waitCtx context.Context, db *sql.DB, t tableNames, migrations []migration, log *slog.Logger) error {
|
||||
name := t.migrateLockName()
|
||||
seed := t.sql("INSERT IGNORE INTO {locks} (`name`, `generation`) VALUES (?, 0)")
|
||||
_, err := db.ExecContext(waitCtx, seed, name)
|
||||
if mysqlErrorIs(err, 1146) {
|
||||
if _, err = db.ExecContext(waitCtx, t.sql(createLockTableSQL)); err != nil {
|
||||
return fmt.Errorf("create lock table: %w", lockError(ctx, waitCtx, err))
|
||||
}
|
||||
_, err = db.ExecContext(waitCtx, seed, name)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("seed migration lock: %w", lockError(ctx, waitCtx, err))
|
||||
}
|
||||
|
||||
return runInTx(ctx, db, func(tx *sql.Tx) error {
|
||||
var key string
|
||||
if err := tx.QueryRowContext(waitCtx, t.sql("SELECT `name` FROM {locks} WHERE `name` = ? FOR UPDATE"), name).Scan(&key); err != nil {
|
||||
return fmt.Errorf("acquire migration lock: %w", lockError(ctx, waitCtx, err))
|
||||
}
|
||||
// DDL implicitly commits its session. Keep the guard transaction
|
||||
// open on its own connection while the other session runs migrations.
|
||||
conn, err := db.Conn(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open migration connection: %w", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
if err := applyMigrations(ctx, conn, t, migrations, log); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user