File tree Expand file tree Collapse file tree 1 file changed +13
-15
lines changed
Expand file tree Collapse file tree 1 file changed +13
-15
lines changed Original file line number Diff line number Diff line change @@ -24,22 +24,20 @@ public function prune(Auditable $model): bool
2424 if (($ threshold = $ model ->getAuditThreshold ()) > 0 ) {
2525 $ auditClass = get_class ($ model ->audits ()->getModel ());
2626 $ auditModel = new $ auditClass ;
27+ $ keyName = $ auditModel ->getKeyName ();
2728
28- return $ model ->audits ()
29- ->leftJoinSub (
30- $ model ->audits ()->getQuery ()
31- ->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 ())
42- ->delete () > 0 ;
29+ $ query = $ model ->audits ()->latest ()->select ($ keyName );
30+
31+ if ($ query ->count () <= $ threshold ) {
32+ return false ;
33+ }
34+
35+ $ query ->chunk ($ threshold , function () {});
36+ $ query ->chunk (1000 , function ($ chunk ) use ($ auditClass , $ keyName ) {
37+ $ auditClass ::whereIn ($ keyName , $ chunk ->pluck ($ keyName ))->delete ();
38+ });
39+
40+ return true ;
4341 }
4442
4543 return false ;
You can’t perform that action at this time.
0 commit comments