From 84206ad38750840431ea0cbc490177ad61b11bfc Mon Sep 17 00:00:00 2001 From: Edouard CHIN Date: Tue, 16 Jan 2018 15:45:18 -0500 Subject: Added a test around `NO_AUTO_VALUE_ON_ZERO`: - The mysql `NO_AUTO_VALUE_ON_ZERO` mode should be disabled when inserting fixtures in bulk, this PR adds a test to make sure we don't remove it by mistake - If we live this mode enabled, a statement like this wouldn't work and a `Duplicate entry '0' for key 'PRIMARY'` error will be raised. That's because `DEFAULT` on auto_increment will return 0 ```sql INSERT INTO `aircraft` (`id`, `name`, `wheels_count`) VALUES (DEFAULT, 'first', 2), (DEFAULT, 'second', 3) ``` --- activerecord/test/cases/fixtures_test.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 8e8a49af8e..d692c0eccb 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -81,6 +81,28 @@ class FixturesTest < ActiveRecord::TestCase end end + def test_no_auto_value_on_zero_is_disabled + skip unless current_adapter?(:Mysql2Adapter) + + begin + fixtures = [ + { "name" => "first", "wheels_count" => 2 }, + { "name" => "second", "wheels_count" => 3 } + ] + subscriber = InsertQuerySubscriber.new + subscription = ActiveSupport::Notifications.subscribe("sql.active_record", subscriber) + + assert_nothing_raised do + ActiveRecord::Base.connection.insert_fixtures(fixtures, "aircraft") + end + + expected_sql = "INSERT INTO `aircraft` (`id`, `name`, `wheels_count`) VALUES (DEFAULT, 'first', 2), (DEFAULT, 'second', 3)" + assert_equal expected_sql, subscriber.events.first + ensure + ActiveSupport::Notifications.unsubscribe(subscription) + end + end + def test_broken_yaml_exception badyaml = Tempfile.new ["foo", ".yml"] badyaml.write "a: : " -- cgit v1.2.3