Skip to content

Commit e0cf2ad

Browse files
author
Shlomi Noach
authored
Merge pull request #376 from github/old-table-name-timestamp
supporting --timestamp-old-table
2 parents 6b98151 + 91521f9 commit e0cf2ad

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

doc/command-line-flags.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,7 @@ See `approve-renamed-columns`
130130
### test-on-replica
131131

132132
Issue the migration on a replica; do not modify data on master. Useful for validating, testing and benchmarking. See [testing-on-replica](testing-on-replica.md)
133+
134+
### timestamp-old-table
135+
136+
Makes the _old_ table include a timestamp value. The _old_ table is what the original table is renamed to at the end of a successful migration. For example, if the table is `gh_ost_test`, then the _old_ table would normally be `_gh_ost_test_del`. With `--timestamp-old-table` it would be, for example, `_gh_ost_test_20170221103147_del`.

go/base/context.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ type MigrationContext struct {
121121
OkToDropTable bool
122122
InitiallyDropOldTable bool
123123
InitiallyDropGhostTable bool
124+
TimestampOldTable bool // Should old table name include a timestamp
124125
CutOverType CutOver
125126
ReplicaServerId uint
126127

@@ -234,11 +235,12 @@ func (this *MigrationContext) GetGhostTableName() string {
234235

235236
// GetOldTableName generates the name of the "old" table, into which the original table is renamed.
236237
func (this *MigrationContext) GetOldTableName() string {
237-
if this.TestOnReplica {
238-
return fmt.Sprintf("_%s_ght", this.OriginalTableName)
239-
}
240-
if this.MigrateOnReplica {
241-
return fmt.Sprintf("_%s_ghr", this.OriginalTableName)
238+
if this.TimestampOldTable {
239+
t := this.StartTime
240+
timestamp := fmt.Sprintf("%d%02d%02d%02d%02d%02d",
241+
t.Year(), t.Month(), t.Day(),
242+
t.Hour(), t.Minute(), t.Second())
243+
return fmt.Sprintf("_%s_%s_del", this.OriginalTableName, timestamp)
242244
}
243245
return fmt.Sprintf("_%s_del", this.OriginalTableName)
244246
}

go/cmd/gh-ost/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func main() {
7777
flag.BoolVar(&migrationContext.OkToDropTable, "ok-to-drop-table", false, "Shall the tool drop the old table at end of operation. DROPping tables can be a long locking operation, which is why I'm not doing it by default. I'm an online tool, yes?")
7878
flag.BoolVar(&migrationContext.InitiallyDropOldTable, "initially-drop-old-table", false, "Drop a possibly existing OLD table (remains from a previous run?) before beginning operation. Default is to panic and abort if such table exists")
7979
flag.BoolVar(&migrationContext.InitiallyDropGhostTable, "initially-drop-ghost-table", false, "Drop a possibly existing Ghost table (remains from a previous run?) before beginning operation. Default is to panic and abort if such table exists")
80+
flag.BoolVar(&migrationContext.TimestampOldTable, "timestamp-old-table", false, "Use a timestamp in old table name. This makes old table names unique and non conflicting cross migrations")
8081
cutOver := flag.String("cut-over", "atomic", "choose cut-over type (default|atomic, two-step)")
8182
flag.BoolVar(&migrationContext.ForceNamedCutOverCommand, "force-named-cut-over", false, "When true, the 'unpostpone|cut-over' interactive command must name the migrated table")
8283

go/logic/applier.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ func (this *Applier) ValidateOrDropExistingTables() error {
142142
return err
143143
}
144144
}
145+
if len(this.migrationContext.GetOldTableName()) > mysql.MaxTableNameLength {
146+
log.Fatalf("--timestamp-old-table defined, but resulting table name (%s) is too long (only %d characters allowed)", this.migrationContext.GetOldTableName(), mysql.MaxTableNameLength)
147+
}
148+
145149
if this.tableExists(this.migrationContext.GetOldTableName()) {
146150
return fmt.Errorf("Table %s already exists. Panicking. Use --initially-drop-old-table to force dropping it, though I really prefer that you drop it or rename it away", sql.EscapeName(this.migrationContext.GetOldTableName()))
147151
}

go/mysql/utils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/outbrain/golib/sqlutils"
1717
)
1818

19+
const MaxTableNameLength = 64
20+
1921
type ReplicationLagResult struct {
2022
Key InstanceKey
2123
Lag time.Duration

0 commit comments

Comments
 (0)