From 168676bd9d122cf5d3fcd33ae89ad2348db68b97 Mon Sep 17 00:00:00 2001 From: Shlomi Noach Date: Wed, 19 Jul 2017 15:14:03 +0300 Subject: [PATCH 1/2] ghost/changelog/del table names support max characters limit --- go/base/context.go | 17 +++++++++++++---- go/base/context_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 go/base/context_test.go diff --git a/go/base/context.go b/go/base/context.go index beb5475..723d9e8 100644 --- a/go/base/context.go +++ b/go/base/context.go @@ -243,9 +243,18 @@ func GetMigrationContext() *MigrationContext { return context } +func getSafeTableName(baseName string, suffix string) string { + name := fmt.Sprintf("_%s_%s", baseName, suffix) + if len(name) <= mysql.MaxTableNameLength { + return name + } + extraCharacters := len(name) - mysql.MaxTableNameLength + return fmt.Sprintf("_%s_%s", baseName[0:len(baseName)-extraCharacters], suffix) +} + // GetGhostTableName generates the name of ghost table, based on original table name func (this *MigrationContext) GetGhostTableName() string { - return fmt.Sprintf("_%s_gho", this.OriginalTableName) + return getSafeTableName(this.OriginalTableName, "gho") } // GetOldTableName generates the name of the "old" table, into which the original table is renamed. @@ -255,14 +264,14 @@ func (this *MigrationContext) GetOldTableName() string { timestamp := fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) - return fmt.Sprintf("_%s_%s_del", this.OriginalTableName, timestamp) + return getSafeTableName(this.OriginalTableName, fmt.Sprintf("%s_del", timestamp)) } - return fmt.Sprintf("_%s_del", this.OriginalTableName) + return getSafeTableName(this.OriginalTableName, "del") } // GetChangelogTableName generates the name of changelog table, based on original table name func (this *MigrationContext) GetChangelogTableName() string { - return fmt.Sprintf("_%s_ghc", this.OriginalTableName) + 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 new file mode 100644 index 0000000..ff2f65d --- /dev/null +++ b/go/base/context_test.go @@ -0,0 +1,38 @@ +/* + Copyright 2016 GitHub Inc. + See https://github.com/github/gh-ost/blob/master/LICENSE +*/ + +package base + +import ( + "testing" + + "github.com/outbrain/golib/log" + test "github.com/outbrain/golib/tests" +) + +func init() { + log.SetLevel(log.ERROR) +} + +func TestGetTableNames(t *testing.T) { + context = newMigrationContext() + { + context.OriginalTableName = "some_table" + test.S(t).ExpectEquals(context.GetOldTableName(), "_some_table_del") + test.S(t).ExpectEquals(context.GetGhostTableName(), "_some_table_gho") + test.S(t).ExpectEquals(context.GetChangelogTableName(), "_some_table_ghc") + } + { + context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890" + test.S(t).ExpectEquals(context.GetOldTableName(), "_a1234567890123456789012345678901234567890123456789012345678_del") + test.S(t).ExpectEquals(context.GetGhostTableName(), "_a1234567890123456789012345678901234567890123456789012345678_gho") + test.S(t).ExpectEquals(context.GetChangelogTableName(), "_a1234567890123456789012345678901234567890123456789012345678_ghc") + } + { + context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890123" + oldTableName := context.GetOldTableName() + test.S(t).ExpectEquals(oldTableName, "_a1234567890123456789012345678901234567890123456789012345678_del") + } +} From 39e73e37d0a9cd5556984cb9ecac56ed365b35d8 Mon Sep 17 00:00:00 2001 From: Shlomi Noach Date: Wed, 19 Jul 2017 15:22:46 +0300 Subject: [PATCH 2/2] testing context.TimestampOldTable --- go/base/context_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/go/base/context_test.go b/go/base/context_test.go index ff2f65d..b9e87d6 100644 --- a/go/base/context_test.go +++ b/go/base/context_test.go @@ -7,6 +7,7 @@ package base import ( "testing" + "time" "github.com/outbrain/golib/log" test "github.com/outbrain/golib/tests" @@ -35,4 +36,12 @@ func TestGetTableNames(t *testing.T) { oldTableName := context.GetOldTableName() test.S(t).ExpectEquals(oldTableName, "_a1234567890123456789012345678901234567890123456789012345678_del") } + { + context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890123" + context.TimestampOldTable = true + longForm := "Jan 2, 2006 at 3:04pm (MST)" + context.StartTime, _ = time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)") + oldTableName := context.GetOldTableName() + test.S(t).ExpectEquals(oldTableName, "_a1234567890123456789012345678901234567890123_20130203195400_del") + } }