File tree Expand file tree Collapse file tree 4 files changed +72
-0
lines changed Expand file tree Collapse file tree 4 files changed +72
-0
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,9 @@ php artisan snapshot:load my-first-dump
2424
2525# List all snapshots
2626php artisan snapshot:list
27+
28+ # Remove old snapshots. Keeping only the most recent
29+ php artisan snapshot:cleanup --keep=2
2730```
2831
2932This package supports MySQL, PostgreSQL and SQLite.
@@ -135,6 +138,12 @@ A dump can be deleted with:
135138php 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
140149There are several events fired which can be used to perform some logic of your own:
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 77use Spatie \DbSnapshots \Commands \Create ;
88use Spatie \DbSnapshots \Commands \Delete ;
99use Illuminate \Contracts \Filesystem \Factory ;
10+ use Spatie \DbSnapshots \Commands \Cleanup ;
1011use Spatie \DbSnapshots \Commands \ListSnapshots ;
1112
1213class DbSnapshotsServiceProvider extends ServiceProvider
@@ -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
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Spatie \DbSnapshots \Commands \Test ;
4+
5+ use Mockery as m ;
6+ use Spatie \DbSnapshots \Test \TestCase ;
7+ use Illuminate \Support \Facades \Artisan ;
8+ use Illuminate \Support \Facades \Storage ;
9+
10+ class CleanupTest extends TestCase
11+ {
12+ /** @test */
13+ public function it_can_delete_old_snapshots_keeping_the_desired_number_of_snapshots ()
14+ {
15+ // Add sleep to make sure files do not have the same modified time.
16+ // They may not sort properly if all have the same timestamp.
17+ $ this ->clearDisk ();
18+
19+ $ this ->disk ->put ("snapshot1.sql " , 'new content ' );
20+
21+ sleep (1 );
22+
23+ $ this ->disk ->put ("snapshot2.sql " , 'new content ' );
24+
25+ Artisan::call ('snapshot:cleanup ' , ['--keep ' => 1 ]);
26+
27+ $ this ->disk ->assertMissing ('snapshot1.sql ' );
28+ $ this ->disk ->assertExists ('snapshot2.sql ' );
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments