Skip to content

Commit e2c14f5

Browse files
author
Shlomi Noach
authored
Merge branch 'master' into dynamic-dml-batch-size
2 parents de0ebcb + d05f0ce commit e2c14f5

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
"time"
11+
12+
"github.com/outbrain/golib/log"
13+
test "github.com/outbrain/golib/tests"
14+
)
15+
16+
func init() {
17+
log.SetLevel(log.ERROR)
18+
}
19+
20+
func TestGetTableNames(t *testing.T) {
21+
context = newMigrationContext()
22+
{
23+
context.OriginalTableName = "some_table"
24+
test.S(t).ExpectEquals(context.GetOldTableName(), "_some_table_del")
25+
test.S(t).ExpectEquals(context.GetGhostTableName(), "_some_table_gho")
26+
test.S(t).ExpectEquals(context.GetChangelogTableName(), "_some_table_ghc")
27+
}
28+
{
29+
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890"
30+
test.S(t).ExpectEquals(context.GetOldTableName(), "_a1234567890123456789012345678901234567890123456789012345678_del")
31+
test.S(t).ExpectEquals(context.GetGhostTableName(), "_a1234567890123456789012345678901234567890123456789012345678_gho")
32+
test.S(t).ExpectEquals(context.GetChangelogTableName(), "_a1234567890123456789012345678901234567890123456789012345678_ghc")
33+
}
34+
{
35+
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890123"
36+
oldTableName := context.GetOldTableName()
37+
test.S(t).ExpectEquals(oldTableName, "_a1234567890123456789012345678901234567890123456789012345678_del")
38+
}
39+
{
40+
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890123"
41+
context.TimestampOldTable = true
42+
longForm := "Jan 2, 2006 at 3:04pm (MST)"
43+
context.StartTime, _ = time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
44+
oldTableName := context.GetOldTableName()
45+
test.S(t).ExpectEquals(oldTableName, "_a1234567890123456789012345678901234567890123_20130203195400_del")
46+
}
47+
}

0 commit comments

Comments
 (0)