Ensure the #max_allowed_packet method exists for the MysqlAdapters.

https://github.com/zdennis/activerecord-import/issues#issue/12
This commit is contained in:
Zach Dennis 2011-01-11 21:01:34 -05:00
parent 1175bafe0e
commit cb6b726eac
2 changed files with 38 additions and 1 deletions

View file

@ -7,6 +7,15 @@ module ActiveRecord::Import::MysqlAdapter
end end
end end
# Returns the maximum number of bytes that the server will allow
# in a single packet
def max_allowed_packet # :nodoc:
result = execute( "SHOW VARIABLES like 'max_allowed_packet';" )
# original Mysql gem responds to #fetch_row while Mysql2 responds to #first
val = result.respond_to?(:fetch_row) ? result.fetch_row[1] : result.first[1]
val.to_i
end
# Returns a generated ON DUPLICATE KEY UPDATE statement given the passed # Returns a generated ON DUPLICATE KEY UPDATE statement given the passed
# in +args+. # in +args+.
def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc: def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc:

View file

@ -1,4 +1,32 @@
def should_support_mysql_import_functionality def should_support_mysql_import_functionality
describe "building insert value sets" do
it "should properly build insert value set based on max packet allowed" do
values = [
"('1','2','3')",
"('4','5','6')",
"('7','8','9')" ]
adapter = ActiveRecord::Base.connection.class
values_size_in_bytes = adapter.sum_sizes( *values )
base_sql_size_in_bytes = 15
max_bytes = 30
value_sets = adapter.get_insert_value_sets( values, base_sql_size_in_bytes, max_bytes )
assert_equal 3, value_sets.size, 'Three value sets were expected!'
# Each element in the value_sets array must be an array
value_sets.each_with_index { |e,i|
assert_kind_of Array, e, "Element #{i} was expected to be an Array!" }
# Each element in the values array should have a 1:1 correlation to the elements
# in the returned value_sets arrays
assert_equal values[0], value_sets[0].first
assert_equal values[1], value_sets[1].first
assert_equal values[2], value_sets[2].first
end
end
describe "#import with :on_duplicate_key_update option (mysql specific functionality)" do describe "#import with :on_duplicate_key_update option (mysql specific functionality)" do
extend ActiveSupport::TestCase::MySQLAssertions extend ActiveSupport::TestCase::MySQLAssertions