Adding in support for :synchronization option for #import. Pulled in from ar-extensions. Currently only works with MySQL since that is the only database that supports on duplicate key update functionality.

https://github.com/zdennis/activerecord-import/issues#issue/6
This commit is contained in:
Zach Dennis 2011-03-19 15:49:26 -04:00
parent 319c52d80f
commit e00e9d7d59
2 changed files with 27 additions and 1 deletions

View file

@ -142,4 +142,30 @@ def should_support_mysql_import_functionality
end
end
describe "#import with :synchronization option" do
let(:topics){ Array.new }
let(:values){ [ [topics.first.id, "Jerry Carter"], [topics.last.id, "Chad Fowler"] ]}
let(:columns){ %W(id author_name) }
setup do
topics << Topic.create!(:title=>"LDAP", :author_name=>"Big Bird")
topics << Topic.create!(:title=>"Rails Recipes", :author_name=>"Elmo")
end
it "synchronizes passed in ActiveRecord model instances with the data just imported" do
columns2update = [ 'author_name' ]
expected_count = Topic.count
Topic.import( columns, values,
:validate=>false,
:on_duplicate_key_update=>columns2update,
:synchronize=>topics )
assert_equal expected_count, Topic.count, "no new records should have been created!"
assert_equal "Jerry Carter", topics.first.author_name, "wrong author!"
assert_equal "Chad Fowler", topics.last.author_name, "wrong author!"
end
end
end