Skip to content

Commit 89cb8bc

Browse files
authored
Merge pull request #148 from michaeljhopkins/main
Prepend the connection onto the name
2 parents a12430e + 60c51f7 commit 89cb8bc

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ Giving your snapshot a name is optional. If you don't pass a name the current da
133133
php artisan snapshot:create
134134
```
135135

136+
If you pass a connection but do not declare a name for the snapshot, the connection will be prepended
137+
138+
```bash
139+
# Creates a snapshot named something like `logging_2017-03-17 14:31`
140+
php artisan snapshot:create --connection=logging
141+
```
142+
136143
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:
137144

138145
```bash

src/Commands/Create.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function handle()
2121
?: config('db-snapshots.default_connection')
2222
?? config('database.default');
2323

24-
$snapshotName = $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s');
24+
$snapshotName = $this->getSnapshotName();
2525

2626
$compress = $this->option('compress') || config('db-snapshots.compress', false);
2727

@@ -49,4 +49,13 @@ public function handle()
4949

5050
$this->info("Snapshot `{$snapshotName}` created (size: {$size})");
5151
}
52+
53+
private function getSnapshotName(): string
54+
{
55+
if (!is_null($this->option('connection')) && is_null($this->argument('name'))) {
56+
return $this->option('connection'). "_". Carbon::now()->format('Y-m-d_H-i-s');
57+
}
58+
59+
return $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s');
60+
}
5261
}

tests/Commands/CreateTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919
->expect('test.sql')
2020
->fileOnDiskToPassRegex('/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
2121

22+
it('can will prepend the connection to the default name if no specific name is passed', function () {
23+
24+
config()->set('database.connections.second_connection', [
25+
'driver' => 'sqlite',
26+
'database' => __DIR__ . '/../temp/second_connection.sqlite',
27+
'prefix' => '',
28+
]);
29+
30+
Artisan::call('snapshot:create', ['--connection' => 'second_connection']);
31+
$fileName = 'second_connection_' . Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
32+
33+
$this->disk->assertExists($fileName);
34+
});
35+
2236
it('can create a compressed snapshot from CLI param', function () {
2337
Artisan::call('snapshot:create', ['--compress' => true]);
2438

0 commit comments

Comments
 (0)