From 0b74f7217be578f54fb421a23e8210be2dc90397 Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Mon, 30 Sep 2024 15:26:30 +0200 Subject: [PATCH 1/3] Add extraOptions to dbDumper --- src/Commands/Create.php | 8 ++++++-- src/SnapshotFactory.php | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Commands/Create.php b/src/Commands/Create.php index 0be21e8..d683606 100644 --- a/src/Commands/Create.php +++ b/src/Commands/Create.php @@ -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.'; @@ -35,6 +35,9 @@ public function handle() $exclude = null; } + $extraOptions = $this->option('extraOptions') ?: config('db-snapshots.extraOptions', []); + $extraOptions = is_string($extraOptions) ? explode(',', $exclude) : $exclude; + $snapshot = app(SnapshotFactory::class)->create( $snapshotName, @@ -42,7 +45,8 @@ public function handle() $connectionName, $compress, $tables, - $exclude + $exclude, + $extraOptions ); $size = Format::humanReadableSize($snapshot->size()); diff --git a/src/SnapshotFactory.php b/src/SnapshotFactory.php index 912606f..c3270e0 100644 --- a/src/SnapshotFactory.php +++ b/src/SnapshotFactory.php @@ -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); @@ -84,6 +84,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'); From 1e2822d52be59c62b7724d31a01c2caeeef164ec Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Sat, 30 Nov 2024 20:58:29 +0100 Subject: [PATCH 2/3] Fix Errors during Testing --- src/Commands/Create.php | 2 +- src/Events/CreatingSnapshot.php | 3 ++- src/SnapshotFactory.php | 7 ++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Commands/Create.php b/src/Commands/Create.php index d683606..d0690aa 100644 --- a/src/Commands/Create.php +++ b/src/Commands/Create.php @@ -36,7 +36,7 @@ public function handle() } $extraOptions = $this->option('extraOptions') ?: config('db-snapshots.extraOptions', []); - $extraOptions = is_string($extraOptions) ? explode(',', $exclude) : $exclude; + $extraOptions = is_string($extraOptions) ? explode(',', $extraOptions) : $extraOptions; $snapshot = app(SnapshotFactory::class)->create( diff --git a/src/Events/CreatingSnapshot.php b/src/Events/CreatingSnapshot.php index 35be24c..4e7059a 100644 --- a/src/Events/CreatingSnapshot.php +++ b/src/Events/CreatingSnapshot.php @@ -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 ) { // } diff --git a/src/SnapshotFactory.php b/src/SnapshotFactory.php index c3270e0..3946de1 100644 --- a/src/SnapshotFactory.php +++ b/src/SnapshotFactory.php @@ -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); @@ -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(); From 92878606e03423d0e2b1474c4b34a41cbee3d508 Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Tue, 10 Dec 2024 22:03:53 +0100 Subject: [PATCH 3/3] Add Test --- tests/Commands/CreateTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/Commands/CreateTest.php b/tests/Commands/CreateTest.php index 77d1fcb..1e7179a 100644 --- a/tests/Commands/CreateTest.php +++ b/tests/Commands/CreateTest.php @@ -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'); @@ -112,3 +113,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'); +});