From f0b90e2f3109388b8db4f33447ee698f0321fc66 Mon Sep 17 00:00:00 2001 From: Brian Stanley Date: Tue, 5 Aug 2025 12:59:11 -0400 Subject: [PATCH] Fix SQLite migrations table insert statements due to incorrect row offset and improve tests for insert statements --- composer.json | 1 + src/Commands/MigrateDumpCommand.php | 11 +++++++++-- tests/Mysql/MigrateDumpTest.php | 3 ++- tests/Postgresql/MigrateDumpTest.php | 3 ++- tests/Sqlite/MigrateDumpTest.php | 3 ++- tests/TestCase.php | 4 ++++ ...0_00_00_000001_second_migration_for_testing.php | 14 ++++++++++++++ 7 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 tests/migrations/setup/0000_00_00_000001_second_migration_for_testing.php diff --git a/composer.json b/composer.json index 7687fdd..2c652b6 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ } ], "require": { + "ext-mbstring": "*", "php": "~7.3|~8.0.0|~8.1.0|~8.2.0|~8.3.0|~8.4.0", "laravel/framework": "^8.0|^9.0|^10.0|^11.0|^12" }, diff --git a/src/Commands/MigrateDumpCommand.php b/src/Commands/MigrateDumpCommand.php index 30e185c..578badd 100644 --- a/src/Commands/MigrateDumpCommand.php +++ b/src/Commands/MigrateDumpCommand.php @@ -442,9 +442,16 @@ private static function sqliteSchemaDump(array $db_config, string $schema_sql_pa } if ($migrationsTable === $table) { - $insert_rows = array_slice($output, 4, -1); + $insert_rows = array_filter( + $output, + function ($line) { + return 0 !== preg_match('/^\s*(INSERT INTO\s)/iu', $line) + && 0 < mb_strlen($line); + } + ); + $sorted = self::reorderMigrationRows($insert_rows); - array_splice($output, 4, -1, $sorted); + array_splice($output, array_key_first($insert_rows), -1, $sorted); } file_put_contents( diff --git a/tests/Mysql/MigrateDumpTest.php b/tests/Mysql/MigrateDumpTest.php index 82a5dc3..2d2bdd5 100644 --- a/tests/Mysql/MigrateDumpTest.php +++ b/tests/Mysql/MigrateDumpTest.php @@ -17,7 +17,8 @@ public function test_handle() $this->assertFileExists($this->schemaSqlPath); $result_sql = file_get_contents($this->schemaSqlPath); $this->assertMatchesRegularExpression("/CREATE TABLE [\`\"]{$this->dbPrefix}test_ms[\`\"]/", $result_sql); - $this->assertMatchesRegularExpression("/INSERT INTO [\`\"]{$this->dbPrefix}migrations[\`\"]/", $result_sql); + $this->assertStringContainsString("INSERT INTO `{$this->dbPrefix}migrations` VALUES (1,'0000_00_00_000000_create_test_tables',0);", $result_sql); + $this->assertStringContainsString("INSERT INTO `{$this->dbPrefix}migrations` VALUES (2,'0000_00_00_000001_second_migration_for_testing',0);", $result_sql); $this->assertStringNotContainsString(' AUTO_INCREMENT=', $result_sql); $last_character = mb_substr($result_sql, -1); $this->assertMatchesRegularExpression("/[\r\n]\z/mu", $last_character); diff --git a/tests/Postgresql/MigrateDumpTest.php b/tests/Postgresql/MigrateDumpTest.php index 6714895..d884926 100644 --- a/tests/Postgresql/MigrateDumpTest.php +++ b/tests/Postgresql/MigrateDumpTest.php @@ -18,7 +18,8 @@ public function test_handle() $this->assertFileExists($this->schemaSqlPath); $result_sql = file_get_contents($this->schemaSqlPath); $this->assertMatchesRegularExpression("/CREATE TABLE (public\.)?{$this->dbPrefix}test_ms /", $result_sql); - $this->assertMatchesRegularExpression("/INSERT INTO (public\.)?{$this->dbPrefix}migrations /", $result_sql); + $this->assertStringContainsString("INSERT INTO public.{$this->dbPrefix}migrations VALUES (1,'0000_00_00_000000_create_test_tables',0);", $result_sql); + $this->assertStringContainsString("INSERT INTO public.{$this->dbPrefix}migrations VALUES (2,'0000_00_00_000001_second_migration_for_testing',0);", $result_sql); $last_character = mb_substr($result_sql, -1); $this->assertMatchesRegularExpression("/[\r\n]\z/mu", $last_character); } diff --git a/tests/Sqlite/MigrateDumpTest.php b/tests/Sqlite/MigrateDumpTest.php index 8578bf3..6916393 100644 --- a/tests/Sqlite/MigrateDumpTest.php +++ b/tests/Sqlite/MigrateDumpTest.php @@ -14,7 +14,8 @@ public function test_handle() $this->assertFileExists($this->schemaSqlPath); $result_sql = file_get_contents($this->schemaSqlPath); $this->assertMatchesRegularExpression("/CREATE TABLE( IF NOT EXISTS)? \"{$this->dbPrefix}test_ms\" /", $result_sql); - $this->assertMatchesRegularExpression("/INSERT INTO \"?{$this->dbPrefix}migrations\"? /", $result_sql); + $this->assertStringContainsString("INSERT INTO {$this->dbPrefix}migrations VALUES(1,'0000_00_00_000000_create_test_tables',0);", $result_sql); + $this->assertStringContainsString("INSERT INTO {$this->dbPrefix}migrations VALUES(2,'0000_00_00_000001_second_migration_for_testing',0);", $result_sql); $last_character = mb_substr($result_sql, -1); $this->assertMatchesRegularExpression("/[\r\n]\z/mu", $last_character); } diff --git a/tests/TestCase.php b/tests/TestCase.php index ec8bb72..1fbe719 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -65,5 +65,9 @@ protected function createTestTablesWithoutMigrate() : void 'migration' => '0000_00_00_000000_create_test_tables', 'batch' => 1, ]); + \DB::table('migrations')->insert([ + 'migration' => '0000_00_00_000001_second_migration_for_testing', + 'batch' => 1, + ]); } } diff --git a/tests/migrations/setup/0000_00_00_000001_second_migration_for_testing.php b/tests/migrations/setup/0000_00_00_000001_second_migration_for_testing.php new file mode 100644 index 0000000..c23f062 --- /dev/null +++ b/tests/migrations/setup/0000_00_00_000001_second_migration_for_testing.php @@ -0,0 +1,14 @@ +