Skip to content

Commit b538d5b

Browse files
Merge pull request #1705 from github/zacharysierakowski/move-tables-1.2
Move Tables: 1.2 skip-tables and run overall move tables flow except cutover (#8206)
2 parents 7a9dca7 + cfb5d3d commit b538d5b

17 files changed

Lines changed: 1025 additions & 176 deletions

go/base/context.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,24 @@ func (mctx *MigrationContext) GetGhostTableName() string {
398398
}
399399
}
400400

401+
// GetTargetTableName generates the name of the target table, based on original table name and
402+
// the migration context (i.e. move-tables mode).
403+
func (mctx *MigrationContext) GetTargetTableName() string {
404+
if mctx.IsMoveTablesMode() {
405+
return mctx.MoveTables.TableNames[0]
406+
}
407+
return mctx.GetGhostTableName()
408+
}
409+
410+
// GetTargetDatabaseName fetches the name of the target database, which defaults to the original
411+
// database name unless we're in move-tables mode.
412+
func (mctx *MigrationContext) GetTargetDatabaseName() string {
413+
if mctx.IsMoveTablesMode() {
414+
return mctx.MoveTables.TargetDatabase
415+
}
416+
return mctx.DatabaseName
417+
}
418+
401419
// GetOldTableName generates the name of the "old" table, into which the original table is renamed.
402420
func (mctx *MigrationContext) GetOldTableName() string {
403421
var tableName string
@@ -939,11 +957,27 @@ func (mctx *MigrationContext) ApplyCredentials() {
939957
// Override
940958
mctx.InspectorConnectionConfig.Password = mctx.CliPassword
941959
}
960+
961+
if mctx.IsMoveTablesMode() {
962+
// Derive the applier config from the inspector config, but point it at
963+
// the target host and override credentials from the target CLI args.
964+
mctx.MoveTables.ConnectionConfig = mctx.InspectorConnectionConfig.DuplicateCredentials(mysql.InstanceKey{
965+
Hostname: mctx.MoveTables.TargetHost,
966+
Port: mctx.MoveTables.TargetPort,
967+
})
968+
mctx.MoveTables.ConnectionConfig.User = mctx.MoveTables.TargetUser
969+
mctx.MoveTables.ConnectionConfig.Password = mctx.MoveTables.TargetPass
970+
}
942971
}
943972

944973
func (mctx *MigrationContext) SetupTLS() error {
945974
if mctx.UseTLS {
946-
return mctx.InspectorConnectionConfig.UseTLS(mctx.TLSCACertificate, mctx.TLSCertificate, mctx.TLSKey, mctx.TLSAllowInsecure)
975+
if err := mctx.InspectorConnectionConfig.UseTLS(mctx.TLSCACertificate, mctx.TLSCertificate, mctx.TLSKey, mctx.TLSAllowInsecure); err != nil {
976+
return err
977+
}
978+
if mctx.IsMoveTablesMode() && mctx.MoveTables.ConnectionConfig != nil {
979+
return mctx.MoveTables.ConnectionConfig.UseTLS(mctx.TLSCACertificate, mctx.TLSCertificate, mctx.TLSKey, mctx.TLSAllowInsecure)
980+
}
947981
}
948982
return nil
949983
}

go/base/context_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,62 @@ func TestReadConfigFile(t *testing.T) {
216216
}
217217
}
218218

219+
func TestApplyCredentialsMoveTablesDerivesConnectionConfig(t *testing.T) {
220+
ctx := NewMigrationContext()
221+
ctx.MoveTables.TableNames = []string{"some_table"}
222+
ctx.MoveTables.TargetHost = "target-host"
223+
ctx.MoveTables.TargetPort = 3307
224+
ctx.MoveTables.TargetUser = "target-user"
225+
ctx.MoveTables.TargetPass = "target-pass"
226+
227+
ctx.InspectorConnectionConfig.Key.Hostname = "source-host"
228+
ctx.InspectorConnectionConfig.Key.Port = 3306
229+
ctx.InspectorConnectionConfig.User = "source-user"
230+
ctx.InspectorConnectionConfig.Password = "source-pass"
231+
ctx.InspectorConnectionConfig.Timeout = 12.5
232+
ctx.InspectorConnectionConfig.TransactionIsolation = "REPEATABLE-READ"
233+
ctx.InspectorConnectionConfig.Charset = "utf8mb4"
234+
235+
ctx.ApplyCredentials()
236+
237+
got := ctx.MoveTables.ConnectionConfig
238+
require.NotNil(t, got)
239+
require.Equal(t, "target-host", got.Key.Hostname)
240+
require.Equal(t, 3307, got.Key.Port)
241+
require.Equal(t, "target-user", got.User)
242+
require.Equal(t, "target-pass", got.Password)
243+
require.Equal(t, 12.5, got.Timeout)
244+
require.Equal(t, "REPEATABLE-READ", got.TransactionIsolation)
245+
require.Equal(t, "utf8mb4", got.Charset)
246+
require.NotNil(t, got.ImpliedKey)
247+
require.Equal(t, "target-host", got.ImpliedKey.Hostname)
248+
require.Equal(t, 3307, got.ImpliedKey.Port)
249+
}
250+
251+
func TestSetupTLSAppliesToMoveTablesConfig(t *testing.T) {
252+
ctx := NewMigrationContext()
253+
ctx.UseTLS = true
254+
ctx.TLSAllowInsecure = true
255+
ctx.MoveTables.TableNames = []string{"some_table"}
256+
ctx.MoveTables.TargetHost = "target-host"
257+
ctx.MoveTables.TargetPort = 3307
258+
ctx.MoveTables.TargetUser = "target-user"
259+
ctx.MoveTables.TargetPass = "target-pass"
260+
261+
ctx.InspectorConnectionConfig.Key.Hostname = "source-host"
262+
ctx.InspectorConnectionConfig.Key.Port = 3306
263+
ctx.InspectorConnectionConfig.User = "source-user"
264+
ctx.InspectorConnectionConfig.Password = "source-pass"
265+
266+
ctx.ApplyCredentials()
267+
require.NoError(t, ctx.SetupTLS())
268+
269+
require.NotNil(t, ctx.InspectorConnectionConfig.TLSConfig())
270+
require.NotNil(t, ctx.MoveTables.ConnectionConfig.TLSConfig())
271+
require.Equal(t, "source-host", ctx.InspectorConnectionConfig.TLSConfig().ServerName)
272+
require.Equal(t, "target-host", ctx.MoveTables.ConnectionConfig.TLSConfig().ServerName)
273+
}
274+
219275
func TestSetAbortError_StoresFirstError(t *testing.T) {
220276
ctx := NewMigrationContext()
221277

go/cmd/gh-ost/main.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/github/gh-ost/go/base"
2121
"github.com/github/gh-ost/go/logic"
2222
"github.com/github/gh-ost/go/metrics"
23+
"github.com/github/gh-ost/go/mysql"
2324
"github.com/github/gh-ost/go/sql"
2425
_ "github.com/go-sql-driver/mysql"
2526
"github.com/openark/golib/log"
@@ -233,12 +234,6 @@ func main() {
233234
migrationContext.Log.SetLevel(log.ERROR)
234235
}
235236

236-
if err := migrationContext.SetConnectionConfig(*storageEngine); err != nil {
237-
migrationContext.Log.Fatale(err)
238-
}
239-
240-
migrationContext.SetConnectionCharset(*charset)
241-
242237
if migrationContext.AlterStatement == "" && !migrationContext.Revert && *moveTables == "" {
243238
log.Fatal("--alter must be provided and statement must not be empty, or --revert must be used, or --move-tables must be used")
244239
}
@@ -379,6 +374,7 @@ func main() {
379374
// For now, we only support moving a single table at a time.
380375
log.Fatal("--move-tables currently supports only a single table")
381376
}
377+
382378
if migrationContext.MoveTables.TargetUser == "" {
383379
migrationContext.MoveTables.TargetUser = migrationContext.CliUser
384380
}
@@ -388,8 +384,15 @@ func main() {
388384
if migrationContext.MoveTables.TargetDatabase == "" {
389385
migrationContext.MoveTables.TargetDatabase = migrationContext.DatabaseName
390386
}
387+
migrationContext.MoveTables.ConnectionConfig = mysql.NewConnectionConfig()
391388
}
392389

390+
if err := migrationContext.SetConnectionConfig(*storageEngine); err != nil {
391+
migrationContext.Log.Fatale(err)
392+
}
393+
394+
migrationContext.SetConnectionCharset(*charset)
395+
393396
switch *cutOver {
394397
case "atomic", "default", "":
395398
migrationContext.CutOverType = base.CutOverAtomic

0 commit comments

Comments
 (0)