From 7d699b0a876647323fabc896ef434fd641e1cb03 Mon Sep 17 00:00:00 2001 From: Michael Hopkins Date: Fri, 30 Aug 2024 10:25:20 -0600 Subject: [PATCH 1/6] Add some code to add the connection to the snapshot name if it's passed in --- src/Commands/Create.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Commands/Create.php b/src/Commands/Create.php index 0be21e8..e3a15e5 100644 --- a/src/Commands/Create.php +++ b/src/Commands/Create.php @@ -21,7 +21,11 @@ public function handle() ?: config('db-snapshots.default_connection') ?? config('database.default'); - $snapshotName = $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s'); + if (! is_null($this->option('connection')) && is_null($this->argument('name'))) { + $snapshotName = $this->option('connection') . "_" . Carbon::now()->format('Y-m-d_H-i-s'); + } else { + $snapshotName = $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s'); + } $compress = $this->option('compress') || config('db-snapshots.compress', false); From 9e535faa3580b61fd7740eccf1ff6deedfc67184 Mon Sep 17 00:00:00 2001 From: Michael Hopkins Date: Fri, 30 Aug 2024 10:27:03 -0600 Subject: [PATCH 2/6] Update Create.php Add some code to add the connection to the snapshot name if it's passed in --- src/Commands/Create.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Commands/Create.php b/src/Commands/Create.php index 0be21e8..e3a15e5 100644 --- a/src/Commands/Create.php +++ b/src/Commands/Create.php @@ -21,7 +21,11 @@ public function handle() ?: config('db-snapshots.default_connection') ?? config('database.default'); - $snapshotName = $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s'); + if (! is_null($this->option('connection')) && is_null($this->argument('name'))) { + $snapshotName = $this->option('connection') . "_" . Carbon::now()->format('Y-m-d_H-i-s'); + } else { + $snapshotName = $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s'); + } $compress = $this->option('compress') || config('db-snapshots.compress', false); From b07dfcf2449fe7e46390861c17fc13a8a21e895e Mon Sep 17 00:00:00 2001 From: Michael Hopkins Date: Mon, 2 Sep 2024 09:47:21 -0600 Subject: [PATCH 3/6] Add test --- tests/Commands/CreateTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Commands/CreateTest.php b/tests/Commands/CreateTest.php index 77d1fcb..b0ffe33 100644 --- a/tests/Commands/CreateTest.php +++ b/tests/Commands/CreateTest.php @@ -19,6 +19,20 @@ ->expect('test.sql') ->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/'); +it('can will prepend the connection to the default name if no specific name is passed', function () { + + config()->set('database.connections.second_connection', [ + 'driver' => 'sqlite', + 'database' => __DIR__ . '/../temp/second_connection.sqlite', + 'prefix' => '', + ]); + + Artisan::call('snapshot:create', ['--connection' => 'second_connection']); + $fileName = 'second_connection_' . Carbon::now()->format('Y-m-d_H-i-s') . '.sql'; + + $this->disk->assertExists($fileName); +}); + it('can create a compressed snapshot from CLI param', function () { Artisan::call('snapshot:create', ['--compress' => true]); From ee50ee6bfbbe7e72fda2dcabfcc5ba849b34fb7a Mon Sep 17 00:00:00 2001 From: Michael Hopkins Date: Mon, 2 Sep 2024 09:51:19 -0600 Subject: [PATCH 4/6] Refactor out else statement --- src/Commands/Create.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Commands/Create.php b/src/Commands/Create.php index e3a15e5..72108fd 100644 --- a/src/Commands/Create.php +++ b/src/Commands/Create.php @@ -21,11 +21,9 @@ public function handle() ?: config('db-snapshots.default_connection') ?? config('database.default'); - if (! is_null($this->option('connection')) && is_null($this->argument('name'))) { - $snapshotName = $this->option('connection') . "_" . Carbon::now()->format('Y-m-d_H-i-s'); - } else { - $snapshotName = $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s'); - } + $snapshotName = !is_null($this->option('connection')) && is_null($this->argument('name')) + ? $this->option('connection')."_".Carbon::now()->format('Y-m-d_H-i-s') + : $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s'); $compress = $this->option('compress') || config('db-snapshots.compress', false); From e2e77a1f378b75124917837b9a0d56eda48da757 Mon Sep 17 00:00:00 2001 From: Michael Hopkins Date: Mon, 2 Sep 2024 09:56:24 -0600 Subject: [PATCH 5/6] Extract to method --- src/Commands/Create.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Commands/Create.php b/src/Commands/Create.php index 72108fd..8a9ecf3 100644 --- a/src/Commands/Create.php +++ b/src/Commands/Create.php @@ -21,9 +21,7 @@ public function handle() ?: config('db-snapshots.default_connection') ?? config('database.default'); - $snapshotName = !is_null($this->option('connection')) && is_null($this->argument('name')) - ? $this->option('connection')."_".Carbon::now()->format('Y-m-d_H-i-s') - : $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s'); + $snapshotName = $this->getSnapshotName(); $compress = $this->option('compress') || config('db-snapshots.compress', false); @@ -51,4 +49,13 @@ public function handle() $this->info("Snapshot `{$snapshotName}` created (size: {$size})"); } + + private function getSnapshotName(): string + { + if (!is_null($this->option('connection')) && is_null($this->argument('name'))) { + return $this->option('connection'). "_". Carbon::now()->format('Y-m-d_H-i-s'); + } + + return $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s'); + } } From 60c51f788146dae30065b632534116e1be476d20 Mon Sep 17 00:00:00 2001 From: Michael Hopkins Date: Mon, 2 Sep 2024 09:59:51 -0600 Subject: [PATCH 6/6] Update docs --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index e5d447b..a5c1cea 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,13 @@ Giving your snapshot a name is optional. If you don't pass a name the current da php artisan snapshot:create ``` +If you pass a connection but do not declare a name for the snapshot, the connection will be prepended + +```bash +# Creates a snapshot named something like `logging_2017-03-17 14:31` +php artisan snapshot:create --connection=logging +``` + Maybe you only want to snapshot a couple of tables. You can do this by passing the `--table` multiple times or as a comma separated list: ```bash