mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
use set_server_option if possible
This commit is contained in:
parent
05bef14051
commit
425449e98d
2 changed files with 38 additions and 5 deletions
|
@ -559,14 +559,32 @@ module ActiveRecord
|
|||
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)
|
||||
|
|
|
@ -114,6 +114,21 @@ class FixturesTest < ActiveRecord::TestCase
|
|||
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)
|
||||
|
|
Loading…
Reference in a new issue