Skip to content

Commit 8c45ee1

Browse files
committed
Add CLI flags for move-tables
1 parent e62debe commit 8c45ee1

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

go/base/context.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,16 @@ type MigrationContext struct {
271271
SkipMetadataLockCheck bool
272272
IsOpenMetadataLockInstruments bool
273273

274+
// move tables:
275+
MoveTables struct {
276+
TableNames []string // List of table names to be moved.
277+
TargetHost string // Target hostname for the move. This must be a primary/writable host.
278+
TargetPort int // Target MySQL port for the move.
279+
TargetUser string // Target username for the move. If not specified, it will default to the source user.
280+
TargetPass string // Target password for the move. If not specified, it will default to the source password.
281+
TargetDatabase string // Target database name for the move. If not specified, it will default to the source database name.
282+
}
283+
274284
Log Logger
275285
}
276286

go/cmd/gh-ost/main.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"os/signal"
1414
"regexp"
15+
"strings"
1516
"syscall"
1617

1718
"github.com/github/gh-ost/go/base"
@@ -164,8 +165,16 @@ func main() {
164165
version := flag.Bool("version", false, "Print version & exit")
165166
checkFlag := flag.Bool("check-flag", false, "Check if another flag exists/supported. This allows for cross-version scripting. Exits with 0 when all additional provided flags exist, nonzero otherwise. You must provide (dummy) values for flags that require a value. Example: gh-ost --check-flag --cut-over-lock-timeout-seconds --nice-ratio 0")
166167
flag.StringVar(&migrationContext.ForceTmpTableName, "force-table-names", "", "table name prefix to be used on the temporary tables")
167-
flag.CommandLine.SetOutput(os.Stdout)
168168

169+
// move tables flags
170+
moveTables := flag.String("move-tables", "", "Comma delimited list of tables to move. e.g. 'table1,table2,table3'. This is a special mode that allows you to move tables between database clusters. This mode is mutually exclusive with --alter, --table, --test-on-replica, --migrate-on-replica, --execute-on-replica, and --revert.")
171+
flag.StringVar(&migrationContext.MoveTables.TargetHost, "target-host", "", "Target MySQL hostname for --move-tables mode. Must be specified if --move-tables is specified.")
172+
flag.IntVar(&migrationContext.MoveTables.TargetPort, "target-port", 3306, "Target MySQL port for --move-tables mode. Defaults to 3306.")
173+
flag.StringVar(&migrationContext.MoveTables.TargetUser, "target-user", "", "Target MySQL username for --move-tables mode. If not provided, uses the same user as the source connection")
174+
flag.StringVar(&migrationContext.MoveTables.TargetPass, "target-password", "", "Target MySQL password for --move-tables mode. If not provided, uses the same password as the source connection")
175+
flag.StringVar(&migrationContext.MoveTables.TargetDatabase, "target-database", "", "Target MySQL database name for --move-tables mode. If not provided, uses the same database name as the source connection")
176+
177+
flag.CommandLine.SetOutput(os.Stdout)
169178
flag.Parse()
170179

171180
if *checkFlag {
@@ -320,6 +329,33 @@ func main() {
320329
migrationContext.Log.Warning("--exact-rowcount with --panic-on-warnings: row counts cannot be exact due to warning detection")
321330
}
322331

332+
if *moveTables != "" {
333+
if migrationContext.AlterStatement != "" {
334+
log.Fatal("--move-tables is mutually exclusive with --alter")
335+
}
336+
if migrationContext.OriginalTableName != "" {
337+
log.Fatal("--move-tables is mutually exclusive with --table")
338+
}
339+
if migrationContext.TestOnReplica {
340+
log.Fatal("--move-tables is mutually exclusive with --test-on-replica")
341+
}
342+
if migrationContext.MigrateOnReplica {
343+
log.Fatal("--move-tables is mutually exclusive with --migrate-on-replica")
344+
}
345+
if migrationContext.Revert {
346+
log.Fatal("--move-tables is mutually exclusive with --revert")
347+
}
348+
if migrationContext.MoveTables.TargetHost == "" {
349+
log.Fatal("--target-host must be specified when using --move-tables")
350+
}
351+
migrationContext.MoveTables.TableNames = strings.Split(*moveTables, ",")
352+
if len(migrationContext.MoveTables.TableNames) > 1 {
353+
// Future version will support moving multiple tables at the same time.
354+
// For now, we only support moving a single table at a time.
355+
log.Fatal("--move-tables currently supports only a single table")
356+
}
357+
}
358+
323359
switch *cutOver {
324360
case "atomic", "default", "":
325361
migrationContext.CutOverType = base.CutOverAtomic

0 commit comments

Comments
 (0)