Skip to content

Commit 3590d72

Browse files
author
Paul Rogers
committed
fix(MigrateDumpCommand::reorderMigrationRows): Correct rushed implementation of sorting by version timestamp.
1 parent 192e89e commit 3590d72

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/Commands/MigrateDumpCommand.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ public function handle()
5656
$this->info('Dumped schema');
5757
}
5858

59-
private static function reorderMigrationRows(array $output) : array
59+
public static function reorderMigrationRows(array $output) : array
6060
{
6161
if (config('migration-snapshot.reorder')) {
6262
$reordered = [];
63-
$new_id = 1;
6463
foreach ($output as $line) {
6564
// Extract parts of "INSERT ... VALUES ([id],'[ver]',[batch])
6665
// where version begins with "YYYY_MM_DD_HHMMSS".
@@ -76,9 +75,14 @@ private static function reorderMigrationRows(array $output) : array
7675
}
7776
// Reassemble parts with new values and index by timestamp of
7877
// version string to sort.
79-
$reordered[$m[2]] = "$m[1]($new_id,'$m[2]$m[3],0);";
80-
$new_id += 1;
78+
$reordered[$m[2]] = "$m[1](/*NEWID*/,'$m[2]$m[3],0);";
8179
}
80+
ksort($reordered);
81+
$reordered = array_values($reordered);
82+
foreach ($reordered as $index => &$line) {
83+
$line = str_replace('/*NEWID*/', $index + 1, $line);
84+
}
85+
8286
return $reordered;
8387
}
8488

tests/Mysql/MigrateDumpTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33

44
namespace OrisIntel\MigrationSnapshot\Tests\Mysql;
55

6+
use OrisIntel\MigrationSnapshot\Commands\MigrateDumpCommand;
67
use OrisIntel\MigrationSnapshot\Tests\TestCase;
78

89
class MigrateDumpTest extends TestCase
910
{
11+
protected function getEnvironmentSetUp($app)
12+
{
13+
$app['config']->set('migration-snapshot.reorder', true);
14+
}
15+
1016
public function test_handle()
1117
{
1218
$this->createTestTablesWithoutMigrate();
@@ -19,4 +25,21 @@ public function test_handle()
1925
$this->assertContains('INSERT INTO `migrations`', $result_sql);
2026
$this->assertNotContains(' AUTO_INCREMENT=', $result_sql);
2127
}
22-
}
28+
29+
public function test_reorderMigrationRows()
30+
{
31+
$output = [
32+
"INSERT INTO migrations VALUES (1,'0001_01_01_000001_one',1);",
33+
"INSERT INTO migrations VALUES (2,'0001_01_01_000003_three',2);",
34+
"INSERT INTO migrations VALUES (3,'0001_01_01_000002_two',3);",
35+
];
36+
$reordered = array_values(
37+
MigrateDumpCommand::reorderMigrationRows($output)
38+
);
39+
$this->assertEquals([
40+
"INSERT INTO migrations VALUES (1,'0001_01_01_000001_one',0);",
41+
"INSERT INTO migrations VALUES (2,'0001_01_01_000002_two',0);",
42+
"INSERT INTO migrations VALUES (3,'0001_01_01_000003_three',0);",
43+
], $reordered);
44+
}
45+
}

tests/Sqlite/MigrateDumpTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ public function test_handle()
1616
$this->assertRegExp('/CREATE TABLE( IF NOT EXISTS)? "test_ms" /', $result_sql);
1717
$this->assertRegExp('/INSERT INTO "?migrations"? /', $result_sql);
1818
}
19-
}
19+
}

0 commit comments

Comments
 (0)