Skip to content

Commit 4d4b9f4

Browse files
authored
Merge pull request #150 from wattnpapa/addExtraOptionDump
Add extraOptions to dbDumper
2 parents 4dc5141 + 9287860 commit 4d4b9f4

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

src/Commands/Create.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

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

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

@@ -35,14 +35,18 @@ public function handle()
3535
$exclude = null;
3636
}
3737

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

3942
$snapshot = app(SnapshotFactory::class)->create(
4043
$snapshotName,
4144
config('db-snapshots.disk'),
4245
$connectionName,
4346
$compress,
4447
$tables,
45-
$exclude
48+
$exclude,
49+
$extraOptions
4650
);
4751

4852
$size = Format::humanReadableSize($snapshot->size());

src/Events/CreatingSnapshot.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public function __construct(
1111
public FilesystemAdapter $disk,
1212
public string $connectionName,
1313
public ?array $tables = null,
14-
public ?array $exclude = null
14+
public ?array $exclude = null,
15+
public ?array $extraOptions = null
1516
) {
1617
//
1718
}

src/SnapshotFactory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct(
2020
//
2121
}
2222

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

@@ -36,10 +36,11 @@ public function create(string $snapshotName, string $diskName, string $connectio
3636
$disk,
3737
$connectionName,
3838
$tables,
39-
$exclude
39+
$exclude,
40+
$extraOptions
4041
));
4142

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

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

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

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

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

88+
foreach ($extraOptions as $extraOption) {
89+
$dbDumper->addExtraOption($extraOption);
90+
}
91+
8792
$dbDumper->dumpToFile($dumpPath);
8893

8994
$file = fopen($dumpPath, 'r');

tests/Commands/CreateTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Carbon\Carbon;
44
use Illuminate\Support\Facades\Artisan;
5+
use Illuminate\Support\Facades\Storage;
56

67
it('can create a snapshot without a specific', function () {
78
Artisan::call('snapshot:create');
@@ -126,3 +127,31 @@
126127
->fileOnDiskToFailRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/')
127128
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
128129
});
130+
131+
it('passes extraOptions correctly to the command', function () {
132+
// Set up
133+
Storage::fake('snapshots');
134+
135+
// Mock or set the extraOptions you want to test
136+
$extraOptions = ['--compress' => true, '--exclude-tables' => 'logs'];
137+
138+
// Execute the artisan command with extra options
139+
Artisan::call('snapshot:create', [
140+
'name' => 'test_snapshot',
141+
'--disk' => 'snapshots',
142+
'--extraOptions' => json_encode($extraOptions),
143+
]);
144+
145+
// Assertions
146+
$output = Artisan::output();
147+
expect($output)->toContain('--compress')->toContain('true');
148+
expect($output)->toContain('--exclude-tables')->toContain('logs');
149+
150+
// Verify the snapshot file was created
151+
$snapshotFileName = 'test_snapshot.sql';
152+
Storage::disk('snapshots')->assertExists($snapshotFileName);
153+
154+
// Optionally, check the snapshot's content or metadata
155+
$snapshotContent = Storage::disk('snapshots')->get($snapshotFileName);
156+
expect($snapshotContent)->toContain('--compress')->toContain('--exclude-tables');
157+
});

0 commit comments

Comments
 (0)