aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/fixtures_test.rb
diff options
context:
space:
mode:
authorKir Shatrov <shatrov@me.com>2017-06-18 11:14:52 -0400
committerKir Shatrov <shatrov@me.com>2017-06-20 13:18:53 -0400
commit4ee42379cc30a07a7d47b7be8c710dba0efa407a (patch)
tree19f5e734b0751771edcb80050524760353928031 /activerecord/test/cases/fixtures_test.rb
parent09cb26bc1e653999827cf3eb955d42a2c932b3f5 (diff)
downloadrails-4ee42379cc30a07a7d47b7be8c710dba0efa407a.tar.gz
rails-4ee42379cc30a07a7d47b7be8c710dba0efa407a.tar.bz2
rails-4ee42379cc30a07a7d47b7be8c710dba0efa407a.zip
Use bulk INSERT to insert fixtures
Improves the performance from O(n) to O(1). Previously it would require 50 queries to insert 50 fixtures. Now it takes only one query. Disabled on sqlite which doesn't support multiple inserts.
Diffstat (limited to 'activerecord/test/cases/fixtures_test.rb')
-rw-r--r--activerecord/test/cases/fixtures_test.rb32
1 files changed, 31 insertions, 1 deletions
diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb
index a0a6d3c7ef..b499e60922 100644
--- a/activerecord/test/cases/fixtures_test.rb
+++ b/activerecord/test/cases/fixtures_test.rb
@@ -54,6 +54,31 @@ class FixturesTest < ActiveRecord::TestCase
end
end
+ class InsertQuerySubscriber
+ attr_reader :events
+
+ def initialize
+ @events = []
+ end
+
+ def call(_, _, _, _, values)
+ @events << values[:sql] if values[:sql] =~ /INSERT/
+ end
+ end
+
+ if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
+ def test_bulk_insert
+ begin
+ subscriber = InsertQuerySubscriber.new
+ subscription = ActiveSupport::Notifications.subscribe("sql.active_record", subscriber)
+ create_fixtures("bulbs")
+ assert_equal 1, subscriber.events.size, "It takes one INSERT query to insert two fixtures"
+ ensure
+ ActiveSupport::Notifications.unsubscribe(subscription)
+ end
+ end
+ end
+
def test_broken_yaml_exception
badyaml = Tempfile.new ["foo", ".yml"]
badyaml.write "a: : "
@@ -248,7 +273,12 @@ class FixturesTest < ActiveRecord::TestCase
e = assert_raise(ActiveRecord::Fixture::FixtureError) do
ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT + "/naked/yml", "parrots")
end
- assert_equal(%(table "parrots" has no column named "arrr".), e.message)
+
+ if current_adapter?(:SQLite3Adapter)
+ assert_equal(%(table "parrots" has no column named "arrr".), e.message)
+ else
+ assert_equal(%(table "parrots" has no columns named "arrr", "foobar".), e.message)
+ end
end
def test_yaml_file_with_symbol_columns