From 1d04baafa6e79ec8892f9159784617dca8deea1e Mon Sep 17 00:00:00 2001 From: Edouard CHIN Date: Tue, 23 Jan 2018 14:21:58 -0500 Subject: Allow a 2 bytes margin: - mysql will add a 2 bytes margin to the statement, so given a `max_allowed_packet` set to 1024 bytes, a 1024 bytes fixtures will no be inserted (mysql will throw an error) - Preventing this by decreasing the max_allowed_packet by 2 bytes when doing the comparison with the actual statement size --- .../active_record/connection_adapters/abstract_mysql_adapter.rb | 3 ++- activerecord/test/cases/fixtures_test.rb | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 5e6ab1c16d..072a5337a5 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -568,7 +568,8 @@ module ActiveRecord end def max_allowed_packet - @max_allowed_packet ||= show_variable("max_allowed_packet") + bytes_margin = 2 + @max_allowed_packet ||= (show_variable("max_allowed_packet") - bytes_margin) end def with_multi_statements diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 13034eef22..de5c96daaa 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -119,17 +119,19 @@ class FixturesTest < ActiveRecord::TestCase if current_adapter?(:Mysql2Adapter) def test_insert_fixtures_set_raises_an_error_when_max_allowed_packet_is_smaller_than_fixtures_set_size conn = ActiveRecord::Base.connection + mysql_margin = 2 packet_size = 1024 + bytes_needed_to_have_a_1024_bytes_fixture = 855 fixtures = { "traffic_lights" => [ - { "location" => "US", "state" => ["NY"], "long_state" => ["a" * packet_size] }, + { "location" => "US", "state" => ["NY"], "long_state" => ["a" * bytes_needed_to_have_a_1024_bytes_fixture] }, ] } - conn.stubs(:max_allowed_packet).returns(packet_size) + conn.stubs(:max_allowed_packet).returns(packet_size - mysql_margin) error = assert_raises(ActiveRecord::ActiveRecordError) { conn.insert_fixtures_set(fixtures) } - assert_match(/Fixtures set is too large/, error.message) + assert_match(/Fixtures set is too large #{packet_size}\./, error.message) end def test_insert_fixture_set_when_max_allowed_packet_is_bigger_than_fixtures_set_size -- cgit v1.2.3