-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix prune method #951
fix prune method #951
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,4 +195,6 @@ | |
*/ | ||
|
||
'console' => false, | ||
|
||
'placeholders_limit' => 50000, | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,26 @@ public function audit(Auditable $model): ?Audit | |
public function prune(Auditable $model): bool | ||
{ | ||
if (($threshold = $model->getAuditThreshold()) > 0) { | ||
return $model->audits() | ||
$class = get_class($model->audits()->getModel()); | ||
$auditKeyName = (new $class)->getKeyName(); | ||
|
||
$forRemoval = $model->audits() | ||
->latest() | ||
->offset($threshold)->limit(PHP_INT_MAX) | ||
->delete() > 0; | ||
->skip($threshold) | ||
->take(PHP_INT_MAX) | ||
->pluck($auditKeyName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to me that it is more efficient to put the |
||
|
||
if (!$forRemoval->isEmpty()) { | ||
$forRemovalChunks = $forRemoval->chunk(Config::get('audit.placeholders_limit', 50000)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you have thousands of records, they will all be stored in memory as models, wouldn't that be less efficient than a Also, there would be two queries versus just one. |
||
$answer = false; | ||
foreach ($forRemovalChunks as $chunk) { | ||
$answer = $model->audits() | ||
->whereIn($auditKeyName, $chunk) | ||
->delete() > 0 || $answer; | ||
} | ||
|
||
return $answer; | ||
} | ||
} | ||
|
||
return false; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
skip and take are alias of offset and limit