Skip to content

Commit

Permalink
Merge pull request #150 from wattnpapa/addExtraOptionDump
Browse files Browse the repository at this point in the history
Add extraOptions to dbDumper
  • Loading branch information
freekmurze authored Dec 11, 2024
2 parents 4dc5141 + 9287860 commit 4d4b9f4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/Commands/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Create extends Command
{
protected $signature = 'snapshot:create {name?} {--connection=} {--compress} {--table=*} {--exclude=*}';
protected $signature = 'snapshot:create {name?} {--connection=} {--compress} {--table=*} {--exclude=*} {--extraOptions=*}';

protected $description = 'Create a new snapshot.';

Expand All @@ -35,14 +35,18 @@ public function handle()
$exclude = null;
}

$extraOptions = $this->option('extraOptions') ?: config('db-snapshots.extraOptions', []);
$extraOptions = is_string($extraOptions) ? explode(',', $extraOptions) : $extraOptions;


$snapshot = app(SnapshotFactory::class)->create(
$snapshotName,
config('db-snapshots.disk'),
$connectionName,
$compress,
$tables,
$exclude
$exclude,
$extraOptions
);

$size = Format::humanReadableSize($snapshot->size());
Expand Down
3 changes: 2 additions & 1 deletion src/Events/CreatingSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public function __construct(
public FilesystemAdapter $disk,
public string $connectionName,
public ?array $tables = null,
public ?array $exclude = null
public ?array $exclude = null,
public ?array $extraOptions = null
) {
//
}
Expand Down
13 changes: 9 additions & 4 deletions src/SnapshotFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
//
}

public function create(string $snapshotName, string $diskName, string $connectionName, bool $compress = false, ?array $tables = null, ?array $exclude = null): Snapshot
public function create(string $snapshotName, string $diskName, string $connectionName, bool $compress = false, ?array $tables = null, ?array $exclude = null, array $extraOptions = []): Snapshot
{
$disk = $this->getDisk($diskName);

Expand All @@ -36,10 +36,11 @@ public function create(string $snapshotName, string $diskName, string $connectio
$disk,
$connectionName,
$tables,
$exclude
$exclude,
$extraOptions
));

$this->createDump($connectionName, $fileName, $disk, $compress, $tables, $exclude);
$this->createDump($connectionName, $fileName, $disk, $compress, $tables, $exclude, $extraOptions);

$snapshot = new Snapshot($disk, $fileName);

Expand All @@ -64,7 +65,7 @@ protected function getDbDumper(string $connectionName): DbDumper
return $factory::createForConnection($connectionName);
}

protected function createDump(string $connectionName, string $fileName, FilesystemAdapter $disk, bool $compress = false, ?array $tables = null, ?array $exclude = null): void
protected function createDump(string $connectionName, string $fileName, FilesystemAdapter $disk, bool $compress = false, ?array $tables = null, ?array $exclude = null, array $extraOptions = []): void
{
$directory = (new TemporaryDirectory(config('db-snapshots.temporary_directory_path')))->create();

Expand All @@ -84,6 +85,10 @@ protected function createDump(string $connectionName, string $fileName, Filesyst
$dbDumper->excludeTables($exclude);
}

foreach ($extraOptions as $extraOption) {
$dbDumper->addExtraOption($extraOption);
}

$dbDumper->dumpToFile($dumpPath);

$file = fopen($dumpPath, 'r');
Expand Down
29 changes: 29 additions & 0 deletions tests/Commands/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Carbon\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Storage;

it('can create a snapshot without a specific', function () {
Artisan::call('snapshot:create');
Expand Down Expand Up @@ -126,3 +127,31 @@
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/')
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
});

it('passes extraOptions correctly to the command', function () {
// Set up
Storage::fake('snapshots');

// Mock or set the extraOptions you want to test
$extraOptions = ['--compress' => true, '--exclude-tables' => 'logs'];

// Execute the artisan command with extra options
Artisan::call('snapshot:create', [
'name' => 'test_snapshot',
'--disk' => 'snapshots',
'--extraOptions' => json_encode($extraOptions),
]);

// Assertions
$output = Artisan::output();
expect($output)->toContain('--compress')->toContain('true');
expect($output)->toContain('--exclude-tables')->toContain('logs');

// Verify the snapshot file was created
$snapshotFileName = 'test_snapshot.sql';
Storage::disk('snapshots')->assertExists($snapshotFileName);

// Optionally, check the snapshot's content or metadata
$snapshotContent = Storage::disk('snapshots')->get($snapshotFileName);
expect($snapshotContent)->toContain('--compress')->toContain('--exclude-tables');
});

0 comments on commit 4d4b9f4

Please sign in to comment.