Skip to content

Commit a37f1a4

Browse files
authored
Merge pull request #125 from ariaieboy/patch1
add --exclude to the create command
2 parents 7ee7b8d + 355a142 commit a37f1a4

File tree

8 files changed

+101
-13
lines changed

8 files changed

+101
-13
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,18 @@ This is the content of the published file:
8484
```php
8585
return [
8686

87-
/**
87+
/*
8888
* The name of the disk on which the snapshots are stored.
8989
*/
9090
'disk' => 'snapshots',
9191

92-
/**
92+
/*
9393
* The connection to be used to create snapshots. Set this to null
94-
* to use the default configured in `config/database.php`
94+
* to use the default configured in `config/databases.php`
9595
*/
9696
'default_connection' => null,
9797

98-
/**
98+
/*
9999
* The directory where temporary files will be stored.
100100
*/
101101
'temporary_directory_path' => storage_path('app/laravel-db-snapshots/temp'),
@@ -111,7 +111,15 @@ return [
111111
* Default: `null`
112112
*/
113113
'tables' => null,
114+
115+
/*
116+
* All tables will be included in the snapshot expect this tables. Set to `null` to include all tables.
117+
*
118+
* Default: `null`
119+
*/
120+
'exclude' => null,
114121
];
122+
115123
```
116124

117125
## Usage
@@ -137,6 +145,14 @@ php artisan snapshot:create --table=posts,users
137145
php artisan snapshot:create --table=posts --table=users
138146
```
139147

148+
You may want to exclude some tables from snapshot. You can do this by passing the `--exclude` multiple times or as a comma separated list:
149+
```bash
150+
# create snapshot from all tables exclude the users and posts
151+
php artisan snapshot:create --exclude=posts,users
152+
php artisan snapshot:create --exclude=posts --exclude=users
153+
```
154+
> Note: if you pass `--table` and `--exclude` in the same time it will use `--table` to create the snapshot and it's ignore the `--exclude`
155+
140156
When creating snapshots, you can optionally create compressed snapshots. To do this either pass the `--compress` option on the command line, or set the `db-snapshots.compress` configuration option to `true`:
141157

142158
```bash

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": "^8.0",
2020
"illuminate/support": "^7.0|^8.0|^9.0",
2121
"league/flysystem": "^1.0.41|^2.0|^3.0",
22-
"spatie/db-dumper": "^3.1",
22+
"spatie/db-dumper": "^3.3",
2323
"spatie/laravel-package-tools": "^1.6",
2424
"spatie/temporary-directory": "^2.0"
2525
},

config/db-snapshots.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@
2929
* Default: `null`
3030
*/
3131
'tables' => null,
32+
33+
/*
34+
* All tables will be included in the snapshot expect this tables. Set to `null` to include all tables.
35+
*
36+
* Default: `null`
37+
*/
38+
'exclude' => null,
3239
];

src/Commands/Create.php

Lines changed: 11 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=*}';
12+
protected $signature = 'snapshot:create {name?} {--connection=} {--compress} {--table=*} {--exclude=*}';
1313

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

@@ -28,12 +28,21 @@ public function handle()
2828
$tables = $this->option('table') ?: config('db-snapshots.tables', null);
2929
$tables = is_string($tables) ? explode(',', $tables) : $tables;
3030

31+
if (is_null($tables)) {
32+
$exclude = $this->option('exclude') ?: config('db-snapshots.exclude', null);
33+
$exclude = is_string($exclude) ? explode(',', $exclude) : $exclude;
34+
} else {
35+
$exclude = null;
36+
}
37+
38+
3139
$snapshot = app(SnapshotFactory::class)->create(
3240
$snapshotName,
3341
config('db-snapshots.disk'),
3442
$connectionName,
3543
$compress,
36-
$tables
44+
$tables,
45+
$exclude
3746
);
3847

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

src/Events/CreatingSnapshot.php

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

src/SnapshotFactory.php

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

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

27-
$fileName = $snapshotName.'.sql';
27+
$fileName = $snapshotName . '.sql';
2828
$fileName = pathinfo($fileName, PATHINFO_BASENAME);
2929

3030
if ($compress) {
@@ -35,10 +35,11 @@ public function create(string $snapshotName, string $diskName, string $connectio
3535
$fileName,
3636
$disk,
3737
$connectionName,
38-
$tables
38+
$tables,
39+
$exclude
3940
));
4041

41-
$this->createDump($connectionName, $fileName, $disk, $compress, $tables);
42+
$this->createDump($connectionName, $fileName, $disk, $compress, $tables, $exclude);
4243

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

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

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

@@ -79,6 +80,10 @@ protected function createDump(string $connectionName, string $fileName, Filesyst
7980
$dbDumper->includeTables($tables);
8081
}
8182

83+
if (is_array($exclude)) {
84+
$dbDumper->excludeTables($exclude);
85+
}
86+
8287
$dbDumper->dumpToFile($dumpPath);
8388

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

tests/Commands/CreateTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,42 @@ public function it_can_create_a_snapshot_with_specific_tables_specified_in_the_c
9191
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
9292
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
9393
}
94+
95+
/** @test */
96+
public function it_can_create_a_snapshot_without_excluded_tables_specified_in_the_command_options()
97+
{
98+
Artisan::call('snapshot:create', ['--exclude' => ['users', 'posts']]);
99+
100+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
101+
102+
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/');
103+
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
104+
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
105+
}
106+
107+
/** @test */
108+
public function it_can_create_a_snapshot_without_excluded_tables_specified_in_the_command_options_as_a_string()
109+
{
110+
Artisan::call('snapshot:create', ['--exclude' => 'users,posts']);
111+
112+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
113+
114+
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/');
115+
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
116+
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
117+
}
118+
119+
/** @test */
120+
public function it_can_create_a_snapshot_without_excluded_tables_specified_in_the_config()
121+
{
122+
$this->app['config']->set('db-snapshots.exclude', ['users', 'posts']);
123+
124+
Artisan::call('snapshot:create');
125+
126+
$fileName = Carbon::now()->format('Y-m-d_H-i-s') . '.sql';
127+
128+
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "users"/');
129+
$this->assertFileOnDiskFailsRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "posts"/');
130+
$this->assertFileOnDiskPassesRegex($fileName, '/CREATE TABLE(?: IF NOT EXISTS){0,1} "models"/');
131+
}
94132
}

tests/Events/CreatingSnapshotTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@ public function creating_a_snapshot_fires_the_creating_snapshot_event()
2020
return $event->fileName === 'my-snapshot.sql';
2121
});
2222
}
23+
24+
/** @test */
25+
public function creating_a_snapshot_with_exclude_will_pass_excluded_tables()
26+
{
27+
Event::fake();
28+
29+
Artisan::call('snapshot:create', ['name' => 'my-snapshot', '--exclude' => ['tb1', 'tb2']]);
30+
31+
Event::assertDispatched(CreatingSnapshot::class, function (CreatingSnapshot $event) {
32+
return ($event->fileName === 'my-snapshot.sql') && $event->exclude === ['tb1', 'tb2'];
33+
});
34+
}
2335
}

0 commit comments

Comments
 (0)