diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-12-12 16:47:51 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-12 16:47:51 -0500 |
commit | 185a30f75289ab158abd3c21536930c37af61338 (patch) | |
tree | 265389a1c174006be2b5200ff4609ae6b7a31093 /activerecord/test | |
parent | 2eee8c3bad674bc3170cfac3e8586127560a4ea5 (diff) | |
parent | 6ddba9b46c1d307ea7e03d753d0988183b9cb80c (diff) | |
download | rails-185a30f75289ab158abd3c21536930c37af61338.tar.gz rails-185a30f75289ab158abd3c21536930c37af61338.tar.bz2 rails-185a30f75289ab158abd3c21536930c37af61338.zip |
Merge pull request #31405 from bogdanvlviv/fix-conflicts-counter_cache-with-touch-by-optimistic_locking
Fix conflicts `counter_cache` with `touch: true` by optimistic locking.
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/locking_test.rb | 34 | ||||
-rw-r--r-- | activerecord/test/models/wheel.rb | 2 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 3 |
3 files changed, 36 insertions, 3 deletions
diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index e857180bd1..437a5a38a3 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -399,11 +399,43 @@ class OptimisticLockingTest < ActiveRecord::TestCase end end + def test_counter_cache_with_touch_and_lock_version + car = Car.create! + + assert_equal 0, car.wheels_count + assert_equal 0, car.lock_version + + previously_car_updated_at = car.updated_at + travel(1.second) do + Wheel.create!(wheelable: car) + end + + assert_equal 1, car.reload.wheels_count + assert_not_equal previously_car_updated_at, car.updated_at + assert_equal 1, car.lock_version + + previously_car_updated_at = car.updated_at + car.wheels.first.update(size: 42) + + assert_equal 1, car.reload.wheels_count + assert_not_equal previously_car_updated_at, car.updated_at + assert_equal 2, car.lock_version + + previously_car_updated_at = car.updated_at + travel(1.second) do + car.wheels.first.destroy! + end + + assert_equal 0, car.reload.wheels_count + assert_not_equal previously_car_updated_at, car.updated_at + assert_equal 3, car.lock_version + end + def test_polymorphic_destroy_with_dependencies_and_lock_version car = Car.create! assert_difference "car.wheels.count" do - car.wheels << Wheel.create! + car.wheels.create end assert_difference "car.wheels.count", -1 do car.reload.destroy diff --git a/activerecord/test/models/wheel.rb b/activerecord/test/models/wheel.rb index e05fb64477..8db57d181e 100644 --- a/activerecord/test/models/wheel.rb +++ b/activerecord/test/models/wheel.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class Wheel < ActiveRecord::Base - belongs_to :wheelable, polymorphic: true, counter_cache: true + belongs_to :wheelable, polymorphic: true, counter_cache: true, touch: true end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index bf66846840..3205c4c20a 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -123,7 +123,7 @@ ActiveRecord::Schema.define do create_table :cars, force: true do |t| t.string :name t.integer :engines_count - t.integer :wheels_count + t.integer :wheels_count, default: 0 t.column :lock_version, :integer, null: false, default: 0 t.timestamps null: false end @@ -964,6 +964,7 @@ ActiveRecord::Schema.define do end create_table :wheels, force: true do |t| + t.integer :size t.references :wheelable, polymorphic: true end |