提交 1d04baaf 编写于 作者: E Edouard CHIN

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
上级 9a5b1fad
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册