File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed
Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -23,9 +23,22 @@ public function audit(Auditable $model): ?Audit
2323 public function prune (Auditable $ model ): bool
2424 {
2525 if (($ threshold = $ model ->getAuditThreshold ()) > 0 ) {
26+ $ auditClass = get_class ($ model ->audits ()->getModel ());
27+ $ auditModel = new $ auditClass ;
28+
2629 return $ model ->audits ()
27- ->latest ()
28- ->offset ($ threshold )->limit (PHP_INT_MAX )
30+ ->leftJoinSub (
31+ $ model ->audits ()->select ($ auditModel ->getKeyName ())->limit ($ threshold )->latest (),
32+ 'audit_threshold ' ,
33+ function ($ join ) use ($ auditModel ) {
34+ $ join ->on (
35+ $ auditModel ->gettable ().'. ' .$ auditModel ->getKeyName (),
36+ '= ' ,
37+ 'audit_threshold. ' .$ auditModel ->getKeyName ()
38+ );
39+ }
40+ )
41+ ->whereNull ('audit_threshold. ' .$ auditModel ->getKeyName ())
2942 ->delete () > 0 ;
3043 }
3144
Original file line number Diff line number Diff line change @@ -297,16 +297,23 @@ public function itWillRemoveOlderAuditsAboveTheThreshold()
297297 ]);
298298
299299 $ article = factory (Article::class)->create ([
300- 'reviewed ' => 1 ,
300+ 'title ' => ' Title #0 ' ,
301301 ]);
302302
303- foreach (range (0 , 99 ) as $ count ) {
303+ foreach (range (1 , 20 ) as $ count ) {
304+ if ($ count === 11 ) {
305+ sleep (1 );
306+ }
307+
304308 $ article ->update ([
305- 'reviewed ' => ( $ count % 2 ) ,
309+ 'title ' => ' Title # ' . $ count ,
306310 ]);
307311 }
308312
309- $ this ->assertSame (10 , $ article ->audits ()->count ());
313+ $ audits = $ article ->audits ()->get ();
314+ $ this ->assertSame (10 , $ audits ->count ());
315+ $ this ->assertSame ('Title #11 ' , $ audits ->first ()->new_values ['title ' ]);
316+ $ this ->assertSame ('Title #20 ' , $ audits ->last ()->new_values ['title ' ]);
310317 }
311318
312319 /**
You can’t perform that action at this time.
0 commit comments