Skip to content

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

fix: trim statement for gh-ost parser (#1382)

p0ny1788e2097cfb
diff --git a/server/task_executor_schema_update_ghost_cutover.go b/server/task_executor_schema_update_ghost_cutover.go
index a715898c10..0b46feda57 100644
--- a/server/task_executor_schema_update_ghost_cutover.go
+++ b/server/task_executor_schema_update_ghost_cutover.go
@@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"os"
- "strings"
"time"
"github.com/bytebase/bytebase/api"
@@ -41,8 +40,7 @@ func (exec *SchemaUpdateGhostCutoverTaskExecutor) RunOnce(ctx context.Context, s
return true, nil, fmt.Errorf("invalid database schema update gh-ost sync payload: %w", err)
}
- statement := strings.TrimSpace(payload.Statement)
- tableName, err := getTableNameFromStatement(statement)
+ tableName, err := getTableNameFromStatement(payload.Statement)
if err != nil {
return true, nil, fmt.Errorf("failed to parse table name from statement, error: %w", err)
}
diff --git a/server/task_executor_schema_update_ghost_sync.go b/server/task_executor_schema_update_ghost_sync.go
index ceac76c062..2f518bf0ed 100644
--- a/server/task_executor_schema_update_ghost_sync.go
+++ b/server/task_executor_schema_update_ghost_sync.go
@@ -52,7 +52,9 @@ func getPostponeFlagFilename(taskID int, databaseID int, databaseName string, ta
}
func getTableNameFromStatement(statement string) (string, error) {
- statement = strings.TrimSpace(statement)
+ // Trim the statement for the parser.
+ // This in effect removes all leading and trailing spaces, substitute multiple spaces with one.
+ statement = strings.Join(strings.Fields(statement), " ")
parser := ghostsql.NewParserFromAlterStatement(statement)
if !parser.HasExplicitTable() {
return "", fmt.Errorf("failed to parse table name from statement, statement: %v", statement)
@@ -90,6 +92,7 @@ func newMigrationContext(config ghostConfig) (*base.MigrationContext, error) {
cutoverLockTimoutSeconds = 3
exponentialBackoffMaxInterval = 64
)
+ statement := strings.Join(strings.Fields(config.alterStatement), " ")
migrationContext := base.NewMigrationContext()
migrationContext.InspectorConnectionConfig.Key.Hostname = config.host
port := 3306
@@ -105,7 +108,7 @@ func newMigrationContext(config ghostConfig) (*base.MigrationContext, error) {
migrationContext.CliPassword = config.password
migrationContext.DatabaseName = config.database
migrationContext.OriginalTableName = config.table
- migrationContext.AlterStatement = config.alterStatement
+ migrationContext.AlterStatement = statement
migrationContext.Noop = config.noop
migrationContext.ReplicaServerId = config.serverID
// set defaults
diff --git a/tests/ghost_test.go b/tests/ghost_test.go
index f323252e6f..df18f79052 100644
--- a/tests/ghost_test.go
+++ b/tests/ghost_test.go
@@ -8,15 +8,40 @@ import (
"encoding/json"
"fmt"
"strconv"
+ "strings"
"testing"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/resources/mysql"
+ ghostsql "github.com/github/gh-ost/go/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/require"
)
+func TestGhostParser(t *testing.T) {
+ t.Parallel()
+ a := require.New(t)
+ const statement = `
+ ALTER TABLE
+ test
+ ADD
+ COLUMN ghost_play_2 int;
+ `
+ t.Run("fail to parse", func(t *testing.T) {
+ t.Parallel()
+ parser := ghostsql.NewParserFromAlterStatement(statement)
+ a.Equal(false, parser.HasExplicitTable())
+ })
+ t.Run("succeed to parse", func(t *testing.T) {
+ t.Parallel()
+ s := strings.Join(strings.Fields(statement), " ")
+ parser := ghostsql.NewParserFromAlterStatement(s)
+ a.Equal(true, parser.HasExplicitTable())
+ a.Equal("test", parser.GetExplicitTable())
+ })
+}
+
func TestGhostSchemaUpdate(t *testing.T) {
const (
databaseName = "testGhostSchemaUpdate"