@@ -302,6 +302,7 @@ func (suite *ApplierTestSuite) SetupSuite() {
302302
303303 // Second database & connection for move-tables tests:
304304 _ , err = suite .db .ExecContext (ctx , fmt .Sprintf ("CREATE DATABASE IF NOT EXISTS %s" , testMysqlDatabaseOther ))
305+ suite .Require ().NoError (err )
305306 otherConf := drivermysql .NewConfig ()
306307 otherConf .DBName = testMysqlDatabaseOther
307308 otherConf .User = testMysqlUser
@@ -1789,6 +1790,59 @@ func (suite *ApplierTestSuite) TestApplyIterationMoveTableCopyQueries() {
17891790 suite .Require ().Equal ("2025-12-31 23:59:59" , results [2 ].createdAt )
17901791}
17911792
1793+ func (suite * ApplierTestSuite ) TestApplyIterationMoveTableCopyQueriesNoRows () {
1794+ ctx := context .Background ()
1795+ var err error
1796+
1797+ _ , err = suite .db .ExecContext (ctx , fmt .Sprintf ("CREATE TABLE %s (id INT NOT NULL, name VARCHAR(50), created_at DATETIME NOT NULL, PRIMARY KEY(id));" , getTestTableName ()))
1798+ suite .Require ().NoError (err )
1799+ _ , err = suite .otherDB .ExecContext (ctx , fmt .Sprintf ("CREATE TABLE %s (id INT NOT NULL, name VARCHAR(50), created_at DATETIME NOT NULL, PRIMARY KEY(id));" , getTestOtherTableName ()))
1800+ suite .Require ().NoError (err )
1801+ _ , err = suite .db .ExecContext (ctx , fmt .Sprintf ("INSERT INTO %s (id, name, created_at) VALUES (1, 'alice', '2024-01-15 10:30:00'), (2, 'bob', '2024-06-20 14:45:00'), (3, 'carol', '2025-12-31 23:59:59');" , getTestTableName ()))
1802+ suite .Require ().NoError (err )
1803+
1804+ connectionConfig , err := getTestConnectionConfig (ctx , suite .mysqlContainer )
1805+ suite .Require ().NoError (err )
1806+
1807+ migrationContext := newTestMigrationContext ()
1808+ migrationContext .ApplierConnectionConfig = connectionConfig
1809+ migrationContext .MoveTables .ConnectionConfig = connectionConfig
1810+ migrationContext .SetConnectionConfig ("innodb" )
1811+ migrationContext .OriginalTableColumns = sql .NewColumnList ([]string {"id" , "name" , "created_at" })
1812+ migrationContext .SharedColumns = sql .NewColumnList ([]string {"id" , "name" , "created_at" })
1813+ migrationContext .MappedSharedColumns = sql .NewColumnList ([]string {"id" , "name" , "created_at" })
1814+ migrationContext .UniqueKey = & sql.UniqueKey {
1815+ Name : "PRIMARY" ,
1816+ Columns : * sql .NewColumnList ([]string {"id" }),
1817+ }
1818+ migrationContext .MoveTables .TableNames = []string {testMysqlTableName }
1819+ migrationContext .MoveTables .TargetDatabase = testMysqlDatabaseOther
1820+
1821+ applier := NewApplier (migrationContext )
1822+ applier .prepareQueries ()
1823+ defer applier .Teardown ()
1824+
1825+ err = applier .InitDBConnections ()
1826+ suite .Require ().NoError (err )
1827+
1828+ // Point the iteration range at a key range that contains no rows so the
1829+ // SELECT returns an empty result set and the INSERT is skipped.
1830+ migrationContext .MigrationIterationRangeMinValues = sql .ToColumnValues ([]interface {}{100 })
1831+ migrationContext .MigrationIterationRangeMaxValues = sql .ToColumnValues ([]interface {}{200 })
1832+
1833+ chunkSize , rowsAffected , duration , err := applier .ApplyIterationMoveTableCopyQueries ()
1834+ suite .Require ().NoError (err )
1835+ suite .Require ().Equal (int64 (0 ), rowsAffected )
1836+ suite .Require ().Equal (int64 (1000 ), chunkSize )
1837+ suite .Require ().Greater (duration , time .Duration (0 ))
1838+
1839+ // Verify no rows were copied to the target table.
1840+ var count int
1841+ err = suite .otherDB .QueryRowContext (ctx , "SELECT COUNT(*) FROM " + getTestOtherTableName ()).Scan (& count )
1842+ suite .Require ().NoError (err )
1843+ suite .Require ().Equal (0 , count )
1844+ }
1845+
17921846func TestApplier (t * testing.T ) {
17931847 if testing .Short () {
17941848 t .Skip ("skipping applier test suite in short mode" )
0 commit comments