-
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
Add support for setting meta without transaction block #143
Merged
palkan
merged 10 commits into
palkan:master
from
oleg-kiviljov:support-meta-without-transaction
Nov 29, 2019
+97
−18
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
880c32a
Add support for setting meta without transaction block
oleg-kiviljov e3fb870
Extract duplicated methods into MetaWrapper class
oleg-kiviljov b9c78b3
Add tests for transactional:false setting
oleg-kiviljov 37492c7
Refactor Meta.wrap_with method
oleg-kiviljov 123f334
Raise ArgumentError if with_meta was called without block
oleg-kiviljov 3dc9ec9
Fix rubocop offense
oleg-kiviljov 7bbc011
Extract call_block_in_meta_context method into MetaWrapper class
oleg-kiviljov 3716831
Add changelog entry
oleg-kiviljov 4ea0375
Add documentation about transactional option to README
oleg-kiviljov cd37fab
Add nickname shortcut to changelog
oleg-kiviljov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,19 @@ | |
module Logidze # :nodoc: | ||
# Provide methods to attach meta information | ||
module Meta | ||
def with_meta(meta, &block) | ||
MetaTransaction.wrap_with(meta, &block) | ||
def with_meta(meta, transactional: true, &block) | ||
palkan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
wrapper = transactional ? MetaWithTransaction : MetaWithoutTransaction | ||
wrapper.wrap_with(meta, &block) | ||
end | ||
|
||
def with_responsible(responsible_id, &block) | ||
def with_responsible(responsible_id, transactional: true, &block) | ||
return yield if responsible_id.nil? | ||
|
||
meta = {Logidze::History::Version::META_RESPONSIBLE => responsible_id} | ||
with_meta(meta, &block) | ||
with_meta(meta, transactional: transactional, &block) | ||
end | ||
|
||
class MetaTransaction # :nodoc: | ||
class MetaWrapper # :nodoc: | ||
def self.wrap_with(meta, &block) | ||
new(meta, &block).perform | ||
end | ||
|
@@ -29,25 +30,22 @@ def initialize(meta, &block) | |
end | ||
|
||
def perform | ||
return if block.nil? | ||
raise ArgumentError, "Block must be given" unless block | ||
return block.call if meta.nil? | ||
|
||
ActiveRecord::Base.transaction { call_block_in_meta_context } | ||
call_block_in_meta_context | ||
end | ||
|
||
private | ||
|
||
def call_block_in_meta_context | ||
prev_meta = current_meta | ||
|
||
meta_stack.push(meta) | ||
|
||
pg_set_meta_param(current_meta) | ||
result = block.call | ||
pg_reset_meta_param(prev_meta) | ||
|
||
result | ||
ensure | ||
pg_reset_meta_param(prev_meta) | ||
meta_stack.pop | ||
end | ||
|
||
|
@@ -60,20 +58,49 @@ def meta_stack | |
Thread.current[:meta] | ||
end | ||
|
||
def pg_set_meta_param(value) | ||
encoded_meta = connection.quote(ActiveSupport::JSON.encode(value)) | ||
connection.execute("SET LOCAL logidze.meta = #{encoded_meta};") | ||
def encode_meta(value) | ||
connection.quote(ActiveSupport::JSON.encode(value)) | ||
end | ||
|
||
def pg_reset_meta_param(prev_meta) | ||
if prev_meta.empty? | ||
connection.execute("SET LOCAL logidze.meta TO DEFAULT;") | ||
pg_clear_meta_param | ||
else | ||
pg_set_meta_param(prev_meta) | ||
end | ||
end | ||
end | ||
|
||
private_constant :MetaTransaction | ||
class MetaWithTransaction < MetaWrapper # :nodoc: | ||
private | ||
|
||
def call_block_in_meta_context | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, we can move this to the base class ( |
||
connection.transaction { super } | ||
end | ||
|
||
def pg_set_meta_param(value) | ||
connection.execute("SET LOCAL logidze.meta = #{encode_meta(value)};") | ||
end | ||
|
||
def pg_clear_meta_param | ||
connection.execute("SET LOCAL logidze.meta TO DEFAULT;") | ||
end | ||
end | ||
|
||
class MetaWithoutTransaction < MetaWrapper # :nodoc: | ||
private | ||
|
||
def pg_set_meta_param(value) | ||
connection.execute("SET logidze.meta = #{encode_meta(value)};") | ||
end | ||
|
||
def pg_clear_meta_param | ||
connection.execute("SET logidze.meta TO DEFAULT;") | ||
end | ||
end | ||
|
||
private_constant :MetaWrapper | ||
private_constant :MetaWithTransaction | ||
private_constant :MetaWithoutTransaction | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
One more thing: please, add the value for your nickname's shortcut to the end of this file.