Permalink
Browse files

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
  • Loading branch information...
zdennis committed Mar 19, 2011
1 parent 319c52d commit e00e9d7d5925d42ee454d29134fa73435d8b0b39
Showing with 27 additions and 1 deletion.
  1. +26 −0 test/support/mysql/import_examples.rb
  2. +1 −1 test/synchronize_test.rb
@@ -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
View
@@ -19,4 +19,4 @@
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

0 comments on commit e00e9d7

Please sign in to comment.