diff --git a/lib/activerecord-import/adapters/mysql_adapter.rb b/lib/activerecord-import/adapters/mysql_adapter.rb index 81c7911..3310d88 100644 --- a/lib/activerecord-import/adapters/mysql_adapter.rb +++ b/lib/activerecord-import/adapters/mysql_adapter.rb @@ -6,7 +6,16 @@ module ActiveRecord::Import::MysqlAdapter include ActiveRecord::Import::OnDuplicateKeyUpdateSupport 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 # in +args+. def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc: diff --git a/test/support/mysql/import_examples.rb b/test/support/mysql/import_examples.rb index e8bb1ef..f455089 100644 --- a/test/support/mysql/import_examples.rb +++ b/test/support/mysql/import_examples.rb @@ -1,4 +1,32 @@ 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 extend ActiveSupport::TestCase::MySQLAssertions