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

Don't mess with column_defaults when optimistic locking is enabled #15771

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,10 @@ class Base
include Integration
include Validations
include CounterCache
include Locking::Optimistic
include Locking::Pessimistic
include Attributes
include AttributeDecorators
include Locking::Optimistic
include Locking::Pessimistic
include AttributeMethods
include Callbacks
include Timestamp
Expand Down
33 changes: 23 additions & 10 deletions activerecord/lib/active_record/locking/optimistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ module Optimistic
included do
class_attribute :lock_optimistically, instance_writer: false
self.lock_optimistically = true

is_lock_column = ->(name, _) { lock_optimistically && name == locking_column }
decorate_matching_attribute_types(is_lock_column, :_optimistic_locking) do |type|
LockingType.new(type)
end
end

def locking_enabled? #:nodoc:
Expand Down Expand Up @@ -141,7 +146,7 @@ def locking_enabled?

# Set the column to use for optimistic locking. Defaults to +lock_version+.
def locking_column=(value)
@column_defaults = nil
clear_caches_calculated_from_columns
@locking_column = value.to_s
end

Expand All @@ -162,18 +167,26 @@ def update_counters(id, counters)
counters = counters.merge(locking_column => 1) if locking_enabled?
super
end
end
end

def column_defaults
@column_defaults ||= begin
defaults = super
class LockingType < SimpleDelegator
def type_cast_from_database(value)
# `nil` *should* be changed to 0
super.to_i
end

if defaults.key?(locking_column) && lock_optimistically
defaults[locking_column] ||= 0
end
def changed?(old_value, *)
# Ensure we save if the default was `nil`
super || old_value == 0
end

defaults
end
end
def init_with(coder)
__setobj__(coder['subtype'])
end

def encode_with(coder)
coder['subtype'] = __getobj__
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/locking_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ def test_removing_has_and_belongs_to_many_associations_upon_destroy
assert p.treasures.empty?
assert RichPerson.connection.select_all("SELECT * FROM peoples_treasures WHERE rich_person_id = 1").empty?
end

def test_yaml_dumping_with_lock_column
t1 = LockWithoutDefault.new
t2 = YAML.load(YAML.dump(t1))

assert_equal t1.attributes, t2.attributes
end
end

class OptimisticLockingWithSchemaChangeTest < ActiveRecord::TestCase
Expand Down