1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Dynamically modified schema and association would not be correctly reset

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
This commit is contained in:
Akira Matsuda 2014-09-06 20:06:40 +09:00
parent e11914b551
commit da2f61947d
4 changed files with 21 additions and 16 deletions

View file

@ -5,6 +5,7 @@ require 'models/job'
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'
@ -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

View file

@ -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"

View file

@ -0,0 +1,4 @@
class PersonalLegacyThing < ActiveRecord::Base
self.locking_column = :version
belongs_to :person, :counter_cache => true
end

View file

@ -546,6 +546,12 @@ ActiveRecord::Schema.define do
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