diff --git a/src/Commands/MigrateDumpCommand.php b/src/Commands/MigrateDumpCommand.php index d45f45b..0b56e43 100644 --- a/src/Commands/MigrateDumpCommand.php +++ b/src/Commands/MigrateDumpCommand.php @@ -56,11 +56,10 @@ public function handle() $this->info('Dumped schema'); } - private static function reorderMigrationRows(array $output) : array + public static function reorderMigrationRows(array $output) : array { if (config('migration-snapshot.reorder')) { $reordered = []; - $new_id = 1; foreach ($output as $line) { // Extract parts of "INSERT ... VALUES ([id],'[ver]',[batch]) // where version begins with "YYYY_MM_DD_HHMMSS". @@ -76,9 +75,14 @@ private static function reorderMigrationRows(array $output) : array } // Reassemble parts with new values and index by timestamp of // version string to sort. - $reordered[$m[2]] = "$m[1]($new_id,'$m[2]$m[3],0);"; - $new_id += 1; + $reordered[$m[2]] = "$m[1](/*NEWID*/,'$m[2]$m[3],0);"; } + ksort($reordered); + $reordered = array_values($reordered); + foreach ($reordered as $index => &$line) { + $line = str_replace('/*NEWID*/', $index + 1, $line); + } + return $reordered; } diff --git a/tests/Mysql/MigrateDumpTest.php b/tests/Mysql/MigrateDumpTest.php index d4ed005..7ac6d3b 100644 --- a/tests/Mysql/MigrateDumpTest.php +++ b/tests/Mysql/MigrateDumpTest.php @@ -3,10 +3,16 @@ namespace OrisIntel\MigrationSnapshot\Tests\Mysql; +use OrisIntel\MigrationSnapshot\Commands\MigrateDumpCommand; use OrisIntel\MigrationSnapshot\Tests\TestCase; class MigrateDumpTest extends TestCase { + protected function getEnvironmentSetUp($app) + { + $app['config']->set('migration-snapshot.reorder', true); + } + public function test_handle() { $this->createTestTablesWithoutMigrate(); @@ -19,4 +25,21 @@ public function test_handle() $this->assertContains('INSERT INTO `migrations`', $result_sql); $this->assertNotContains(' AUTO_INCREMENT=', $result_sql); } -} \ No newline at end of file + + public function test_reorderMigrationRows() + { + $output = [ + "INSERT INTO migrations VALUES (1,'0001_01_01_000001_one',1);", + "INSERT INTO migrations VALUES (2,'0001_01_01_000003_three',2);", + "INSERT INTO migrations VALUES (3,'0001_01_01_000002_two',3);", + ]; + $reordered = array_values( + MigrateDumpCommand::reorderMigrationRows($output) + ); + $this->assertEquals([ + "INSERT INTO migrations VALUES (1,'0001_01_01_000001_one',0);", + "INSERT INTO migrations VALUES (2,'0001_01_01_000002_two',0);", + "INSERT INTO migrations VALUES (3,'0001_01_01_000003_three',0);", + ], $reordered); + } +} diff --git a/tests/Sqlite/MigrateDumpTest.php b/tests/Sqlite/MigrateDumpTest.php index bab66a0..214a55f 100644 --- a/tests/Sqlite/MigrateDumpTest.php +++ b/tests/Sqlite/MigrateDumpTest.php @@ -16,4 +16,4 @@ public function test_handle() $this->assertRegExp('/CREATE TABLE( IF NOT EXISTS)? "test_ms" /', $result_sql); $this->assertRegExp('/INSERT INTO "?migrations"? /', $result_sql); } -} \ No newline at end of file +}