Migrate executes the complete migration logic. This is *the* major gh-ost function.
()
| 467 | |
| 468 | // Migrate executes the complete migration logic. This is *the* major gh-ost function. |
| 469 | func (mgtr *Migrator) Migrate() (err error) { |
| 470 | mgtr.migrationContext.Log.Infof("Migrating %s.%s", sql.EscapeName(mgtr.migrationContext.DatabaseName), sql.EscapeName(mgtr.migrationContext.OriginalTableName)) |
| 471 | mgtr.migrationContext.StartTime = time.Now() |
| 472 | mgtr.migrationContext.SetLastHeartbeatOnChangelogTime(mgtr.migrationContext.StartTime) |
| 473 | |
| 474 | // Ensure context is cancelled on exit (cleanup) |
| 475 | defer mgtr.migrationContext.CancelContext() |
| 476 | |
| 477 | if mgtr.migrationContext.Hostname, err = os.Hostname(); err != nil { |
| 478 | return err |
| 479 | } |
| 480 | |
| 481 | go mgtr.listenOnPanicAbort() |
| 482 | |
| 483 | if err := mgtr.hooksExecutor.OnStartup(); err != nil { |
| 484 | return err |
| 485 | } |
| 486 | if err := mgtr.parser.ParseAlterStatement(mgtr.migrationContext.AlterStatement); err != nil { |
| 487 | return err |
| 488 | } |
| 489 | if err := mgtr.validateAlterStatement(); err != nil { |
| 490 | return err |
| 491 | } |
| 492 | |
| 493 | // After this point, we'll need to teardown anything that's been started |
| 494 | // so we don't leave things hanging around |
| 495 | defer mgtr.teardown() |
| 496 | |
| 497 | if err := mgtr.initiateInspector(); err != nil { |
| 498 | return err |
| 499 | } |
| 500 | if err := mgtr.checkAbort(); err != nil { |
| 501 | return err |
| 502 | } |
| 503 | // If we are resuming, we will initiateStreaming later when we know |
| 504 | // the binlog coordinates to resume streaming from. |
| 505 | // If not resuming, the streamer must be initiated before the applier, |
| 506 | // so that the "GhostTableMigrated" event gets processed. |
| 507 | if !mgtr.migrationContext.Resume { |
| 508 | if err := mgtr.initiateStreaming(); err != nil { |
| 509 | return err |
| 510 | } |
| 511 | if err := mgtr.checkAbort(); err != nil { |
| 512 | return err |
| 513 | } |
| 514 | } |
| 515 | if err := mgtr.initiateApplier(); err != nil { |
| 516 | return err |
| 517 | } |
| 518 | if err := mgtr.checkAbort(); err != nil { |
| 519 | return err |
| 520 | } |
| 521 | if err := mgtr.createFlagFiles(); err != nil { |
| 522 | return err |
| 523 | } |
| 524 | // In MySQL 8.0 (and possibly earlier) some DDL statements can be applied instantly. |
| 525 | // Attempt to do this if AttemptInstantDDL is set. |
| 526 | if mgtr.migrationContext.AttemptInstantDDL { |