Skip to content

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

fix: gh-ost sync task executor EndMigration logic (#1270)

p0nydf0e8c8e247a
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index 1407f8a016..e297976309 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -132,7 +132,7 @@ func ExecuteMigration(ctx context.Context, l *zap.Logger, executor MigrationExec
// For baseline migration, we also record the live schema to detect the schema drift.
// See https://bytebase.com/blog/what-is-database-schema-drift
if err := executor.Dump(ctx, m.Database, &prevSchemaBuf, true /*schemaOnly*/); err != nil {
- return -1, "", formatError(err)
+ return -1, "", FormatError(err)
}
}
@@ -166,14 +166,14 @@ func ExecuteMigration(ctx context.Context, l *zap.Logger, executor MigrationExec
}
}
if err := executor.Execute(ctx, statement); err != nil {
- return -1, "", formatError(err)
+ return -1, "", FormatError(err)
}
}
// Phase 4 - Dump the schema after migration
var afterSchemaBuf bytes.Buffer
if err := executor.Dump(ctx, m.Database, &afterSchemaBuf, true /*schemaOnly*/); err != nil {
- return -1, "", formatError(err)
+ return -1, "", FormatError(err)
}
return insertedID, afterSchemaBuf.String(), nil
@@ -296,12 +296,12 @@ func Query(ctx context.Context, l *zap.Logger, sqldb *sql.DB, statement string,
columnNames, err := rows.Columns()
if err != nil {
- return nil, formatError(err)
+ return nil, FormatError(err)
}
columnTypes, err := rows.ColumnTypes()
if err != nil {
- return nil, formatError(err)
+ return nil, FormatError(err)
}
colCount := len(columnTypes)
@@ -334,7 +334,7 @@ func Query(ctx context.Context, l *zap.Logger, sqldb *sql.DB, statement string,
}
if err := rows.Scan(scanArgs...); err != nil {
- return nil, formatError(err)
+ return nil, FormatError(err)
}
rowData := []interface{}{}
@@ -440,7 +440,8 @@ func FindMigrationHistoryList(ctx context.Context, findMigrationHistoryListQuery
return migrationHistoryList, nil
}
-func formatError(err error) error {
+// FormatError formats schema migration errors
+func FormatError(err error) error {
if err == nil {
return nil
}
diff --git a/server/task_executor_schema_update_ghost_sync.go b/server/task_executor_schema_update_ghost_sync.go
index ae049dae1b..af97d91633 100644
--- a/server/task_executor_schema_update_ghost_sync.go
+++ b/server/task_executor_schema_update_ghost_sync.go
@@ -153,8 +153,9 @@ func runGhostMigration(ctx context.Context, l *zap.Logger, server *Server, task
return postMigration(ctx, l, server, task, vcsPushEvent, mi, migrationID, schema)
}
-func executeSync(ctx context.Context, l *zap.Logger, task *api.Task, mi *db.MigrationInfo, statement string) (int64, string, error) {
+func executeSync(ctx context.Context, l *zap.Logger, task *api.Task, mi *db.MigrationInfo, statement string) (migrationHistoryID int64, updatedSchema string, resErr error) {
statement = strings.TrimSpace(statement)
+ databaseName := db.BytebaseDatabase
driver, err := getAdminDatabaseDriver(ctx, task.Instance, task.Database.Name, l)
if err != nil {
@@ -176,29 +177,31 @@ func executeSync(ctx context.Context, l *zap.Logger, task *api.Task, mi *db.Migr
return -1, "", err
}
- insertedID, err := util.BeginMigration(ctx, executor, mi, prevSchemaBuf.String(), statement, db.BytebaseDatabase)
+ insertedID, err := util.BeginMigration(ctx, executor, mi, prevSchemaBuf.String(), statement, databaseName)
if err != nil {
return -1, "", err
}
startedNs := time.Now().UnixNano()
+ defer func() {
+ if err := util.EndMigration(ctx, l, executor, startedNs, insertedID, updatedSchema, databaseName, resErr == nil /*isDone*/); err != nil {
+ l.Error("failed to update migration history record",
+ zap.Error(err),
+ zap.Int64("migration_id", migrationHistoryID),
+ )
+ }
+ }()
+
err = executeGhost(task.Instance, task.Database.Name, statement)
if err != nil {
return -1, "", err
}
var afterSchemaBuf bytes.Buffer
- if err := executor.Dump(ctx, mi.Database, &afterSchemaBuf, true); err != nil {
- return -1, "", err
+ if err := executor.Dump(ctx, mi.Database, &afterSchemaBuf, true /*schemaOnly*/); err != nil {
+ return -1, "", util.FormatError(err)
}
- if err := util.EndMigration(ctx, l, executor, startedNs, insertedID, afterSchemaBuf.String(), mi.Database, true); err != nil {
- l.Error("failed to update migration history record",
- zap.Error(err),
- zap.Int64("migration_id",
- insertedID),
- )
- }
return insertedID, afterSchemaBuf.String(), nil
}