From 843c74f960dfaa104bc0fcac26febac9a975f999 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 21 Jul 2017 15:37:57 -0400 Subject: [PATCH] Add a force-table-names command flag usage example: ``` gh-ost ... --force-table-names=migration_tbl ``` The force-table-names flag will receive a table name and use that name when creating the _del, _gho, _ghc tables. For instance for that example above, the tables would be: _migration_tbl_del _migration_tbl_gho _migration_tbl_ghc [fixes #399] --- go/base/context.go | 26 ++++++++++++++++++++++---- go/base/context_test.go | 8 ++++++++ go/cmd/gh-ost/main.go | 1 + 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/go/base/context.go b/go/base/context.go index 8077e22..fff040e 100644 --- a/go/base/context.go +++ b/go/base/context.go @@ -192,6 +192,7 @@ type MigrationContext struct { Iteration int64 MigrationIterationRangeMinValues *sql.ColumnValues MigrationIterationRangeMaxValues *sql.ColumnValues + ForceTmpTableName string recentBinlogCoordinates mysql.BinlogCoordinates @@ -253,25 +254,42 @@ func getSafeTableName(baseName string, suffix string) string { } // GetGhostTableName generates the name of ghost table, based on original table name +// or a given table name func (this *MigrationContext) GetGhostTableName() string { - return getSafeTableName(this.OriginalTableName, "gho") + if this.ForceTmpTableName != "" { + return getSafeTableName(this.ForceTmpTableName, "gho") + } else { + return getSafeTableName(this.OriginalTableName, "gho") + } } // GetOldTableName generates the name of the "old" table, into which the original table is renamed. func (this *MigrationContext) GetOldTableName() string { + var tableName string + if this.ForceTmpTableName != "" { + tableName = this.ForceTmpTableName + } else { + tableName = this.OriginalTableName + } + if this.TimestampOldTable { t := this.StartTime timestamp := fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) - return getSafeTableName(this.OriginalTableName, fmt.Sprintf("%s_del", timestamp)) + return getSafeTableName(tableName, fmt.Sprintf("%s_del", timestamp)) } - return getSafeTableName(this.OriginalTableName, "del") + return getSafeTableName(tableName, "del") } // GetChangelogTableName generates the name of changelog table, based on original table name +// or a given table name. func (this *MigrationContext) GetChangelogTableName() string { - return getSafeTableName(this.OriginalTableName, "ghc") + if this.ForceTmpTableName != "" { + return getSafeTableName(this.ForceTmpTableName, "ghc") + } else { + return getSafeTableName(this.OriginalTableName, "ghc") + } } // GetVoluntaryLockName returns a name of a voluntary lock to be used throughout diff --git a/go/base/context_test.go b/go/base/context_test.go index b9e87d6..90a8d21 100644 --- a/go/base/context_test.go +++ b/go/base/context_test.go @@ -44,4 +44,12 @@ func TestGetTableNames(t *testing.T) { oldTableName := context.GetOldTableName() test.S(t).ExpectEquals(oldTableName, "_a1234567890123456789012345678901234567890123_20130203195400_del") } + { + context.OriginalTableName = "foo_bar_baz" + context.ForceTmpTableName = "tmp" + context.TimestampOldTable = false + test.S(t).ExpectEquals(context.GetOldTableName(), "_tmp_del") + test.S(t).ExpectEquals(context.GetGhostTableName(), "_tmp_gho") + test.S(t).ExpectEquals(context.GetChangelogTableName(), "_tmp_ghc") + } } diff --git a/go/cmd/gh-ost/main.go b/go/cmd/gh-ost/main.go index a4f4f3e..7aabe81 100644 --- a/go/cmd/gh-ost/main.go +++ b/go/cmd/gh-ost/main.go @@ -120,6 +120,7 @@ func main() { help := flag.Bool("help", false, "Display usage") version := flag.Bool("version", false, "Print version & exit") checkFlag := flag.Bool("check-flag", false, "Check if another flag exists/supported. This allows for cross-version scripting. Exits with 0 when all additional provided flags exist, nonzero otherwise. You must provide (dummy) values for flags that require a value. Example: gh-ost --check-flag --cut-over-lock-timeout-seconds --nice-ratio 0") + flag.StringVar(&migrationContext.ForceTmpTableName, "force-table-names", "", "table name prefix to be used on the temporary tables") flag.Parse()