Merge pull request #451 from github/long-table-names

ghost/changelog/del table names support max characters limit
This commit is contained in:
Shlomi Noach 2017-07-20 16:47:11 +03:00 committed by GitHub
commit d05f0cebc3
2 changed files with 60 additions and 4 deletions

View File

@ -243,9 +243,18 @@ func GetMigrationContext() *MigrationContext {
return context 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 // GetGhostTableName generates the name of ghost table, based on original table name
func (this *MigrationContext) GetGhostTableName() string { 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. // 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", timestamp := fmt.Sprintf("%d%02d%02d%02d%02d%02d",
t.Year(), t.Month(), t.Day(), t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second()) 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 // GetChangelogTableName generates the name of changelog table, based on original table name
func (this *MigrationContext) GetChangelogTableName() string { 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 // GetVoluntaryLockName returns a name of a voluntary lock to be used throughout

47
go/base/context_test.go Normal file
View File

@ -0,0 +1,47 @@
/*
Copyright 2016 GitHub Inc.
See https://github.com/github/gh-ost/blob/master/LICENSE
*/
package base
import (
"testing"
"time"
"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")
}
{
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")
}
}