From bb13a57430a0a9b62943c2566c8ead1ff9ef8ffe Mon Sep 17 00:00:00 2001 From: Brian Stanley Date: Fri, 8 Aug 2025 17:37:36 -0400 Subject: [PATCH] Ensure dump files removed before appending to them --- src/Commands/MigrateDumpCommand.php | 6 ++++++ tests/MigrateDumpTest.php | 33 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Commands/MigrateDumpCommand.php b/src/Commands/MigrateDumpCommand.php index 36b9d9e..b628d1c 100644 --- a/src/Commands/MigrateDumpCommand.php +++ b/src/Commands/MigrateDumpCommand.php @@ -29,6 +29,8 @@ public function handle() $schema_sql_directory = dirname($schema_sql_path); if (! file_exists($schema_sql_directory)) { mkdir($schema_sql_directory, 0755); + } elseif (file_exists($schema_sql_path)) { + unlink($schema_sql_path); } if (! in_array($db_config['driver'], self::SUPPORTED_DB_DRIVERS, true)) { @@ -67,6 +69,10 @@ public function handle() $data_path = preg_replace('/\.sql$/', '.pgdump', $data_path); } + if (file_exists($data_path)) { + unlink($data_path); + } + $method = $db_config['driver'] . 'DataDump'; $exit_code = self::{$method}($db_config, $data_path); diff --git a/tests/MigrateDumpTest.php b/tests/MigrateDumpTest.php index 285a167..505377d 100644 --- a/tests/MigrateDumpTest.php +++ b/tests/MigrateDumpTest.php @@ -1,5 +1,6 @@ schemaSqlPath); $this->assertStringNotContainsString('/*', $schema_sql); } + + public function test_dumpWithData() + { + if (!file_exists($this->schemaSqlDirectory)) { + mkdir($this->schemaSqlDirectory, 0755); + } + + file_put_contents($this->schemaSqlPath, 'Line that should not exist 1' . PHP_EOL); + file_put_contents($this->schemaSqlPath, 'Line that should not exist 2', FILE_APPEND); + + $dataSqlPath = MigrateDumpCommand::getDataSqlPath( + $this->app['config']->get('database.connections.' . $this->dbDefault . '.driver') + ); + + file_put_contents($dataSqlPath, 'Line that should not exist 1' . PHP_EOL); + file_put_contents($dataSqlPath, 'Line that should not exist 2', FILE_APPEND); + + $this->createTestTablesWithoutMigrate(); + $result = \Artisan::call('migrate:dump', ['--include-data' => true]); + $this->assertEquals(0, $result); + $this->assertDirectoryExists($this->schemaSqlDirectory); + + $this->assertFileExists($this->schemaSqlPath); + $result_sql = file_get_contents($this->schemaSqlPath); + $this->assertStringNotContainsString('Line that should not exist 1', $result_sql); + $this->assertStringNotContainsString('Line that should not exist 2', $result_sql); + + $this->assertFileExists($dataSqlPath); + $result_data = file_get_contents($dataSqlPath); + $this->assertStringNotContainsString('Line that should not exist 1', $result_data); + $this->assertStringNotContainsString('Line that should not exist 2', $result_data); + } }