Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use set_server_option if possible #33363

Merged
merged 1 commit into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -559,14 +559,32 @@ def max_allowed_packet
end

def with_multi_statements
previous_flags = @config[:flags]
@config[:flags] = Mysql2::Client::MULTI_STATEMENTS
reconnect!
if supports_set_server_option?
@connection.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_ON)
elsif !supports_multi_statements?
previous_flags = @config[:flags]
@config[:flags] = Mysql2::Client::MULTI_STATEMENTS
reconnect!
end

yield
ensure
@config[:flags] = previous_flags
reconnect!
unless supports_multi_statements?
if supports_set_server_option?
@connection.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_OFF)
else
@config[:flags] = previous_flags
reconnect!
end
end
end

def supports_multi_statements?
(@config[:flags] & Mysql2::Client::MULTI_STATEMENTS) != 0
end

def supports_set_server_option?
@connection.respond_to?(:set_server_option)
end

def initialize_type_map(m = type_map)
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/fixtures_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ def test_bulk_insert_with_a_multi_statement_query_raises_an_exception_when_any_i
end
end
end

def test_bulk_insert_with_a_multi_statement_query_in_a_nested_transaction
fixtures = {
"traffic_lights" => [
{ "location" => "US", "state" => ["NY"], "long_state" => ["a"] },
]
}

ActiveRecord::Base.transaction do
con = ActiveRecord::Base.connection
assert_equal 1, con.open_transactions
con.insert_fixtures_set(fixtures)
assert_equal 1, con.open_transactions
end
end
end

if current_adapter?(:Mysql2Adapter)
Expand Down