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:
parent
319c52d80f
commit
e00e9d7d59
|
@ -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
|
|
@ -19,4 +19,4 @@ describe ".synchronize" do
|
|||
assert_equal "#{titles[1]}_haha", actual_titles[1], "the second record was not correctly updated"
|
||||
assert_equal "#{titles[2]}_haha", actual_titles[2], "the third record was not correctly updated"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue