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

Support for auditSync and auditDetach on Auditable custom Pivot class #954

Merged
merged 9 commits into from
Feb 26, 2025

Conversation

txdFrancesco
Copy link
Contributor

Allow the use of auditSync and auditDetach on BelongsToMany relations where the pivot class is an Auditable class.

This is useful in cases where the relation model is itself a Laravel model and may be updated (and thus audited) indipendently from the related models, without requring dedicated code around every ->auditSync call on the related models.

Copy link
Contributor

@willpower232 willpower232 left a comment

Choose a reason for hiding this comment

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

are you able to correct your spaces to match the rest of the code and add some tests for the functionality you've added?

@txdFrancesco
Copy link
Contributor Author

For the spaces sure, for the tests I'll need to add a couple of models to reproduce.
Users -> GroupMembers (with 'role' column) -> Groups could be an example of my case. Let me know if you have a better/preferred example structure. I'll update when I'm done, thanks

@txdFrancesco
Copy link
Contributor Author

I added some tests that cover sync, attach and detach with an Auditable custom Pivot class.
AuditAttach is not affected by my code change, I put in the test anyway for completeness. Let me know if it is better to remove it.

The other tests are failing without the code change and are passing now.

@txdFrancesco txdFrancesco requested a review from willpower232 July 4, 2024 14:10
Copy link
Contributor

@willpower232 willpower232 left a comment

Choose a reason for hiding this comment

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

some final spaces and maybe wake up github actions

@bmoex
Copy link

bmoex commented Oct 24, 2024

I think auditAttach should be checked too if you want to implement fully

@txdFrancesco
Copy link
Contributor Author

txdFrancesco commented Oct 24, 2024

some final spaces and maybe wake up github actions

@willpower232 Sorry for the inconvenience, I'm not sure what should I do to make this pull request go through.
The spacing was fixed by your commit (I just fixed one more line), but it's not clear to me what you mean with "wake up github actions". Could you explain or point me in the right direction please?
Thank you

@txdFrancesco
Copy link
Contributor Author

I think auditAttach should be checked too if you want to implement fully

The bug was caused by the way the detach method creates the query to delete the pivot class (creating a new instance and not retrieving the one in the database). Attaching is not affected by it (there is no instance to retrieve), applying the changes would just remove the "created" audit on the pivot class, which is against the idea of having an auditable pivot class.

A full solution would be to rework how the detach method builds the query instead of wrapping with withoutAuditing, but frankly I don't know the codebase enough to make such an impactful change for what is an edge usecase.

@willpower232
Copy link
Contributor

I was confused as to why github actions wasn't working but it seems to be behaving now, I'll re read the PR shortly

@onlime
Copy link
Contributor

onlime commented Jan 30, 2025

The bug was caused by the way the detach method creates the query to delete the pivot class (creating a new instance and not retrieving the one in the database). Attaching is not affected by it (there is no instance to retrieve), applying the changes would just remove the "created" audit on the pivot class, which is against the idea of having an auditable pivot class.

Thanks a lot @txdFrancesco for this explanation. Would be super nice if this could be supported in the future.

@MortenDHansen is there something in the works for V14? f49ab23 looks promising, or at least it's a smart refactor to move that logic into a separate AuditsPivotRecords trait.
Thanks a lot for this great package and very cool to see some progress here.

Copy link
Contributor

@willpower232 willpower232 left a comment

Choose a reason for hiding this comment

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

seems fine to me, not sure when is good to merge this @erikn69 perhaps just needs a pint/phpstan run after merging your other PR?

Copy link
Contributor

@willpower232 willpower232 left a comment

Choose a reason for hiding this comment

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

@txdFrancesco are you able to update the PR now that the factories are modernised?

Copy link
Contributor Author

@txdFrancesco txdFrancesco left a comment

Choose a reason for hiding this comment

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

Should be done, test are passing on my machine. @willpower232 can you approve the action workflow? here it says it needs approval from a mantainer

Copy link
Contributor

@willpower232 willpower232 left a comment

Choose a reason for hiding this comment

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

looking good to me, thanks for the quick response

@erikn69 time for merging and v14?

@erikn69
Copy link
Contributor

erikn69 commented Feb 26, 2025

@willpower232 the tests were not run on this change, I won't have time to review this for at least a couple of weeks, if you reviewed it and it won't cause problems feel free to merge it and make the release for v14, you would also have to put the note for the event breaking change (I couldn't find why it works differently in certain scenarios)

@willpower232 willpower232 merged commit f92602d into owen-it:master Feb 26, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants