class OptimisticLockingWithSchemaChangeTest

Public Instance Methods

test_destroy_dependents() click to toggle source

See Lighthouse ticket #1966

# File activerecord/test/cases/locking_test.rb, line 460
def test_destroy_dependents
  # Establish dependent relationship between Person and PersonalLegacyThing
  add_counter_column_to(Person, "personal_legacy_things_count")
  PersonalLegacyThing.reset_column_information

  # Make sure that counter incrementing doesn't cause problems
  p1 = Person.new(first_name: "fjord")
  p1.save!
  t = PersonalLegacyThing.new(person: p1)
  t.save!
  p1.reload
  assert_equal 1, p1.personal_legacy_things_count
  assert p1.destroy
  assert_equal true, p1.frozen?
  assert_raises(ActiveRecord::RecordNotFound) { Person.find(p1.id) }
  assert_raises(ActiveRecord::RecordNotFound) { PersonalLegacyThing.find(t.id) }
ensure
  remove_counter_column_from(Person, "personal_legacy_things_count")
  PersonalLegacyThing.reset_column_information
end
test_destroy_existing_object_with_locking_column_value_null_in_the_database() click to toggle source
# File activerecord/test/cases/locking_test.rb, line 481
def test_destroy_existing_object_with_locking_column_value_null_in_the_database
  ActiveRecord::Base.connection.execute("INSERT INTO lock_without_defaults(title) VALUES('title1')")
  t1 = LockWithoutDefault.last

  assert_equal 0, t1.lock_version
  assert_nil t1.lock_version_before_type_cast

  t1.destroy

  assert t1.destroyed?
end
test_destroy_stale_object() click to toggle source
# File activerecord/test/cases/locking_test.rb, line 493
def test_destroy_stale_object
  t1 = LockWithoutDefault.create!(title: "title1")
  stale_object = LockWithoutDefault.find(t1.id)

  t1.update!(title: "title2")

  assert_raises(ActiveRecord::StaleObjectError) do
    stale_object.destroy!
  end

  refute stale_object.destroyed?
end

Private Instance Methods

add_counter_column_to(model, col = "test_count") click to toggle source
# File activerecord/test/cases/locking_test.rb, line 508
def add_counter_column_to(model, col = "test_count")
  model.connection.add_column model.table_name, col, :integer, null: false, default: 0
  model.reset_column_information
end
counter_test(model, expected_count) { |id| ... } click to toggle source
# File activerecord/test/cases/locking_test.rb, line 518
def counter_test(model, expected_count)
  add_counter_column_to(model)
  object = model.first
  assert_equal 0, object.test_count
  assert_equal 0, object.send(model.locking_column)
  yield object.id
  object.reload
  assert_equal expected_count, object.test_count
  assert_equal 1, object.send(model.locking_column)
ensure
  remove_counter_column_from(model)
end
remove_counter_column_from(model, col = :test_count) click to toggle source
# File activerecord/test/cases/locking_test.rb, line 513
def remove_counter_column_from(model, col = :test_count)
  model.connection.remove_column model.table_name, col
  model.reset_column_information
end