Skip to content

Commit

Permalink
Allow a 2 bytes margin:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Edouard-chin committed Jan 23, 2018
1 parent 9a5b1fa commit 1d04baa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,8 @@ def max_allowed_packet_reached?(current_packet, previous_packet)
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
Expand Down
8 changes: 5 additions & 3 deletions activerecord/test/cases/fixtures_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,19 @@ def test_bulk_insert_with_a_multi_statement_query_raises_an_exception_when_any_i
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
Expand Down

0 comments on commit 1d04baa

Please sign in to comment.