@@ -335,6 +335,9 @@ func (this *Migrator) Migrate() (err error) {
335
335
return err
336
336
}
337
337
338
+ if err := this .finalCleanup (); err != nil {
339
+ return nil
340
+ }
338
341
log .Infof ("Done migrating %s.%s" , sql .EscapeName (this .migrationContext .DatabaseName ), sql .EscapeName (this .migrationContext .OriginalTableName ))
339
342
return nil
340
343
}
@@ -381,26 +384,10 @@ func (this *Migrator) stopWritesAndCompleteMigration() (err error) {
381
384
}); err != nil {
382
385
return err
383
386
}
384
- if err := this .dropOldTableIfRequired (); err != nil {
385
- return err
386
- }
387
387
}
388
388
return
389
389
}
390
390
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
-
404
391
// Inject the "AllEventsUpToLockProcessed" state hint, wait for it to appear in the binary logs,
405
392
// make sure the queue is drained.
406
393
func (this * Migrator ) waitForEventsUpToLock () (err error ) {
@@ -435,9 +422,6 @@ func (this *Migrator) stopWritesAndCompleteMigrationOnMasterQuickAndBumpy() (err
435
422
if err := this .retryOperation (this .applier .UnlockTables ); err != nil {
436
423
return err
437
424
}
438
- if err := this .dropOldTableIfRequired (); err != nil {
439
- return err
440
- }
441
425
442
426
lockAndRenameDuration := this .migrationContext .RenameTablesEndTime .Sub (this .migrationContext .LockTablesStartTime )
443
427
renameDuration := this .migrationContext .RenameTablesEndTime .Sub (this .migrationContext .RenameTablesStartTime )
@@ -520,6 +504,7 @@ func (this *Migrator) stopWritesAndCompleteMigrationOnReplica() (err error) {
520
504
521
505
this .waitForEventsUpToLock ()
522
506
507
+ this .printMigrationStatusHint ()
523
508
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" )
524
509
return nil
525
510
}
@@ -569,6 +554,17 @@ func (this *Migrator) initiateStatus() error {
569
554
return nil
570
555
}
571
556
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
+
572
568
func (this * Migrator ) printStatus () {
573
569
elapsedTime := this .migrationContext .ElapsedTime ()
574
570
elapsedSeconds := int64 (elapsedTime .Seconds ())
@@ -580,16 +576,9 @@ func (this *Migrator) printStatus() {
580
576
}
581
577
582
578
// 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 ()
593
582
}
594
583
595
584
var etaSeconds float64 = math .MaxFloat64
@@ -816,3 +805,20 @@ func (this *Migrator) executeWriteFuncs() error {
816
805
}
817
806
return nil
818
807
}
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