-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from gussan/feature/rails51
Support rails 5.1
- Loading branch information
Showing
15 changed files
with
307 additions
and
98 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
source "https://rubygems.org" | ||
|
||
gem "activerecord", "5.1.0" | ||
gem "activesupport", "5.1.0" | ||
gem "actionview", "5.1.0" | ||
|
||
gemspec :path => '../' |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
source "https://rubygems.org" | ||
|
||
gem "activerecord", "5.1.1" | ||
gem "activesupport", "5.1.1" | ||
gem "actionview", "5.1.1" | ||
|
||
gemspec :path => '../' |
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
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
132 changes: 92 additions & 40 deletions
132
lib/active_record/turntable/active_record_ext/locking_optimistic.rb
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 |
---|---|---|
@@ -1,53 +1,105 @@ | ||
module ActiveRecord::Turntable | ||
module ActiveRecordExt | ||
module LockingOptimistic | ||
::ActiveRecord::Locking::Optimistic.class_eval <<-EOD | ||
private | ||
# @note Override to add sharding condition on optimistic locking | ||
def _update_record(attribute_names = self.attribute_names) #:nodoc: | ||
return super unless locking_enabled? | ||
return 0 if attribute_names.empty? | ||
klass = self.class | ||
lock_col = self.class.locking_column | ||
previous_lock_value = send(lock_col).to_i | ||
increment_lock | ||
attribute_names += [lock_col] | ||
attribute_names.uniq! | ||
begin | ||
relation = self.class.unscoped | ||
condition_scope = relation.where( | ||
self.class.primary_key => id, | ||
lock_col => previous_lock_value, | ||
) | ||
if klass.turntable_enabled? and klass.primary_key != klass.turntable_shard_key.to_s | ||
condition_scope = condition_scope.where( | ||
klass.turntable_shard_key => self.send(klass.turntable_shard_key) | ||
if Util.ar51_or_later? | ||
::ActiveRecord::Locking::Optimistic.class_eval <<-EOD | ||
private | ||
# @note Override to add sharding condition on optimistic locking | ||
def _update_record(attribute_names = self.attribute_names) | ||
return super unless locking_enabled? | ||
return 0 if attribute_names.empty? | ||
begin | ||
klass = self.class | ||
lock_col = self.class.locking_column | ||
previous_lock_value = read_attribute_before_type_cast(lock_col) | ||
increment_lock | ||
attribute_names.push(lock_col) | ||
relation = self.class.unscoped | ||
condition_scope = relation.where( | ||
self.class.primary_key => id, | ||
lock_col => previous_lock_value | ||
) | ||
end | ||
if klass.turntable_enabled? and klass.primary_key != klass.turntable_shard_key.to_s | ||
condition_scope = condition_scope.where( | ||
klass.turntable_shard_key => self.send(klass.turntable_shard_key) | ||
) | ||
end | ||
affected_rows = condition_scope.update_all( | ||
attributes_for_update(attribute_names).map do |name| | ||
[name, _read_attribute(name)] | ||
end.to_h | ||
) | ||
unless affected_rows == 1 | ||
raise ActiveRecord::StaleObjectError.new(self, "update") | ||
end | ||
affected_rows = condition_scope.update_all( | ||
attributes_for_update(attribute_names).map do |name| | ||
[name, _read_attribute(name)] | ||
end.to_h | ||
) | ||
affected_rows | ||
unless affected_rows == 1 | ||
raise ActiveRecord::StaleObjectError.new(self, "update") | ||
# If something went wrong, revert the locking_column value. | ||
rescue Exception | ||
send(lock_col + "=", previous_lock_value.to_i) | ||
raise | ||
end | ||
end | ||
EOD | ||
else | ||
::ActiveRecord::Locking::Optimistic.class_eval <<-EOD | ||
private | ||
# @note Override to add sharding condition on optimistic locking | ||
def _update_record(attribute_names = self.attribute_names) #:nodoc: | ||
return super unless locking_enabled? | ||
return 0 if attribute_names.empty? | ||
klass = self.class | ||
lock_col = self.class.locking_column | ||
previous_lock_value = send(lock_col).to_i | ||
increment_lock | ||
attribute_names += [lock_col] | ||
attribute_names.uniq! | ||
begin | ||
relation = self.class.unscoped | ||
condition_scope = relation.where( | ||
self.class.primary_key => id, | ||
lock_col => previous_lock_value, | ||
) | ||
if klass.turntable_enabled? and klass.primary_key != klass.turntable_shard_key.to_s | ||
condition_scope = condition_scope.where( | ||
klass.turntable_shard_key => self.send(klass.turntable_shard_key) | ||
) | ||
end | ||
affected_rows = condition_scope.update_all( | ||
attributes_for_update(attribute_names).map do |name| | ||
[name, _read_attribute(name)] | ||
end.to_h | ||
) | ||
affected_rows | ||
unless affected_rows == 1 | ||
raise ActiveRecord::StaleObjectError.new(self, "update") | ||
end | ||
# If something went wrong, revert the version. | ||
rescue Exception | ||
send(lock_col + '=', previous_lock_value) | ||
raise | ||
affected_rows | ||
# If something went wrong, revert the version. | ||
rescue Exception | ||
send(lock_col + '=', previous_lock_value) | ||
raise | ||
end | ||
end | ||
end | ||
EOD | ||
EOD | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.