Skip to content

Commit f839caf

Browse files
authored
Merge pull request #79 from jamatheney/cleanup-old-snapshots
Add command to cleanup old snapshots
2 parents 3f7afaf + efc59d1 commit f839caf

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ php artisan snapshot:load my-first-dump
2424

2525
# List all snapshots
2626
php artisan snapshot:list
27+
28+
# Remove old snapshots. Keeping only the most recent
29+
php artisan snapshot:cleanup --keep=2
2730
```
2831

2932
This package supports MySQL, PostgreSQL and SQLite.
@@ -135,6 +138,12 @@ A dump can be deleted with:
135138
php artisan snapshot:delete my-first-dump
136139
```
137140

141+
To remove all backups except the most recent 2
142+
143+
```bash
144+
php artisan snapshot:cleanup --keep=2
145+
```
146+
138147
## Events
139148

140149
There are several events fired which can be used to perform some logic of your own:

src/Commands/Cleanup.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Spatie\DbSnapshots\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Spatie\DbSnapshots\SnapshotRepository;
7+
8+
class Cleanup extends Command
9+
{
10+
protected $signature = 'snapshot:cleanup {--keep=}';
11+
12+
protected $description = 'Specify how many snapshots to keep and delete the rest';
13+
14+
public function handle()
15+
{
16+
$snapshots = app(SnapshotRepository::class)->getAll();
17+
18+
$keep = $this->option('keep');
19+
20+
if (! $this->option('keep')) {
21+
$this->warn('No value for option --keep.');
22+
23+
return;
24+
}
25+
26+
$snapshots->splice($keep)->each(function ($snapshot) {
27+
$snapshot->delete();
28+
});
29+
}
30+
}

src/DbSnapshotsServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\ServiceProvider;
77
use Spatie\DbSnapshots\Commands\Create;
88
use Spatie\DbSnapshots\Commands\Delete;
9+
use Spatie\DbSnapshots\Commands\Cleanup;
910
use Illuminate\Contracts\Filesystem\Factory;
1011
use Spatie\DbSnapshots\Commands\ListSnapshots;
1112

@@ -34,12 +35,14 @@ public function boot()
3435
$this->app->bind('command.snapshot:load', Load::class);
3536
$this->app->bind('command.snapshot:delete', Delete::class);
3637
$this->app->bind('command.snapshot:list', ListSnapshots::class);
38+
$this->app->bind('command.snapshot:cleanup', Cleanup::class);
3739

3840
$this->commands([
3941
'command.snapshot:create',
4042
'command.snapshot:load',
4143
'command.snapshot:delete',
4244
'command.snapshot:list',
45+
'command.snapshot:cleanup',
4346
]);
4447
}
4548

tests/Commands/CleanupTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Spatie\DbSnapshots\Commands\Test;
4+
5+
use Spatie\DbSnapshots\Test\TestCase;
6+
use Illuminate\Support\Facades\Artisan;
7+
8+
class CleanupTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_can_delete_old_snapshots_keeping_the_desired_number_of_snapshots()
12+
{
13+
// Add sleep to make sure files do not have the same modified time.
14+
// They may not sort properly if all have the same timestamp.
15+
$this->clearDisk();
16+
17+
$this->disk->put('snapshot1.sql', 'new content');
18+
19+
sleep(1);
20+
21+
$this->disk->put('snapshot2.sql', 'new content');
22+
23+
Artisan::call('snapshot:cleanup', ['--keep' => 1]);
24+
25+
$this->disk->assertMissing('snapshot1.sql');
26+
$this->disk->assertExists('snapshot2.sql');
27+
}
28+
}

0 commit comments

Comments
 (0)