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
23
23
public function prune (Auditable $ model ): bool
24
24
{
25
25
if (($ threshold = $ model ->getAuditThreshold ()) > 0 ) {
26
+ $ auditClass = get_class ($ model ->audits ()->getModel ());
27
+ $ auditModel = new $ auditClass ;
28
+
26
29
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 ())
29
42
->delete () > 0 ;
30
43
}
31
44
Original file line number Diff line number Diff line change @@ -297,16 +297,23 @@ public function itWillRemoveOlderAuditsAboveTheThreshold()
297
297
]);
298
298
299
299
$ article = factory (Article::class)->create ([
300
- 'reviewed ' => 1 ,
300
+ 'title ' => ' Title #0 ' ,
301
301
]);
302
302
303
- foreach (range (0 , 99 ) as $ count ) {
303
+ foreach (range (1 , 20 ) as $ count ) {
304
+ if ($ count === 11 ) {
305
+ sleep (1 );
306
+ }
307
+
304
308
$ article ->update ([
305
- 'reviewed ' => ( $ count % 2 ) ,
309
+ 'title ' => ' Title # ' . $ count ,
306
310
]);
307
311
}
308
312
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 ' ]);
310
317
}
311
318
312
319
/**
You can’t perform that action at this time.
0 commit comments