-
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 database prune, offset not working on some dbs #948
Conversation
erikn69
commented
Jun 12, 2024
•
edited
Loading
edited
- Closes Audited logs deleted on fresh install with threshold set to > 0 #947
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.
I was going to wait for @neil-cldagency to confirm his problem was resolved before reviewing but yeah, this is a great fix 👌
Is it worth changing the tests to use mysql rather than sqlite? Or running both somehow?
Hi all — apologies, been deep in development. Will look at this tomorrow AM (BST). |
It would be a good idea, I have seen other packages test various DRIVERs |
Getting a consistently occurring error when I attempt to edit any models while the threshold is > 0. Looks like you're using a query not all engines support. Fair enough if it's MySQL only but I presume the package should support at least 5.7+? Will defer to @parallels999 on that one!
MySQL version is 8.0.33-0ubuntu0.20.04.2 (supplied via ddev) |
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.
I didn't realise I already had a suitable test environment for this problem and I'm now sad that current MySQL versions don't support the elegance of this fix.
I think the query could be re-written as a join but writing one manually in Laravel makes my head hurt 😅
The other alternative would be getting the IDs first which seems to work in a pinch.
if (($threshold = $model->getAuditThreshold()) > 0) {
$class = get_class($model->audits()->getModel());
$keyName = (new $class)->getKeyName();
$keys = $model->audits()
->select($keyName)
->limit($threshold)
->latest()
->pluck($keyName);
return $model->audits()
->whereNotIn(
$keyName,
$keys
)
->delete() > 0;
}
I don't prune audits so don't have a bias any particular way.
Ok, i will reverse it for now, I think it would be the best since I cannot test old versions of the db at the moment
I'll give it a try |
From memory, Laravel itself makes use of "get IDs first then |
83a22b2
to
ba451e6
Compare
@willpower232, @neil-cldagency hi, could you test "join" version? |
Looking good to me, it behaves as I think it should in my weird setup. With a threshold of 1 the SQL looks like this and looks like it includes all the correct wheres delete `audits`
from `audits`
left join (
select `id`
from `audits`
where `audits`.`auditable_type` = "App\\Models\\User"
and `audits`.`auditable_id` = 3
and `audits`.`auditable_id` is not null
order by `created_at` desc limit 1
) as `audit_threshold` on `audits`.`id` = `audit_threshold`.`id`
where `audits`.`auditable_type` = "App\\Models\\User"
and `audits`.`auditable_id` = 3
and `audits`.`auditable_id` is not null
and `audit_threshold`.`id` is null |
this fix works, but not sure using left join is good idea for optimization |
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.
these fixes work
@neil-cldagency do you have time to test the fix or should we ship based on what else we've seen? |
@giolaza as you can read above, the left join is the best option, the most elegant solution isn't supported by mysql, and plucking out the IDs would eventually cause a problem for someone |
@willpower232 plucking IDs caused issues when you used it in a subquery with a limit. Maybe a better solution is using Laravel's Before the update, you were using pluck to get the IDs to delete. If we analyze the situation, I think this is the best solution. Situations:
|
@giolaza originally used laravel-auditing/src/Drivers/Database.php Line 32 in 27e1b31
|
Happy to let others test here @willpower232 as I'm knee deep in an implementation at the moment and testing involves reverting a FilamentPHP plugin each time 😅 |
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.
These changes fixed the problem of removing all audits so happy with this
Apologies all, I hadn't checked who regularly maintains this project. Thanks to everyone involved, though! |