@@ -335,6 +335,9 @@ func (this *Migrator) Migrate() (err error) {
335335 return err
336336 }
337337
338+ if err := this .finalCleanup (); err != nil {
339+ return nil
340+ }
338341 log .Infof ("Done migrating %s.%s" , sql .EscapeName (this .migrationContext .DatabaseName ), sql .EscapeName (this .migrationContext .OriginalTableName ))
339342 return nil
340343}
@@ -381,26 +384,10 @@ func (this *Migrator) stopWritesAndCompleteMigration() (err error) {
381384 }); err != nil {
382385 return err
383386 }
384- if err := this .dropOldTableIfRequired (); err != nil {
385- return err
386- }
387387 }
388388 return
389389}
390390
391- func (this * Migrator ) dropOldTableIfRequired () (err error ) {
392- if ! this .migrationContext .OkToDropTable {
393- return nil
394- }
395- dropTableFunc := func () error {
396- return this .applier .dropTable (this .migrationContext .GetOldTableName ())
397- }
398- if err := this .retryOperation (dropTableFunc ); err != nil {
399- return err
400- }
401- return nil
402- }
403-
404391// Inject the "AllEventsUpToLockProcessed" state hint, wait for it to appear in the binary logs,
405392// make sure the queue is drained.
406393func (this * Migrator ) waitForEventsUpToLock () (err error ) {
@@ -435,9 +422,6 @@ func (this *Migrator) stopWritesAndCompleteMigrationOnMasterQuickAndBumpy() (err
435422 if err := this .retryOperation (this .applier .UnlockTables ); err != nil {
436423 return err
437424 }
438- if err := this .dropOldTableIfRequired (); err != nil {
439- return err
440- }
441425
442426 lockAndRenameDuration := this .migrationContext .RenameTablesEndTime .Sub (this .migrationContext .LockTablesStartTime )
443427 renameDuration := this .migrationContext .RenameTablesEndTime .Sub (this .migrationContext .RenameTablesStartTime )
@@ -520,6 +504,7 @@ func (this *Migrator) stopWritesAndCompleteMigrationOnReplica() (err error) {
520504
521505 this .waitForEventsUpToLock ()
522506
507+ this .printMigrationStatusHint ()
523508 log .Info ("Table duplicated with new schema. Am not touching the original table. Replication is stopped. You may now compare the two tables to gain trust into this tool's operation" )
524509 return nil
525510}
@@ -569,6 +554,17 @@ func (this *Migrator) initiateStatus() error {
569554 return nil
570555}
571556
557+ func (this * Migrator ) printMigrationStatusHint () {
558+ hint := fmt .Sprintf ("# Migrating %s.%s; Ghost table is %s.%s; migration started at %+v" ,
559+ sql .EscapeName (this .migrationContext .DatabaseName ),
560+ sql .EscapeName (this .migrationContext .OriginalTableName ),
561+ sql .EscapeName (this .migrationContext .DatabaseName ),
562+ sql .EscapeName (this .migrationContext .GetGhostTableName ()),
563+ this .migrationContext .StartTime .Format (time .RubyDate ),
564+ )
565+ fmt .Println (hint )
566+ }
567+
572568func (this * Migrator ) printStatus () {
573569 elapsedTime := this .migrationContext .ElapsedTime ()
574570 elapsedSeconds := int64 (elapsedTime .Seconds ())
@@ -580,16 +576,9 @@ func (this *Migrator) printStatus() {
580576 }
581577
582578 // Before status, let's see if we should print a nice reminder for what exactly we're doing here.
583- shouldPrintCourtesyReminder := (elapsedSeconds % 600 == 0 )
584- if shouldPrintCourtesyReminder {
585- courtesyReminder := fmt .Sprintf ("# Migrating %s.%s; Ghost table is %s.%s; migration started at %+v" ,
586- sql .EscapeName (this .migrationContext .DatabaseName ),
587- sql .EscapeName (this .migrationContext .OriginalTableName ),
588- sql .EscapeName (this .migrationContext .DatabaseName ),
589- sql .EscapeName (this .migrationContext .GetGhostTableName ()),
590- this .migrationContext .StartTime .Format (time .RubyDate ),
591- )
592- fmt .Println (courtesyReminder )
579+ shouldPrintMigrationStatusHint := (elapsedSeconds % 600 == 0 )
580+ if shouldPrintMigrationStatusHint {
581+ this .printMigrationStatusHint ()
593582 }
594583
595584 var etaSeconds float64 = math .MaxFloat64
@@ -816,3 +805,20 @@ func (this *Migrator) executeWriteFuncs() error {
816805 }
817806 return nil
818807}
808+
809+ // finalCleanup takes actions at very end of migration, dropping tables etc.
810+ func (this * Migrator ) finalCleanup () error {
811+ if err := this .retryOperation (this .applier .DropChangelogTable ); err != nil {
812+ return err
813+ }
814+ if this .migrationContext .OkToDropTable && ! this .migrationContext .TestOnReplica {
815+ dropTableFunc := func () error {
816+ return this .applier .dropTable (this .migrationContext .GetOldTableName ())
817+ }
818+ if err := this .retryOperation (dropTableFunc ); err != nil {
819+ return err
820+ }
821+ }
822+
823+ return nil
824+ }
0 commit comments