Skip to content

Commit

Permalink
fix(MigrateDumpCommand::reorderMigrationRows): Correct rushed impleme…
Browse files Browse the repository at this point in the history
…ntation of sorting by version timestamp.
  • Loading branch information
Paul Rogers committed Apr 30, 2019
1 parent 192e89e commit 3590d72
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/Commands/MigrateDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand All @@ -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;
}

Expand Down
25 changes: 24 additions & 1 deletion tests/Mysql/MigrateDumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -19,4 +25,21 @@ public function test_handle()
$this->assertContains('INSERT INTO `migrations`', $result_sql);
$this->assertNotContains(' AUTO_INCREMENT=', $result_sql);
}
}

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);
}
}
2 changes: 1 addition & 1 deletion tests/Sqlite/MigrateDumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 3590d72

Please sign in to comment.