File tree Expand file tree Collapse file tree 3 files changed +27
-11
lines changed Expand file tree Collapse file tree 3 files changed +27
-11
lines changed Original file line number Diff line number Diff line change @@ -16,23 +16,29 @@ public function prune(int $retentionDays): int
1616 {
1717 $ day = 86400 ;
1818 $ oldestAcceptable = time () - ($ retentionDays * $ day );
19- $ deleteCount = 0 ;
19+ $ ids = [] ;
2020
2121 Post::query ()
2222 ->where ('published_at ' , '< ' , $ oldestAcceptable )
2323 ->select (['id ' , 'thumbnail ' ])
24- ->chunk (250 , function (Collection $ posts ) use (&$ deleteCount ) {
25- $ deleteCount += $ posts ->count ( );
26- $ this ->deletePosts ($ posts );
24+ ->chunk (250 , function (Collection $ posts ) use (&$ ids ) {
25+ array_push ( $ ids , ... $ posts ->pluck ( ' id ' )-> all () );
26+ $ this ->deletePostsThumbnails ($ posts );
2727 });
2828
29- return $ deleteCount ;
29+ foreach (array_chunk ($ ids , 250 ) as $ idChunk ) {
30+ Post::query ()
31+ ->whereIn ('id ' , $ idChunk )
32+ ->delete ();
33+ }
34+
35+ return count ($ ids );
3036 }
3137
3238 /**
3339 * @param Collection<Post> $posts
3440 */
35- protected function deletePosts (Collection $ posts )
41+ protected function deletePostsThumbnails (Collection $ posts )
3642 {
3743 $ storage = Storage::disk ('public ' );
3844
@@ -41,9 +47,5 @@ protected function deletePosts(Collection $posts)
4147 $ storage ->delete ($ post ->thumbnail );
4248 }
4349 }
44-
45- Post::query ()
46- ->whereIn ('id ' , $ posts ->pluck ('id ' )->all ())
47- ->delete ();
4850 }
4951}
Original file line number Diff line number Diff line change @@ -16,8 +16,9 @@ class FeedFactory extends Factory
1616 */
1717 public function definition ()
1818 {
19+ $ url = $ this ->faker ->url . '?query= ' . random_int (0 , 1000 );
1920 return [
20- 'url ' => $ this -> faker -> url ,
21+ 'url ' => $ url ,
2122 'last_fetched_at ' => now ()->subHours (random_int (0 , 100 ))->unix (),
2223 'last_accessed_at ' => time (),
2324 ];
Original file line number Diff line number Diff line change @@ -78,4 +78,17 @@ public function test_command_defaults_to_no_action_if_config_false()
7878
7979 $ this ->assertEquals (1 , Post::query ()->count ());
8080 }
81+
82+ public function test_command_deletes_all_posts_in_range ()
83+ {
84+ Post::factory (500 )->create (['published_at ' => time () - (86400 * 10.1 )]);
85+
86+ $ this ->assertEquals (500 , Post::query ()->count ());
87+
88+ $ this ->artisan ('rss:prune-posts --days=3 ' )
89+ ->expectsConfirmation ('This will delete all posts older than 3 day(s). Do you want to continue? ' , 'yes ' )
90+ ->assertExitCode (0 );
91+
92+ $ this ->assertEquals (0 , Post::query ()->count ());
93+ }
8194}
You can’t perform that action at this time.
0 commit comments