Skip to content

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

fix(ghost): trim trailing semicolon so attempt-instant-ddl stays valid (#20666)

Shogo Tanaka16e5d84e1bbd
diff --git a/backend/component/ghost/config.go b/backend/component/ghost/config.go
index 235608e311..8c93425650 100644
--- a/backend/component/ghost/config.go
+++ b/backend/component/ghost/config.go
@@ -263,7 +263,7 @@ func NewMigrationContext(ctx context.Context, taskID int64, database *store.Data
migrationContext.GhostDatabaseName = common.BackupDatabaseNameOfEngine(storepb.Engine_MYSQL)
migrationContext.DatabaseName = database.DatabaseName
migrationContext.OriginalTableName = tableName
- migrationContext.AlterStatement = strings.Join(strings.Fields(statement), " ")
+ migrationContext.AlterStatement = strings.TrimRight(strings.Join(strings.Fields(statement), " "), "; ")
migrationContext.Noop = noop
// On the source and each replica, you must set the server_id system variable to establish a unique replication ID. For each server, you should pick a unique positive integer in the range from 1 to 2^32 − 1, and each ID must be different from every other ID in use by any other source or replica in the replication topology. Example: server-id=3.
// https://dev.mysql.com/doc/refman/5.7/en/replication-options-source.html
diff --git a/backend/component/ghost/config_test.go b/backend/component/ghost/config_test.go
index be4c4ce189..19586c2c0c 100644
--- a/backend/component/ghost/config_test.go
+++ b/backend/component/ghost/config_test.go
@@ -55,6 +55,48 @@ func TestNewMigrationContextUsesContextAttrs(t *testing.T) {
require.NotContains(t, output, `replica_id=`)
}
+func TestNewMigrationContextTrimsTrailingSemicolon(t *testing.T) {
+ ctx := context.Background()
+ database := &store.DatabaseMessage{DatabaseName: "ghostdb"}
+ dataSource := &storepb.DataSource{
+ Host: "127.0.0.1",
+ Port: "3306",
+ Username: "root",
+ AuthenticationType: storepb.DataSource_PASSWORD,
+ }
+
+ testCases := []struct {
+ name string
+ statement string
+ wantAlter string
+ }{
+ {
+ name: "trailing semicolon",
+ statement: "ALTER TABLE t MODIFY COLUMN id BIGINT NOT NULL AUTO_INCREMENT;",
+ wantAlter: "ALTER TABLE t MODIFY COLUMN id BIGINT NOT NULL AUTO_INCREMENT",
+ },
+ {
+ name: "trailing semicolon with whitespace",
+ statement: "ALTER TABLE t ADD COLUMN c INT ; ",
+ wantAlter: "ALTER TABLE t ADD COLUMN c INT",
+ },
+ {
+ name: "no trailing semicolon",
+ statement: "ALTER TABLE t ADD COLUMN c INT",
+ wantAlter: "ALTER TABLE t ADD COLUMN c INT",
+ },
+ }
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ migrationContext, cleanup, err := NewMigrationContext(ctx, 1, database, dataSource, "t", "_suffix", tc.statement, false, nil, 0)
+ require.NoError(t, err)
+ t.Cleanup(cleanup)
+ require.Equal(t, tc.wantAlter, migrationContext.AlterStatement)
+ require.NotContains(t, migrationContext.AlterStatementOptions, ";")
+ })
+ }
+}
+
func TestNewMigrationContextWritesTLSMaterialToTempFiles(t *testing.T) {
certPEM, keyPEM := generateSelfSignedPEM(t)
diff --git a/backend/runner/taskrun/database_migrate_executor.go b/backend/runner/taskrun/database_migrate_executor.go
index b610b691f9..8ae59e1d04 100644
--- a/backend/runner/taskrun/database_migrate_executor.go
+++ b/backend/runner/taskrun/database_migrate_executor.go
@@ -345,7 +345,7 @@ func executeGhostMigration(ctx context.Context, driverCtx context.Context, task
return common.Errorf(common.Internal, "admin data source not found for instance %s", instance.ResourceID)
}
- migrationContext, cleanup, err := ghost.NewMigrationContext(ctx, task.ID, database, adminDataSource, tableName, fmt.Sprintf("_%d", time.Now().Unix()), cleanedStatement, false, flags, 10000000)
+ migrationContext, cleanup, err := ghost.NewMigrationContext(ctx, task.ID, database, adminDataSource, tableName, fmt.Sprintf("_%d", time.Now().Unix()), statement, false, flags, 10000000)
if err != nil {
return errors.Wrap(err, "failed to init migrationContext for gh-ost")
}