Skip to content

Commit

Permalink
Dynamically modified schema and association would not be correctly reset
Browse files Browse the repository at this point in the history
This fixes <"SQLite3::SQLException: no such column: legacy_things.person_id: SELECT \"legacy_things\".* FROM \"legacy_things\" WHERE \"legacy_things\".\"person_id\" = ?">
in OptimisticLockingTest#test_lock_destroy
  • Loading branch information
amatsuda committed Sep 6, 2014
1 parent e11914b commit da2f619
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
25 changes: 9 additions & 16 deletions activerecord/test/cases/locking_test.rb
Expand Up @@ -5,6 +5,7 @@
require 'models/reader'
require 'models/ship'
require 'models/legacy_thing'
require 'models/personal_legacy_thing'
require 'models/reference'
require 'models/string_key_object'
require 'models/car'
Expand Down Expand Up @@ -311,32 +312,24 @@ class OptimisticLockingWithSchemaChangeTest < ActiveRecord::TestCase

# See Lighthouse ticket #1966
def test_destroy_dependents
# Establish dependent relationship between People and LegacyThing
add_counter_column_to(Person, 'legacy_things_count')
LegacyThing.connection.add_column LegacyThing.table_name, 'person_id', :integer
LegacyThing.reset_column_information
LegacyThing.class_eval do
belongs_to :person, :counter_cache => true
end
Person.class_eval do
has_many :legacy_things, :dependent => :destroy
end
# 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 = LegacyThing.new(:person => p1)
t = PersonalLegacyThing.new(:person => p1)
t.save!
p1.reload
assert_equal 1, p1.legacy_things_count
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) { LegacyThing.find(t.id) }
assert_raises(ActiveRecord::RecordNotFound) { PersonalLegacyThing.find(t.id) }
ensure
remove_counter_column_from(Person, 'legacy_things_count')
LegacyThing.connection.remove_column LegacyThing.table_name, 'person_id'
LegacyThing.reset_column_information
remove_counter_column_from(Person, 'personal_legacy_things_count')
PersonalLegacyThing.reset_column_information
end

private
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/models/person.rb
Expand Up @@ -30,6 +30,8 @@ class Person < ActiveRecord::Base
has_many :agents_of_agents, :through => :agents, :source => :agents
belongs_to :number1_fan, :class_name => 'Person'

has_many :personal_legacy_things, :dependent => :destroy

has_many :agents_posts, :through => :agents, :source => :posts
has_many :agents_posts_authors, :through => :agents_posts, :source => :author
has_many :essays, primary_key: "first_name", foreign_key: "writer_id"
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/models/personal_legacy_thing.rb
@@ -0,0 +1,4 @@
class PersonalLegacyThing < ActiveRecord::Base
self.locking_column = :version
belongs_to :person, :counter_cache => true
end
6 changes: 6 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -546,6 +546,12 @@ def except(adapter_names_to_exclude)
t.column :treasure_id, :integer
end

create_table :personal_legacy_things, force: true do |t|
t.integer :tps_report_number
t.integer :person_id
t.integer :version, null: false, default: 0
end

create_table :pets, primary_key: :pet_id, force: true do |t|
t.string :name
t.integer :owner_id, :integer
Expand Down

0 comments on commit da2f619

Please sign in to comment.