Skip to content

Commit f0b90e2

Browse files
committed
Fix SQLite migrations table insert statements due to incorrect row offset and improve tests for insert statements
1 parent 8f5f5db commit f0b90e2

File tree

7 files changed

+34
-5
lines changed

7 files changed

+34
-5
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
}
2525
],
2626
"require": {
27+
"ext-mbstring": "*",
2728
"php": "~7.3|~8.0.0|~8.1.0|~8.2.0|~8.3.0|~8.4.0",
2829
"laravel/framework": "^8.0|^9.0|^10.0|^11.0|^12"
2930
},

src/Commands/MigrateDumpCommand.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,16 @@ private static function sqliteSchemaDump(array $db_config, string $schema_sql_pa
442442
}
443443

444444
if ($migrationsTable === $table) {
445-
$insert_rows = array_slice($output, 4, -1);
445+
$insert_rows = array_filter(
446+
$output,
447+
function ($line) {
448+
return 0 !== preg_match('/^\s*(INSERT INTO\s)/iu', $line)
449+
&& 0 < mb_strlen($line);
450+
}
451+
);
452+
446453
$sorted = self::reorderMigrationRows($insert_rows);
447-
array_splice($output, 4, -1, $sorted);
454+
array_splice($output, array_key_first($insert_rows), -1, $sorted);
448455
}
449456

450457
file_put_contents(

tests/Mysql/MigrateDumpTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public function test_handle()
1717
$this->assertFileExists($this->schemaSqlPath);
1818
$result_sql = file_get_contents($this->schemaSqlPath);
1919
$this->assertMatchesRegularExpression("/CREATE TABLE [\`\"]{$this->dbPrefix}test_ms[\`\"]/", $result_sql);
20-
$this->assertMatchesRegularExpression("/INSERT INTO [\`\"]{$this->dbPrefix}migrations[\`\"]/", $result_sql);
20+
$this->assertStringContainsString("INSERT INTO `{$this->dbPrefix}migrations` VALUES (1,'0000_00_00_000000_create_test_tables',0);", $result_sql);
21+
$this->assertStringContainsString("INSERT INTO `{$this->dbPrefix}migrations` VALUES (2,'0000_00_00_000001_second_migration_for_testing',0);", $result_sql);
2122
$this->assertStringNotContainsString(' AUTO_INCREMENT=', $result_sql);
2223
$last_character = mb_substr($result_sql, -1);
2324
$this->assertMatchesRegularExpression("/[\r\n]\z/mu", $last_character);

tests/Postgresql/MigrateDumpTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public function test_handle()
1818
$this->assertFileExists($this->schemaSqlPath);
1919
$result_sql = file_get_contents($this->schemaSqlPath);
2020
$this->assertMatchesRegularExpression("/CREATE TABLE (public\.)?{$this->dbPrefix}test_ms /", $result_sql);
21-
$this->assertMatchesRegularExpression("/INSERT INTO (public\.)?{$this->dbPrefix}migrations /", $result_sql);
21+
$this->assertStringContainsString("INSERT INTO public.{$this->dbPrefix}migrations VALUES (1,'0000_00_00_000000_create_test_tables',0);", $result_sql);
22+
$this->assertStringContainsString("INSERT INTO public.{$this->dbPrefix}migrations VALUES (2,'0000_00_00_000001_second_migration_for_testing',0);", $result_sql);
2223
$last_character = mb_substr($result_sql, -1);
2324
$this->assertMatchesRegularExpression("/[\r\n]\z/mu", $last_character);
2425
}

tests/Sqlite/MigrateDumpTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public function test_handle()
1414
$this->assertFileExists($this->schemaSqlPath);
1515
$result_sql = file_get_contents($this->schemaSqlPath);
1616
$this->assertMatchesRegularExpression("/CREATE TABLE( IF NOT EXISTS)? \"{$this->dbPrefix}test_ms\" /", $result_sql);
17-
$this->assertMatchesRegularExpression("/INSERT INTO \"?{$this->dbPrefix}migrations\"? /", $result_sql);
17+
$this->assertStringContainsString("INSERT INTO {$this->dbPrefix}migrations VALUES(1,'0000_00_00_000000_create_test_tables',0);", $result_sql);
18+
$this->assertStringContainsString("INSERT INTO {$this->dbPrefix}migrations VALUES(2,'0000_00_00_000001_second_migration_for_testing',0);", $result_sql);
1819
$last_character = mb_substr($result_sql, -1);
1920
$this->assertMatchesRegularExpression("/[\r\n]\z/mu", $last_character);
2021
}

tests/TestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,9 @@ protected function createTestTablesWithoutMigrate() : void
6565
'migration' => '0000_00_00_000000_create_test_tables',
6666
'batch' => 1,
6767
]);
68+
\DB::table('migrations')->insert([
69+
'migration' => '0000_00_00_000001_second_migration_for_testing',
70+
'batch' => 1,
71+
]);
6872
}
6973
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
class SecondMigrationForTesting extends \Illuminate\Database\Migrations\Migration
4+
{
5+
public function up()
6+
{
7+
// this migration just needs to exist to confirm migration table insert statements
8+
}
9+
10+
public function down()
11+
{
12+
//
13+
}
14+
}

0 commit comments

Comments
 (0)