Skip to content

Commit 9e98474

Browse files
authored
Merge pull request #23 from bstanley-pec/fix/data-dump-file-duplicates
Ensure dump files removed before appending to them
2 parents 33968f7 + bb13a57 commit 9e98474

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Commands/MigrateDumpCommand.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function handle()
2929
$schema_sql_directory = dirname($schema_sql_path);
3030
if (! file_exists($schema_sql_directory)) {
3131
mkdir($schema_sql_directory, 0755);
32+
} elseif (file_exists($schema_sql_path)) {
33+
unlink($schema_sql_path);
3234
}
3335

3436
if (! in_array($db_config['driver'], self::SUPPORTED_DB_DRIVERS, true)) {
@@ -67,6 +69,10 @@ public function handle()
6769
$data_path = preg_replace('/\.sql$/', '.pgdump', $data_path);
6870
}
6971

72+
if (file_exists($data_path)) {
73+
unlink($data_path);
74+
}
75+
7076
$method = $db_config['driver'] . 'DataDump';
7177
$exit_code = self::{$method}($db_config, $data_path);
7278

tests/MigrateDumpTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use AlwaysOpen\MigrationSnapshot\Commands\MigrateDumpCommand;
34
use AlwaysOpen\MigrationSnapshot\Tests\TestCase;
45

56
class MigrateDumpTest extends TestCase
@@ -32,4 +33,36 @@ public function test_dump_callsAfterDumpClosure()
3233
$schema_sql = file_get_contents($this->schemaSqlPath);
3334
$this->assertStringNotContainsString('/*', $schema_sql);
3435
}
36+
37+
public function test_dumpWithData()
38+
{
39+
if (!file_exists($this->schemaSqlDirectory)) {
40+
mkdir($this->schemaSqlDirectory, 0755);
41+
}
42+
43+
file_put_contents($this->schemaSqlPath, 'Line that should not exist 1' . PHP_EOL);
44+
file_put_contents($this->schemaSqlPath, 'Line that should not exist 2', FILE_APPEND);
45+
46+
$dataSqlPath = MigrateDumpCommand::getDataSqlPath(
47+
$this->app['config']->get('database.connections.' . $this->dbDefault . '.driver')
48+
);
49+
50+
file_put_contents($dataSqlPath, 'Line that should not exist 1' . PHP_EOL);
51+
file_put_contents($dataSqlPath, 'Line that should not exist 2', FILE_APPEND);
52+
53+
$this->createTestTablesWithoutMigrate();
54+
$result = \Artisan::call('migrate:dump', ['--include-data' => true]);
55+
$this->assertEquals(0, $result);
56+
$this->assertDirectoryExists($this->schemaSqlDirectory);
57+
58+
$this->assertFileExists($this->schemaSqlPath);
59+
$result_sql = file_get_contents($this->schemaSqlPath);
60+
$this->assertStringNotContainsString('Line that should not exist 1', $result_sql);
61+
$this->assertStringNotContainsString('Line that should not exist 2', $result_sql);
62+
63+
$this->assertFileExists($dataSqlPath);
64+
$result_data = file_get_contents($dataSqlPath);
65+
$this->assertStringNotContainsString('Line that should not exist 1', $result_data);
66+
$this->assertStringNotContainsString('Line that should not exist 2', $result_data);
67+
}
3568
}

0 commit comments

Comments
 (0)