Skip to content

Commit

Permalink
Merge pull request #148 from michaeljhopkins/main
Browse files Browse the repository at this point in the history
Prepend the connection onto the name
  • Loading branch information
freekmurze authored Dec 9, 2024
2 parents a12430e + 60c51f7 commit 89cb8bc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/Commands/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function handle()
?: config('db-snapshots.default_connection')
?? config('database.default');

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

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

Expand Down Expand Up @@ -49,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');
}
}
14 changes: 14 additions & 0 deletions tests/Commands/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down

0 comments on commit 89cb8bc

Please sign in to comment.