Skip to content
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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,6 @@
*/

'console' => false,

'placeholders_limit' => 50000,
];
22 changes: 19 additions & 3 deletions src/Drivers/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

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

->take(PHP_INT_MAX)
->pluck($auditKeyName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that it is more efficient to put the pluck above the slice, replacing the get


if (!$forRemoval->isEmpty()) {
$forRemovalChunks = $forRemoval->chunk(Config::get('audit.placeholders_limit', 50000));
Copy link

@parallels999 parallels999 Jun 26, 2024

Choose a reason for hiding this comment

The 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 leftJoin?

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;
Expand Down