Skip to content

Commit 168676b

Browse files
author
Shlomi Noach
committed
ghost/changelog/del table names support max characters limit
1 parent dd10b52 commit 168676b

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

go/base/context.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,18 @@ func GetMigrationContext() *MigrationContext {
243243
return context
244244
}
245245

246+
func getSafeTableName(baseName string, suffix string) string {
247+
name := fmt.Sprintf("_%s_%s", baseName, suffix)
248+
if len(name) <= mysql.MaxTableNameLength {
249+
return name
250+
}
251+
extraCharacters := len(name) - mysql.MaxTableNameLength
252+
return fmt.Sprintf("_%s_%s", baseName[0:len(baseName)-extraCharacters], suffix)
253+
}
254+
246255
// GetGhostTableName generates the name of ghost table, based on original table name
247256
func (this *MigrationContext) GetGhostTableName() string {
248-
return fmt.Sprintf("_%s_gho", this.OriginalTableName)
257+
return getSafeTableName(this.OriginalTableName, "gho")
249258
}
250259

251260
// GetOldTableName generates the name of the "old" table, into which the original table is renamed.
@@ -255,14 +264,14 @@ func (this *MigrationContext) GetOldTableName() string {
255264
timestamp := fmt.Sprintf("%d%02d%02d%02d%02d%02d",
256265
t.Year(), t.Month(), t.Day(),
257266
t.Hour(), t.Minute(), t.Second())
258-
return fmt.Sprintf("_%s_%s_del", this.OriginalTableName, timestamp)
267+
return getSafeTableName(this.OriginalTableName, fmt.Sprintf("%s_del", timestamp))
259268
}
260-
return fmt.Sprintf("_%s_del", this.OriginalTableName)
269+
return getSafeTableName(this.OriginalTableName, "del")
261270
}
262271

263272
// GetChangelogTableName generates the name of changelog table, based on original table name
264273
func (this *MigrationContext) GetChangelogTableName() string {
265-
return fmt.Sprintf("_%s_ghc", this.OriginalTableName)
274+
return getSafeTableName(this.OriginalTableName, "ghc")
266275
}
267276

268277
// GetVoluntaryLockName returns a name of a voluntary lock to be used throughout

go/base/context_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2016 GitHub Inc.
3+
See https://github.com/github/gh-ost/blob/master/LICENSE
4+
*/
5+
6+
package base
7+
8+
import (
9+
"testing"
10+
11+
"github.com/outbrain/golib/log"
12+
test "github.com/outbrain/golib/tests"
13+
)
14+
15+
func init() {
16+
log.SetLevel(log.ERROR)
17+
}
18+
19+
func TestGetTableNames(t *testing.T) {
20+
context = newMigrationContext()
21+
{
22+
context.OriginalTableName = "some_table"
23+
test.S(t).ExpectEquals(context.GetOldTableName(), "_some_table_del")
24+
test.S(t).ExpectEquals(context.GetGhostTableName(), "_some_table_gho")
25+
test.S(t).ExpectEquals(context.GetChangelogTableName(), "_some_table_ghc")
26+
}
27+
{
28+
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890"
29+
test.S(t).ExpectEquals(context.GetOldTableName(), "_a1234567890123456789012345678901234567890123456789012345678_del")
30+
test.S(t).ExpectEquals(context.GetGhostTableName(), "_a1234567890123456789012345678901234567890123456789012345678_gho")
31+
test.S(t).ExpectEquals(context.GetChangelogTableName(), "_a1234567890123456789012345678901234567890123456789012345678_ghc")
32+
}
33+
{
34+
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890123"
35+
oldTableName := context.GetOldTableName()
36+
test.S(t).ExpectEquals(oldTableName, "_a1234567890123456789012345678901234567890123456789012345678_del")
37+
}
38+
}

0 commit comments

Comments
 (0)