Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Commands/MigrateDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);

Expand Down
33 changes: 33 additions & 0 deletions tests/MigrateDumpTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use AlwaysOpen\MigrationSnapshot\Commands\MigrateDumpCommand;
use AlwaysOpen\MigrationSnapshot\Tests\TestCase;

class MigrateDumpTest extends TestCase
Expand Down Expand Up @@ -32,4 +33,36 @@ public function test_dump_callsAfterDumpClosure()
$schema_sql = file_get_contents($this->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);
}
}