Skip to content
Browse files

Ensure the #max_allowed_packet method exists for the MysqlAdapters.

  • Loading branch information...
1 parent 1175baf commit cb6b726eaca8cfeb1457fa0e9e770920e3150040 @zdennis zdennis committed Jan 11, 2011
Showing with 38 additions and 1 deletion.
  1. +10 −1 lib/activerecord-import/adapters/mysql_adapter.rb
  2. +28 −0 test/support/mysql/import_examples.rb
View
11 lib/activerecord-import/adapters/mysql_adapter.rb
@@ -6,7 +6,16 @@ def self.included(klass)
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:
View
28 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

0 comments on commit cb6b726

Please sign in to comment.
Something went wrong with that request. Please try again.