File tree Expand file tree Collapse file tree 4 files changed +70
-0
lines changed Expand file tree Collapse file tree 4 files changed +70
-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 66use Illuminate \Support \ServiceProvider ;
77use Spatie \DbSnapshots \Commands \Create ;
88use Spatie \DbSnapshots \Commands \Delete ;
9+ use Spatie \DbSnapshots \Commands \Cleanup ;
910use Illuminate \Contracts \Filesystem \Factory ;
1011use 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments