add DB level propagation for the Unscoped flag #7007
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What did this pull request do?
This PR adds
PropagateUnscoped
toDB
'sConfig
. Its purpose is to allow propagation of the parentStatement
'sUnscoped
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 theUnscoped
state whenclone == 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 theUnscoped
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 nestedDelete
orModel
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.