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

add DB level propagation for the Unscoped flag #7007

Merged
merged 3 commits into from
Jun 17, 2024
Merged

Conversation

sprataa
Copy link
Contributor

@sprataa sprataa commented May 6, 2024

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

This PR adds PropagateUnscoped to DB's Config. Its purpose is to allow propagation of the parent Statement's Unscoped flag to nested statements.

Should this become the default behavior?

Is there any scenario where getInstance() where we don't want this flag to propagate? For the record, all tests pass if we just persist the Unscoped state when clone == 1.

I just didn't want to introduce what may be a breaking change in more complex production environments for which gorm's tests don't offer coverage therefore this is a disabled by default flag.

Regardless, this kind of change should not be introduced without a flag to panic control it.

User Case Description

When one wants to create advanced hooks which leverage not just the usual Before/After but also StatementModifier hooks while reusing the Unscoped state of the parent statement, it can be expected that the flag propagates to all child hooks.

One can, and that's what we find ourselves doing more often than not, is to store the original tx/stmt's Unscoped state and adding repeated extra lines of code where one wants to keep the state after a nested Delete or Model calls done inside hooks - arguably, in every possibly nested hook.

Since for some, or even all, use cases, one just wants to have Unscoped propagated to all nested statements, this is a way cleaner and non intrusive solution to address such challenge.

}
DB.Model(&Product6{}).Create(&p)

DB.PropagateUnscoped = true
Copy link
Member

Choose a reason for hiding this comment

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

This is not thread-safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

indeed. pushed a fix and updated the test

@jinzhu jinzhu merged commit 109f239 into go-gorm:master Jun 17, 2024
31 of 34 checks passed
@jinzhu
Copy link
Member

jinzhu commented Jun 17, 2024

Can you add some doc to gorm.io, thank you

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.

2 participants