-
Notifications
You must be signed in to change notification settings - Fork 79
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 with_meta without transactions #136
Labels
Comments
Hi @palkan, are there any ideas how this could be implemented? |
I have one idea which could described as follows: # add an option to specify whether we want a transaction or not
# (this is mostly for backward compatibility)
def with_meta(data, transactional: true, &block)
# transactional_with_meta is the current implementation
return MetaTransaction.wrap(data, &block) if transactional
MetaNoTransaction.wrap(data, &block)
end
# the same as current MetaTransaction with the only one change
class Meta
def perform
raise ArgumentError, "Block must be given" unless block
call_block_in_meta_context
end
def call_block_in_meta_context
prev_meta = current_meta
meta_stack.push(meta)
pg_set_meta_param(current_meta)
result = block.call
result
ensure
# That's the only difference: we move reset to ensure to call it in case of exceptions
pg_reset_meta_param(prev_meta)
meta_stack.pop
end
end
class MetaTransaction < BaseMeta
def call_block_in_meta_context
ActiveRecord::Base.transaction { super }
end
end I hope, that should be enough) |
Hi @palkan, I don't really get how the SET LOCAL is done without transaction based on your example. |
Yeah, there is the point missing; when we're not within a transaction, we can only use |
Closed by #143 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem
Currently, wrapping execution with
Logidze.with_meta
(orLogidze.with_responsible
) call also wraps everything into a DB transaction, i.e.:That could lead to an unexpected behavior, especially, when using
.with_responsible
within anaround_action
hook: all the DB operations happened within the action could be rollbacked in case of the exception. Thus, wrapping everything into.with_responsible
significantly changes the way the application works.Potential Solution
We need to investigate the possibility of removing the transaction from
.with_meta
. The reason why it was used is to guarantee the state at the end of the block at the DB side (that's the waySET LOCAL
works in transactions).If it's possible then we need to upgrade the current behavior by adding a
transactional: true|false
option for.with_meta
/.with_responsible
methods withtrue
by default.Also, we might consider adding a global switch (
Logidze.transactional_meta = true | false
).The text was updated successfully, but these errors were encountered: