Skip to content

Commit 83fb476

Browse files
committed
drop checkpoint table if --ok-to-drop-table
1 parent f9a27b5 commit 83fb476

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

go/logic/migrator.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,9 @@ func (this *Migrator) finalCleanup() error {
17191719
if err := this.retryOperation(this.applier.DropOldTable); err != nil {
17201720
return err
17211721
}
1722+
if err := this.retryOperation(this.applier.DropCheckpointTable); err != nil {
1723+
return err
1724+
}
17221725
} else {
17231726
if !this.migrationContext.Noop {
17241727
this.migrationContext.Log.Infof("Am not dropping old table because I want this operation to be as live as possible. If you insist I should do it, please add `--ok-to-drop-table` next time. But I prefer you do not. To drop the old table, issue:")

go/logic/migrator_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ func (suite *MigratorTestSuite) TearDownTest() {
334334
suite.Require().NoError(err)
335335
_, err = suite.db.ExecContext(ctx, "DROP TABLE IF EXISTS "+getTestGhostTableName())
336336
suite.Require().NoError(err)
337+
_, err = suite.db.ExecContext(ctx, "DROP TABLE IF EXISTS "+getTestRevertedTableName())
338+
suite.Require().NoError(err)
339+
_, err = suite.db.ExecContext(ctx, "DROP TABLE IF EXISTS "+getTestOldTableName())
337340
}
338341

339342
func (suite *MigratorTestSuite) TestMigrateEmpty() {
@@ -668,6 +671,57 @@ func (suite *MigratorTestSuite) TestCutOverLossDataCaseLockGhostBeforeRename() {
668671
suite.Require().Equal("CREATE TABLE `testing` (\n `id` int NOT NULL,\n `name` varchar(64) DEFAULT NULL,\n `foobar` varchar(255) DEFAULT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", createTableSQL)
669672
}
670673

674+
func (suite *MigratorTestSuite) TestRevertEmpty() {
675+
ctx := context.Background()
676+
677+
_, err := suite.db.ExecContext(ctx, fmt.Sprintf("CREATE TABLE %s (id INT PRIMARY KEY, s CHAR(32))", getTestTableName()))
678+
suite.Require().NoError(err)
679+
680+
var oldTableName string
681+
682+
// perform original migration
683+
connectionConfig, err := getTestConnectionConfig(ctx, suite.mysqlContainer)
684+
suite.Require().NoError(err)
685+
{
686+
migrationContext := newTestMigrationContext()
687+
migrationContext.ApplierConnectionConfig = connectionConfig
688+
migrationContext.InspectorConnectionConfig = connectionConfig
689+
migrationContext.SetConnectionConfig("innodb")
690+
migrationContext.AlterStatement = "ADD COLUMN newcol CHAR(32)"
691+
migrationContext.Checkpoint = true
692+
migrationContext.CheckpointIntervalSeconds = 10
693+
migrationContext.DropServeSocket = true
694+
migrationContext.InitiallyDropOldTable = true
695+
migrationContext.UseGTIDs = true
696+
697+
migrator := NewMigrator(migrationContext, "0.0.0")
698+
699+
err = migrator.Migrate()
700+
oldTableName = migrationContext.GetOldTableName()
701+
suite.Require().NoError(err)
702+
}
703+
704+
// revert the original migration
705+
{
706+
migrationContext := newTestMigrationContext()
707+
migrationContext.ApplierConnectionConfig = connectionConfig
708+
migrationContext.InspectorConnectionConfig = connectionConfig
709+
migrationContext.SetConnectionConfig("innodb")
710+
migrationContext.DropServeSocket = true
711+
migrationContext.UseGTIDs = true
712+
migrationContext.Revert = true
713+
migrationContext.OkToDropTable = true
714+
migrationContext.OldTableName = oldTableName
715+
716+
migrator := NewMigrator(migrationContext, "0.0.0")
717+
718+
err = migrator.Revert()
719+
oldTableName = migrationContext.GetOldTableName()
720+
suite.Require().NoError(err)
721+
}
722+
723+
}
724+
671725
func (suite *MigratorTestSuite) TestRevert() {
672726
ctx := context.Background()
673727

go/logic/test_utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ func getTestGhostTableName() string {
2828
return fmt.Sprintf("`%s`.`_%s_gho`", testMysqlDatabase, testMysqlTableName)
2929
}
3030

31+
func getTestRevertedTableName() string {
32+
return fmt.Sprintf("`%s`.`_%s_rev_del`", testMysqlDatabase, testMysqlTableName)
33+
}
34+
35+
func getTestOldTableName() string {
36+
return fmt.Sprintf("`%s`.`_%s_del`", testMysqlDatabase, testMysqlTableName)
37+
}
38+
3139
func getTestConnectionConfig(ctx context.Context, container testcontainers.Container) (*mysql.ConnectionConfig, error) {
3240
host, err := container.Host(ctx)
3341
if err != nil {

0 commit comments

Comments
 (0)