activerecord-import 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/Gemfile +7 -1
 - data/gemfiles/4.1.gemfile +4 -0
 - data/gemfiles/4.2.gemfile +4 -0
 - data/lib/activerecord-import/import.rb +75 -9
 - data/lib/activerecord-import/synchronize.rb +8 -3
 - data/lib/activerecord-import/version.rb +1 -1
 - data/test/import_test.rb +47 -11
 - data/test/models/topic.rb +3 -1
 - data/test/support/active_support/test_case_extensions.rb +1 -1
 - data/test/support/mysql/assertions.rb +12 -12
 - data/test/support/mysql/import_examples.rb +1 -1
 - data/test/test_helper.rb +10 -3
 - data/test/travis/build.sh +26 -15
 - metadata +5 -4
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 7aec7bd524a95df0244ff6fd857ca4b534fffe7e
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 3680f99953ff777985422c2a349eacd88b7df0d5
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 302282ad1f0db1ce53f9961a46de68dea66b6f332e25742b35cf086ce16a458c40c502aa8c3b3a93a220b3fc4307c5ee33e731d786c2365f82a294dac214c17b
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: edbc30d0d1e4ecdeeabacfed9042b02ae89c79ea644d8683dd6a9048847ee1f40f6bea357fe702628bbe8f694a20e2d76db20d1ea7102b2dfe31e3068e7930bc
         
     | 
    
        data/Gemfile
    CHANGED
    
    | 
         @@ -20,7 +20,9 @@ end 
     | 
|
| 
       20 
20 
     | 
    
         | 
| 
       21 
21 
     | 
    
         
             
            # Support libs
         
     | 
| 
       22 
22 
     | 
    
         
             
            gem "factory_girl", "~> 4.2.0"
         
     | 
| 
       23 
     | 
    
         
            -
            gem " 
     | 
| 
      
 23 
     | 
    
         
            +
            gem "timecop"
         
     | 
| 
      
 24 
     | 
    
         
            +
            gem "chronic"
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
       24 
26 
     | 
    
         | 
| 
       25 
27 
     | 
    
         
             
            # Debugging
         
     | 
| 
       26 
28 
     | 
    
         
             
            platforms :jruby do
         
     | 
| 
         @@ -37,4 +39,8 @@ end 
     | 
|
| 
       37 
39 
     | 
    
         | 
| 
       38 
40 
     | 
    
         
             
            version = ENV['AR_VERSION'] || "3.2"
         
     | 
| 
       39 
41 
     | 
    
         | 
| 
      
 42 
     | 
    
         
            +
            if version > "4.0"
         
     | 
| 
      
 43 
     | 
    
         
            +
              gem "minitest"
         
     | 
| 
      
 44 
     | 
    
         
            +
            end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
       40 
46 
     | 
    
         
             
            eval_gemfile File.expand_path("../gemfiles/#{version}.gemfile", __FILE__)
         
     | 
| 
         @@ -25,6 +25,71 @@ module ActiveRecord::Import #:nodoc: 
     | 
|
| 
       25 
25 
     | 
    
         
             
              end
         
     | 
| 
       26 
26 
     | 
    
         
             
            end
         
     | 
| 
       27 
27 
     | 
    
         | 
| 
      
 28 
     | 
    
         
            +
            class ActiveRecord::Associations::CollectionProxy
         
     | 
| 
      
 29 
     | 
    
         
            +
              def import(*args, &block)
         
     | 
| 
      
 30 
     | 
    
         
            +
                @association.import(*args, &block)
         
     | 
| 
      
 31 
     | 
    
         
            +
              end
         
     | 
| 
      
 32 
     | 
    
         
            +
            end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
            class ActiveRecord::Associations::CollectionAssociation
         
     | 
| 
      
 35 
     | 
    
         
            +
              def import(*args, &block)
         
     | 
| 
      
 36 
     | 
    
         
            +
                unless owner.persisted?
         
     | 
| 
      
 37 
     | 
    
         
            +
                  raise ActiveRecord::RecordNotSaved, "You cannot call import unless the parent is saved"
         
     | 
| 
      
 38 
     | 
    
         
            +
                end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
                options = args.last.is_a?(Hash) ? args.pop : {}
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                model_klass = self.reflection.klass
         
     | 
| 
      
 43 
     | 
    
         
            +
                symbolized_foreign_key = self.reflection.foreign_key.to_sym
         
     | 
| 
      
 44 
     | 
    
         
            +
                symbolized_column_names = model_klass.column_names.map(&:to_sym)
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                owner_primary_key = self.owner.class.primary_key
         
     | 
| 
      
 47 
     | 
    
         
            +
                owner_primary_key_value = self.owner.send(owner_primary_key)
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                # assume array of model objects
         
     | 
| 
      
 50 
     | 
    
         
            +
                if args.last.is_a?( Array ) and args.last.first.is_a? ActiveRecord::Base
         
     | 
| 
      
 51 
     | 
    
         
            +
                  if args.length == 2
         
     | 
| 
      
 52 
     | 
    
         
            +
                    models = args.last
         
     | 
| 
      
 53 
     | 
    
         
            +
                    column_names = args.first
         
     | 
| 
      
 54 
     | 
    
         
            +
                  else
         
     | 
| 
      
 55 
     | 
    
         
            +
                    models = args.first
         
     | 
| 
      
 56 
     | 
    
         
            +
                    column_names = symbolized_column_names
         
     | 
| 
      
 57 
     | 
    
         
            +
                  end
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                  if !symbolized_column_names.include?(symbolized_foreign_key)
         
     | 
| 
      
 60 
     | 
    
         
            +
                    column_names << symbolized_foreign_key
         
     | 
| 
      
 61 
     | 
    
         
            +
                  end
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
                  models.each do |m|
         
     | 
| 
      
 64 
     | 
    
         
            +
                    m.send "#{symbolized_foreign_key}=", owner_primary_key_value
         
     | 
| 
      
 65 
     | 
    
         
            +
                  end
         
     | 
| 
      
 66 
     | 
    
         
            +
             
     | 
| 
      
 67 
     | 
    
         
            +
                  return model_klass.import column_names, models, options
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
                # supports empty array
         
     | 
| 
      
 70 
     | 
    
         
            +
                elsif args.last.is_a?( Array ) and args.last.empty?
         
     | 
| 
      
 71 
     | 
    
         
            +
                  return ActiveRecord::Import::Result.new([], 0) if args.last.empty?
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
                # supports 2-element array and array
         
     | 
| 
      
 74 
     | 
    
         
            +
                elsif args.size == 2 and args.first.is_a?( Array ) and args.last.is_a?( Array )
         
     | 
| 
      
 75 
     | 
    
         
            +
                  column_names, array_of_attributes = args
         
     | 
| 
      
 76 
     | 
    
         
            +
                  symbolized_column_names = column_names.map(&:to_s)
         
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
      
 78 
     | 
    
         
            +
                  if !symbolized_column_names.include?(symbolized_foreign_key)
         
     | 
| 
      
 79 
     | 
    
         
            +
                    column_names << symbolized_foreign_key
         
     | 
| 
      
 80 
     | 
    
         
            +
                    array_of_attributes.each { |attrs| attrs << owner_primary_key_value }
         
     | 
| 
      
 81 
     | 
    
         
            +
                  else
         
     | 
| 
      
 82 
     | 
    
         
            +
                    index = symbolized_column_names.index(symbolized_foreign_key)
         
     | 
| 
      
 83 
     | 
    
         
            +
                    array_of_attributes.each { |attrs| attrs[index] = owner_primary_key_value }
         
     | 
| 
      
 84 
     | 
    
         
            +
                  end
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
                  return model_klass.import column_names, array_of_attributes, options
         
     | 
| 
      
 87 
     | 
    
         
            +
                else
         
     | 
| 
      
 88 
     | 
    
         
            +
                  raise ArgumentError.new( "Invalid arguments!" )
         
     | 
| 
      
 89 
     | 
    
         
            +
                end
         
     | 
| 
      
 90 
     | 
    
         
            +
              end
         
     | 
| 
      
 91 
     | 
    
         
            +
            end
         
     | 
| 
      
 92 
     | 
    
         
            +
             
     | 
| 
       28 
93 
     | 
    
         
             
            class ActiveRecord::Base
         
     | 
| 
       29 
94 
     | 
    
         
             
              class << self
         
     | 
| 
       30 
95 
     | 
    
         | 
| 
         @@ -120,7 +185,7 @@ class ActiveRecord::Base 
     | 
|
| 
       120 
185 
     | 
    
         
             
                #  # Example using column_names and array_of_values
         
     | 
| 
       121 
186 
     | 
    
         
             
                #  columns = [ :author_name, :title ]
         
     | 
| 
       122 
187 
     | 
    
         
             
                #  values = [ [ 'zdennis', 'test post' ], [ 'jdoe', 'another test post' ] ]
         
     | 
| 
       123 
     | 
    
         
            -
                #  BlogPost.import columns, values 
     | 
| 
      
 188 
     | 
    
         
            +
                #  BlogPost.import columns, values
         
     | 
| 
       124 
189 
     | 
    
         
             
                #
         
     | 
| 
       125 
190 
     | 
    
         
             
                #  # Example using column_names, array_of_value and options
         
     | 
| 
       126 
191 
     | 
    
         
             
                #  columns = [ :author_name, :title ]
         
     | 
| 
         @@ -142,7 +207,7 @@ class ActiveRecord::Base 
     | 
|
| 
       142 
207 
     | 
    
         
             
                #
         
     | 
| 
       143 
208 
     | 
    
         
             
                # == On Duplicate Key Update (MySQL only)
         
     | 
| 
       144 
209 
     | 
    
         
             
                #
         
     | 
| 
       145 
     | 
    
         
            -
                # The :on_duplicate_key_update option can be either an Array or a Hash. 
     | 
| 
      
 210 
     | 
    
         
            +
                # The :on_duplicate_key_update option can be either an Array or a Hash.
         
     | 
| 
       146 
211 
     | 
    
         
             
                #
         
     | 
| 
       147 
212 
     | 
    
         
             
                # ==== Using an Array
         
     | 
| 
       148 
213 
     | 
    
         
             
                #
         
     | 
| 
         @@ -170,6 +235,7 @@ class ActiveRecord::Base 
     | 
|
| 
       170 
235 
     | 
    
         
             
                  options.merge!( args.pop ) if args.last.is_a? Hash
         
     | 
| 
       171 
236 
     | 
    
         | 
| 
       172 
237 
     | 
    
         
             
                  is_validating = options.delete( :validate )
         
     | 
| 
      
 238 
     | 
    
         
            +
                  is_validating = true unless options[:validate_with_context].nil?
         
     | 
| 
       173 
239 
     | 
    
         | 
| 
       174 
240 
     | 
    
         
             
                  # assume array of model objects
         
     | 
| 
       175 
241 
     | 
    
         
             
                  if args.last.is_a?( Array ) and args.last.first.is_a? ActiveRecord::Base
         
     | 
| 
         @@ -254,7 +320,7 @@ class ActiveRecord::Base 
     | 
|
| 
       254 
320 
     | 
    
         
             
                    instance = new do |model|
         
     | 
| 
       255 
321 
     | 
    
         
             
                      hsh.each_pair{ |k,v| model.send("#{k}=", v) }
         
     | 
| 
       256 
322 
     | 
    
         
             
                    end
         
     | 
| 
       257 
     | 
    
         
            -
                    if not instance.valid?
         
     | 
| 
      
 323 
     | 
    
         
            +
                    if not instance.valid?(options[:validate_with_context])
         
     | 
| 
       258 
324 
     | 
    
         
             
                      array_of_attributes[ i ] = nil
         
     | 
| 
       259 
325 
     | 
    
         
             
                      failed_instances << instance
         
     | 
| 
       260 
326 
     | 
    
         
             
                    end
         
     | 
| 
         @@ -331,11 +397,11 @@ class ActiveRecord::Base 
     | 
|
| 
       331 
397 
     | 
    
         
             
                      # be sure to query sequence_name *last*, only if cheaper tests fail, because it's costly
         
     | 
| 
       332 
398 
     | 
    
         
             
                      if val.nil? && column.name == primary_key && !sequence_name.blank?
         
     | 
| 
       333 
399 
     | 
    
         
             
                         connection_memo.next_value_for_sequence(sequence_name)
         
     | 
| 
       334 
     | 
    
         
            -
                       
     | 
| 
       335 
     | 
    
         
            -
                        if  
     | 
| 
       336 
     | 
    
         
            -
                          connection_memo.quote( 
     | 
| 
      
 400 
     | 
    
         
            +
                      elsif column
         
     | 
| 
      
 401 
     | 
    
         
            +
                        if column.respond_to?(:type_cast_from_user)                         # Rails 4.2 and higher
         
     | 
| 
      
 402 
     | 
    
         
            +
                          connection_memo.quote(column.type_cast_from_user(val), column)
         
     | 
| 
       337 
403 
     | 
    
         
             
                        else
         
     | 
| 
       338 
     | 
    
         
            -
                          connection_memo.quote(val, column)
         
     | 
| 
      
 404 
     | 
    
         
            +
                          connection_memo.quote(column.type_cast(val), column)              # Rails 3.1, 3.2, and 4.1
         
     | 
| 
       339 
405 
     | 
    
         
             
                        end
         
     | 
| 
       340 
406 
     | 
    
         
             
                      end
         
     | 
| 
       341 
407 
     | 
    
         
             
                    end
         
     | 
| 
         @@ -347,7 +413,7 @@ class ActiveRecord::Base 
     | 
|
| 
       347 
413 
     | 
    
         
             
                  AREXT_RAILS_COLUMNS[:create].each_pair do |key, blk|
         
     | 
| 
       348 
414 
     | 
    
         
             
                    if self.column_names.include?(key)
         
     | 
| 
       349 
415 
     | 
    
         
             
                      value = blk.call
         
     | 
| 
       350 
     | 
    
         
            -
                      if index=column_names.index(key)
         
     | 
| 
      
 416 
     | 
    
         
            +
                      if index=column_names.index(key) || index=column_names.index(key.to_sym)
         
     | 
| 
       351 
417 
     | 
    
         
             
                        # replace every instance of the array of attributes with our value
         
     | 
| 
       352 
418 
     | 
    
         
             
                        array_of_attributes.each{ |arr| arr[index] = value if arr[index].nil? }
         
     | 
| 
       353 
419 
     | 
    
         
             
                      else
         
     | 
| 
         @@ -360,7 +426,7 @@ class ActiveRecord::Base 
     | 
|
| 
       360 
426 
     | 
    
         
             
                  AREXT_RAILS_COLUMNS[:update].each_pair do |key, blk|
         
     | 
| 
       361 
427 
     | 
    
         
             
                    if self.column_names.include?(key)
         
     | 
| 
       362 
428 
     | 
    
         
             
                      value = blk.call
         
     | 
| 
       363 
     | 
    
         
            -
                      if index=column_names.index(key)
         
     | 
| 
      
 429 
     | 
    
         
            +
                      if index=column_names.index(key) || index=column_names.index(key.to_sym)
         
     | 
| 
       364 
430 
     | 
    
         
             
                         # replace every instance of the array of attributes with our value
         
     | 
| 
       365 
431 
     | 
    
         
             
                         array_of_attributes.each{ |arr| arr[index] = value }
         
     | 
| 
       366 
432 
     | 
    
         
             
                      else
         
     | 
| 
         @@ -42,9 +42,14 @@ module ActiveRecord # :nodoc: 
     | 
|
| 
       42 
42 
     | 
    
         
             
                    if matched_instance
         
     | 
| 
       43 
43 
     | 
    
         
             
                      instance.clear_aggregation_cache
         
     | 
| 
       44 
44 
     | 
    
         
             
                      instance.clear_association_cache
         
     | 
| 
       45 
     | 
    
         
            -
                      instance.instance_variable_set  
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
                      instance. 
     | 
| 
      
 45 
     | 
    
         
            +
                      instance.instance_variable_set :@attributes, matched_instance.instance_variable_get(:@attributes)
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                      if instance.respond_to?(:clear_changes_information)
         
     | 
| 
      
 48 
     | 
    
         
            +
                        instance.clear_changes_information                  # Rails 4.1 and higher
         
     | 
| 
      
 49 
     | 
    
         
            +
                      else
         
     | 
| 
      
 50 
     | 
    
         
            +
                        instance.changed_attributes.clear                   # Rails 3.1, 3.2
         
     | 
| 
      
 51 
     | 
    
         
            +
                      end
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
       48 
53 
     | 
    
         
             
                      # Since the instance now accurately reflects the record in
         
     | 
| 
       49 
54 
     | 
    
         
             
                      # the database, ensure that instance.persisted? is true.
         
     | 
| 
       50 
55 
     | 
    
         
             
                      instance.instance_variable_set '@new_record', false
         
     | 
    
        data/test/import_test.rb
    CHANGED
    
    | 
         @@ -19,7 +19,7 @@ describe "#import" do 
     | 
|
| 
       19 
19 
     | 
    
         
             
                end
         
     | 
| 
       20 
20 
     | 
    
         
             
              end
         
     | 
| 
       21 
21 
     | 
    
         | 
| 
       22 
     | 
    
         
            -
              describe "with non-default ActiveRecord models" do 
     | 
| 
      
 22 
     | 
    
         
            +
              describe "with non-default ActiveRecord models" do
         
     | 
| 
       23 
23 
     | 
    
         
             
                context "that have a non-standard primary key (that is no sequence)" do
         
     | 
| 
       24 
24 
     | 
    
         
             
                  it "should import models successfully" do
         
     | 
| 
       25 
25 
     | 
    
         
             
                    assert_difference "Widget.count", +3 do
         
     | 
| 
         @@ -32,6 +32,7 @@ describe "#import" do 
     | 
|
| 
       32 
32 
     | 
    
         
             
              context "with :validation option" do
         
     | 
| 
       33 
33 
     | 
    
         
             
                let(:columns) { %w(title author_name) }
         
     | 
| 
       34 
34 
     | 
    
         
             
                let(:valid_values) { [[ "LDAP", "Jerry Carter"], ["Rails Recipes", "Chad Fowler"]] }
         
     | 
| 
      
 35 
     | 
    
         
            +
                let(:valid_values_with_context) { [[ 1111, "Jerry Carter"], [2222, "Chad Fowler"]] }
         
     | 
| 
       35 
36 
     | 
    
         
             
                let(:invalid_values) { [[ "The RSpec Book", ""], ["Agile+UX", ""]] }
         
     | 
| 
       36 
37 
     | 
    
         | 
| 
       37 
38 
     | 
    
         
             
                context "with validation checks turned off" do
         
     | 
| 
         @@ -61,12 +62,24 @@ describe "#import" do 
     | 
|
| 
       61 
62 
     | 
    
         
             
                    end
         
     | 
| 
       62 
63 
     | 
    
         
             
                  end
         
     | 
| 
       63 
64 
     | 
    
         | 
| 
      
 65 
     | 
    
         
            +
                  it "should import valid data with on option" do
         
     | 
| 
      
 66 
     | 
    
         
            +
                    assert_difference "Topic.count", +2 do
         
     | 
| 
      
 67 
     | 
    
         
            +
                      result = Topic.import columns, valid_values_with_context, :validate_with_context => :context_test
         
     | 
| 
      
 68 
     | 
    
         
            +
                    end
         
     | 
| 
      
 69 
     | 
    
         
            +
                  end
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
       64 
71 
     | 
    
         
             
                  it "should not import invalid data" do
         
     | 
| 
       65 
72 
     | 
    
         
             
                    assert_no_difference "Topic.count" do
         
     | 
| 
       66 
73 
     | 
    
         
             
                      result = Topic.import columns, invalid_values, :validate => true
         
     | 
| 
       67 
74 
     | 
    
         
             
                    end
         
     | 
| 
       68 
75 
     | 
    
         
             
                  end
         
     | 
| 
       69 
76 
     | 
    
         | 
| 
      
 77 
     | 
    
         
            +
                  it "should import invalid data with on option" do
         
     | 
| 
      
 78 
     | 
    
         
            +
                    assert_no_difference "Topic.count" do
         
     | 
| 
      
 79 
     | 
    
         
            +
                      result = Topic.import columns, valid_values, :validate_with_context => :context_test
         
     | 
| 
      
 80 
     | 
    
         
            +
                    end
         
     | 
| 
      
 81 
     | 
    
         
            +
                  end
         
     | 
| 
      
 82 
     | 
    
         
            +
             
     | 
| 
       70 
83 
     | 
    
         
             
                  it "should report the failed instances" do
         
     | 
| 
       71 
84 
     | 
    
         
             
                    results = Topic.import columns, invalid_values, :validate => true
         
     | 
| 
       72 
85 
     | 
    
         
             
                    assert_equal invalid_values.size, results.failed_instances.size
         
     | 
| 
         @@ -238,7 +251,7 @@ describe "#import" do 
     | 
|
| 
       238 
251 
     | 
    
         
             
                  setup do
         
     | 
| 
       239 
252 
     | 
    
         
             
                    @existing_book = Book.create(title: "Fell", author_name: "Curry", publisher: "Bayer", created_at: 2.years.ago.utc, created_on: 2.years.ago.utc)
         
     | 
| 
       240 
253 
     | 
    
         
             
                    ActiveRecord::Base.default_timezone = :utc
         
     | 
| 
       241 
     | 
    
         
            -
                     
     | 
| 
      
 254 
     | 
    
         
            +
                    Timecop.freeze Chronic.parse("5 minutes ago") do
         
     | 
| 
       242 
255 
     | 
    
         
             
                      assert_difference "Book.count", +2 do
         
     | 
| 
       243 
256 
     | 
    
         
             
                        result = Book.import ["title", "author_name", "publisher", "created_at", "created_on"], [["LDAP", "Big Bird", "Del Rey", nil, nil], [@existing_book.title, @existing_book.author_name, @existing_book.publisher, @existing_book.created_at, @existing_book.created_on]]
         
     | 
| 
       244 
257 
     | 
    
         
             
                      end
         
     | 
| 
         @@ -272,26 +285,25 @@ describe "#import" do 
     | 
|
| 
       272 
285 
     | 
    
         
             
                end
         
     | 
| 
       273 
286 
     | 
    
         | 
| 
       274 
287 
     | 
    
         
             
                context "when a custom time zone is set" do
         
     | 
| 
      
 288 
     | 
    
         
            +
                  let(:time){ Chronic.parse("5 minutes ago")  }
         
     | 
| 
      
 289 
     | 
    
         
            +
             
     | 
| 
       275 
290 
     | 
    
         
             
                  setup do
         
     | 
| 
       276 
     | 
    
         
            -
                     
     | 
| 
       277 
     | 
    
         
            -
                    ActiveRecord::Base.default_timezone = :utc
         
     | 
| 
       278 
     | 
    
         
            -
                    Delorean.time_travel_to("5 minutes ago") do
         
     | 
| 
      
 291 
     | 
    
         
            +
                    Timecop.freeze(time) do
         
     | 
| 
       279 
292 
     | 
    
         
             
                      assert_difference "Book.count", +1 do
         
     | 
| 
       280 
293 
     | 
    
         
             
                        result = Book.import [:title, :author_name, :publisher], [["LDAP", "Big Bird", "Del Rey"]]
         
     | 
| 
       281 
294 
     | 
    
         
             
                      end
         
     | 
| 
       282 
295 
     | 
    
         
             
                    end
         
     | 
| 
       283 
     | 
    
         
            -
                    ActiveRecord::Base.default_timezone = original_timezone
         
     | 
| 
       284 
296 
     | 
    
         
             
                    @book = Book.last
         
     | 
| 
       285 
297 
     | 
    
         
             
                  end
         
     | 
| 
       286 
298 
     | 
    
         | 
| 
       287 
299 
     | 
    
         
             
                  it "should set the created_at and created_on timestamps for new records"  do
         
     | 
| 
       288 
     | 
    
         
            -
                     
     | 
| 
       289 
     | 
    
         
            -
                     
     | 
| 
      
 300 
     | 
    
         
            +
                    assert_in_delta time.to_i, @book.created_at.to_i, 1.second
         
     | 
| 
      
 301 
     | 
    
         
            +
                    assert_in_delta time.to_i, @book.created_on.to_i, 1.second
         
     | 
| 
       290 
302 
     | 
    
         
             
                  end
         
     | 
| 
       291 
303 
     | 
    
         | 
| 
       292 
304 
     | 
    
         
             
                  it "should set the updated_at and updated_on timestamps for new records" do
         
     | 
| 
       293 
     | 
    
         
            -
                     
     | 
| 
       294 
     | 
    
         
            -
                     
     | 
| 
      
 305 
     | 
    
         
            +
                    assert_in_delta time.to_i, @book.updated_at.to_i, 1.second
         
     | 
| 
      
 306 
     | 
    
         
            +
                    assert_in_delta time.to_i, @book.updated_on.to_i, 1.second
         
     | 
| 
       295 
307 
     | 
    
         
             
                  end
         
     | 
| 
       296 
308 
     | 
    
         
             
                end
         
     | 
| 
       297 
309 
     | 
    
         
             
              end
         
     | 
| 
         @@ -326,6 +338,31 @@ describe "#import" do 
     | 
|
| 
       326 
338 
     | 
    
         
             
                    end
         
     | 
| 
       327 
339 
     | 
    
         
             
                  end
         
     | 
| 
       328 
340 
     | 
    
         
             
                end
         
     | 
| 
      
 341 
     | 
    
         
            +
             
     | 
| 
      
 342 
     | 
    
         
            +
                it "works importing models" do
         
     | 
| 
      
 343 
     | 
    
         
            +
                  topic = FactoryGirl.create :topic
         
     | 
| 
      
 344 
     | 
    
         
            +
                  books = [
         
     | 
| 
      
 345 
     | 
    
         
            +
                    Book.new(:author_name => "Author #1", :title => "Book #1"),
         
     | 
| 
      
 346 
     | 
    
         
            +
                    Book.new(:author_name => "Author #2", :title => "Book #2"),
         
     | 
| 
      
 347 
     | 
    
         
            +
                  ]
         
     | 
| 
      
 348 
     | 
    
         
            +
                  topic.books.import books
         
     | 
| 
      
 349 
     | 
    
         
            +
                  assert_equal 2, topic.books.count
         
     | 
| 
      
 350 
     | 
    
         
            +
                  assert topic.books.detect { |b| b.title == "Book #1" && b.author_name == "Author #1" }
         
     | 
| 
      
 351 
     | 
    
         
            +
                  assert topic.books.detect { |b| b.title == "Book #2" && b.author_name == "Author #2" }
         
     | 
| 
      
 352 
     | 
    
         
            +
                end
         
     | 
| 
      
 353 
     | 
    
         
            +
             
     | 
| 
      
 354 
     | 
    
         
            +
                it "works importing array of columns and values" do
         
     | 
| 
      
 355 
     | 
    
         
            +
                  topic = FactoryGirl.create :topic
         
     | 
| 
      
 356 
     | 
    
         
            +
                  books = [
         
     | 
| 
      
 357 
     | 
    
         
            +
                    Book.new(:author_name => "Foo", :title => "Baz"),
         
     | 
| 
      
 358 
     | 
    
         
            +
                    Book.new(:author_name => "Foo2", :title => "Baz2"),
         
     | 
| 
      
 359 
     | 
    
         
            +
                  ]
         
     | 
| 
      
 360 
     | 
    
         
            +
                  topic.books.import [:author_name, :title], [["Author #1", "Book #1"], ["Author #2", "Book #2"]]
         
     | 
| 
      
 361 
     | 
    
         
            +
                  assert_equal 2, topic.books.count
         
     | 
| 
      
 362 
     | 
    
         
            +
                  assert topic.books.detect { |b| b.title == "Book #1" && b.author_name == "Author #1" }
         
     | 
| 
      
 363 
     | 
    
         
            +
                  assert topic.books.detect { |b| b.title == "Book #2" && b.author_name == "Author #2" }
         
     | 
| 
      
 364 
     | 
    
         
            +
                end
         
     | 
| 
      
 365 
     | 
    
         
            +
             
     | 
| 
       329 
366 
     | 
    
         
             
              end
         
     | 
| 
       330 
367 
     | 
    
         | 
| 
       331 
368 
     | 
    
         
             
              describe "importing when model has default_scope" do
         
     | 
| 
         @@ -355,5 +392,4 @@ describe "#import" do 
     | 
|
| 
       355 
392 
     | 
    
         
             
                  assert_equal({:a => :b}, Widget.find_by_w_id(1).data)
         
     | 
| 
       356 
393 
     | 
    
         
             
                end
         
     | 
| 
       357 
394 
     | 
    
         
             
              end
         
     | 
| 
       358 
     | 
    
         
            -
             
     | 
| 
       359 
395 
     | 
    
         
             
            end
         
     | 
    
        data/test/models/topic.rb
    CHANGED
    
    | 
         @@ -1,7 +1,9 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            class Topic < ActiveRecord::Base
         
     | 
| 
       2 
2 
     | 
    
         
             
              validates_presence_of :author_name
         
     | 
| 
      
 3 
     | 
    
         
            +
              validates :title, numericality: { only_integer: true }, on: :context_test
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
       3 
5 
     | 
    
         
             
              has_many :books
         
     | 
| 
       4 
6 
     | 
    
         
             
              belongs_to :parent, :class_name => "Topic"
         
     | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
       6 
8 
     | 
    
         
             
              composed_of :description, :mapping => [ %w(title title), %w(author_name author_name)], :allow_nil => true, :class_name => "TopicDescription"
         
     | 
| 
       7 
9 
     | 
    
         
             
            end
         
     | 
| 
         @@ -3,29 +3,29 @@ class ActiveSupport::TestCase 
     | 
|
| 
       3 
3 
     | 
    
         
             
                def self.extended(klass)
         
     | 
| 
       4 
4 
     | 
    
         
             
                  klass.instance_eval do
         
     | 
| 
       5 
5 
     | 
    
         
             
                    assertion(:should_not_update_created_at_on_timestamp_columns) do
         
     | 
| 
       6 
     | 
    
         
            -
                       
     | 
| 
      
 6 
     | 
    
         
            +
                      Timecop.freeze Chronic.parse("5 minutes from now") do
         
     | 
| 
       7 
7 
     | 
    
         
             
                        perform_import
         
     | 
| 
       8 
     | 
    
         
            -
                         
     | 
| 
       9 
     | 
    
         
            -
                         
     | 
| 
      
 8 
     | 
    
         
            +
                        assert_in_delta @topic.created_at.to_i, updated_topic.created_at.to_i, 1
         
     | 
| 
      
 9 
     | 
    
         
            +
                        assert_in_delta @topic.created_on.to_i, updated_topic.created_on.to_i, 1
         
     | 
| 
       10 
10 
     | 
    
         
             
                      end
         
     | 
| 
       11 
11 
     | 
    
         
             
                    end
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
       13 
13 
     | 
    
         
             
                    assertion(:should_update_updated_at_on_timestamp_columns) do
         
     | 
| 
       14 
14 
     | 
    
         
             
                      time = Chronic.parse("5 minutes from now")
         
     | 
| 
       15 
     | 
    
         
            -
                       
     | 
| 
      
 15 
     | 
    
         
            +
                      Timecop.freeze time do
         
     | 
| 
       16 
16 
     | 
    
         
             
                        perform_import
         
     | 
| 
       17 
     | 
    
         
            -
                         
     | 
| 
       18 
     | 
    
         
            -
                         
     | 
| 
      
 17 
     | 
    
         
            +
                        assert_in_delta time.to_i, updated_topic.updated_at.to_i, 1
         
     | 
| 
      
 18 
     | 
    
         
            +
                        assert_in_delta time.to_i, updated_topic.updated_on.to_i, 1
         
     | 
| 
       19 
19 
     | 
    
         
             
                      end
         
     | 
| 
       20 
20 
     | 
    
         
             
                    end
         
     | 
| 
       21 
21 
     | 
    
         | 
| 
       22 
22 
     | 
    
         
             
                    assertion(:should_not_update_timestamps) do
         
     | 
| 
       23 
     | 
    
         
            -
                       
     | 
| 
      
 23 
     | 
    
         
            +
                      Timecop.freeze Chronic.parse("5 minutes from now") do
         
     | 
| 
       24 
24 
     | 
    
         
             
                        perform_import :timestamps => false
         
     | 
| 
       25 
     | 
    
         
            -
                         
     | 
| 
       26 
     | 
    
         
            -
                         
     | 
| 
       27 
     | 
    
         
            -
                         
     | 
| 
       28 
     | 
    
         
            -
                         
     | 
| 
      
 25 
     | 
    
         
            +
                        assert_in_delta @topic.created_at.to_i, updated_topic.created_at.to_i, 1
         
     | 
| 
      
 26 
     | 
    
         
            +
                        assert_in_delta @topic.created_on.to_i, updated_topic.created_on.to_i, 1
         
     | 
| 
      
 27 
     | 
    
         
            +
                        assert_in_delta @topic.updated_at.to_i, updated_topic.updated_at.to_i, 1
         
     | 
| 
      
 28 
     | 
    
         
            +
                        assert_in_delta @topic.updated_on.to_i, updated_topic.updated_on.to_i, 1
         
     | 
| 
       29 
29 
     | 
    
         
             
                      end
         
     | 
| 
       30 
30 
     | 
    
         
             
                    end
         
     | 
| 
       31 
31 
     | 
    
         | 
| 
         @@ -52,4 +52,4 @@ class ActiveSupport::TestCase 
     | 
|
| 
       52 
52 
     | 
    
         
             
                  end
         
     | 
| 
       53 
53 
     | 
    
         
             
                end
         
     | 
| 
       54 
54 
     | 
    
         
             
              end
         
     | 
| 
       55 
     | 
    
         
            -
            end
         
     | 
| 
      
 55 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -14,7 +14,7 @@ def should_support_mysql_import_functionality 
     | 
|
| 
       14 
14 
     | 
    
         
             
                end
         
     | 
| 
       15 
15 
     | 
    
         | 
| 
       16 
16 
     | 
    
         
             
                macro(:perform_import){ raise "supply your own #perform_import in a context below" }
         
     | 
| 
       17 
     | 
    
         
            -
                macro(:updated_topic){ Topic.find(@topic) }
         
     | 
| 
      
 17 
     | 
    
         
            +
                macro(:updated_topic){ Topic.find(@topic.id) }
         
     | 
| 
       18 
18 
     | 
    
         | 
| 
       19 
19 
     | 
    
         
             
                context "given columns and values with :validation checks turned off" do
         
     | 
| 
       20 
20 
     | 
    
         
             
                  let(:columns){  %w( id title author_name author_email_address parent_id ) }
         
     | 
    
        data/test/test_helper.rb
    CHANGED
    
    | 
         @@ -10,13 +10,19 @@ ENV["RAILS_ENV"] = "test" 
     | 
|
| 
       10 
10 
     | 
    
         
             
            require "bundler"
         
     | 
| 
       11 
11 
     | 
    
         
             
            Bundler.setup
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
       13 
     | 
    
         
            -
            require "logger"
         
     | 
| 
       14 
     | 
    
         
            -
            require 'test/unit'
         
     | 
| 
       15 
13 
     | 
    
         
             
            require "active_record"
         
     | 
| 
       16 
14 
     | 
    
         
             
            require "active_record/fixtures"
         
     | 
| 
       17 
15 
     | 
    
         
             
            require "active_support/test_case"
         
     | 
| 
       18 
16 
     | 
    
         | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
      
 17 
     | 
    
         
            +
            if ActiveSupport::VERSION::STRING < "4.1"
         
     | 
| 
      
 18 
     | 
    
         
            +
              require 'test/unit'
         
     | 
| 
      
 19 
     | 
    
         
            +
            else
         
     | 
| 
      
 20 
     | 
    
         
            +
              require 'active_support/testing/autorun'
         
     | 
| 
      
 21 
     | 
    
         
            +
            end
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            require 'timecop'
         
     | 
| 
      
 24 
     | 
    
         
            +
            require 'chronic'
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
       20 
26 
     | 
    
         
             
            require "ruby-debug" if RUBY_VERSION.to_f < 1.9
         
     | 
| 
       21 
27 
     | 
    
         | 
| 
       22 
28 
     | 
    
         
             
            adapter = ENV["ARE_DB"] || "sqlite3"
         
     | 
| 
         @@ -25,6 +31,7 @@ FileUtils.mkdir_p 'log' 
     | 
|
| 
       25 
31 
     | 
    
         
             
            ActiveRecord::Base.logger = Logger.new("log/test.log")
         
     | 
| 
       26 
32 
     | 
    
         
             
            ActiveRecord::Base.logger.level = Logger::DEBUG
         
     | 
| 
       27 
33 
     | 
    
         
             
            ActiveRecord::Base.configurations["test"] = YAML.load_file(test_dir.join("database.yml"))[adapter]
         
     | 
| 
      
 34 
     | 
    
         
            +
            ActiveRecord::Base.default_timezone = :utc
         
     | 
| 
       28 
35 
     | 
    
         | 
| 
       29 
36 
     | 
    
         
             
            require "activerecord-import"
         
     | 
| 
       30 
37 
     | 
    
         
             
            ActiveRecord::Base.establish_connection "test"
         
     | 
    
        data/test/travis/build.sh
    CHANGED
    
    | 
         @@ -1,19 +1,30 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            #!/bin/ 
     | 
| 
      
 1 
     | 
    
         
            +
            #!/bin/bash
         
     | 
| 
       2 
2 
     | 
    
         
             
            set -e
         
     | 
| 
       3 
3 
     | 
    
         
             
            set +x
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
       6 
     | 
    
         
            -
             
     | 
| 
       7 
     | 
    
         
            -
             
     | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
       9 
     | 
    
         
            -
            bundle exec rake test:mysqlspatial            # Run tests for mysqlspatial
         
     | 
| 
       10 
     | 
    
         
            -
            bundle exec rake test:postgis                 # Run tests for postgis
         
     | 
| 
       11 
     | 
    
         
            -
            bundle exec rake test:postgresql              # Run tests for postgresql
         
     | 
| 
       12 
     | 
    
         
            -
            bundle exec rake test:seamless_database_pool  # Run tests for seamless_database_pool
         
     | 
| 
       13 
     | 
    
         
            -
            bundle exec rake test:spatialite              # Run tests for spatialite
         
     | 
| 
       14 
     | 
    
         
            -
            # so far the version installed in travis seems < 3.7.11 so we cannot test sqlite3 on it
         
     | 
| 
       15 
     | 
    
         
            -
            # bundle exec rake test:sqlite3
         
     | 
| 
      
 5 
     | 
    
         
            +
            function run {
         
     | 
| 
      
 6 
     | 
    
         
            +
              echo "Running: AR_VERSION=$AR_VERSION $@"
         
     | 
| 
      
 7 
     | 
    
         
            +
              $@
         
     | 
| 
      
 8 
     | 
    
         
            +
            }
         
     | 
| 
       16 
9 
     | 
    
         | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
       18 
     | 
    
         
            -
             
     | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
      
 10 
     | 
    
         
            +
            for activerecord_version in "3.1" "3.2" "4.1" "4.2" ; do
         
     | 
| 
      
 11 
     | 
    
         
            +
              export AR_VERSION=$activerecord_version
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
              bundle update activerecord
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
              run run bundle exec rake test:em_mysql2               # Run tests for em_mysql2
         
     | 
| 
      
 16 
     | 
    
         
            +
              run bundle exec rake test:mysql                   # Run tests for mysql
         
     | 
| 
      
 17 
     | 
    
         
            +
              run bundle exec rake test:mysql2                  # Run tests for mysql2
         
     | 
| 
      
 18 
     | 
    
         
            +
              run bundle exec rake test:mysql2spatial           # Run tests for mysql2spatial
         
     | 
| 
      
 19 
     | 
    
         
            +
              run bundle exec rake test:mysqlspatial            # Run tests for mysqlspatial
         
     | 
| 
      
 20 
     | 
    
         
            +
              run bundle exec rake test:postgis                 # Run tests for postgis
         
     | 
| 
      
 21 
     | 
    
         
            +
              run bundle exec rake test:postgresql              # Run tests for postgresql
         
     | 
| 
      
 22 
     | 
    
         
            +
              run bundle exec rake test:seamless_database_pool  # Run tests for seamless_database_pool
         
     | 
| 
      
 23 
     | 
    
         
            +
              run bundle exec rake test:spatialite              # Run tests for spatialite
         
     | 
| 
      
 24 
     | 
    
         
            +
              # so far the version installed in travis seems < 3.7.11 so we cannot test sqlite3 on it
         
     | 
| 
      
 25 
     | 
    
         
            +
              # run bundle exec rake test:sqlite3
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
              #jruby
         
     | 
| 
      
 28 
     | 
    
         
            +
              #bundle exec rake test:jdbcmysql               # Run tests for jdbcmysql
         
     | 
| 
      
 29 
     | 
    
         
            +
              #bundle exec rake test:jdbcpostgresql          # Run tests for jdbcpostgresql
         
     | 
| 
      
 30 
     | 
    
         
            +
            done
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: activerecord-import
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.7.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Zach Dennis
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2014- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2014-12-22 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: activerecord
         
     | 
| 
         @@ -70,6 +70,8 @@ files: 
     | 
|
| 
       70 
70 
     | 
    
         
             
            - gemfiles/3.1.gemfile
         
     | 
| 
       71 
71 
     | 
    
         
             
            - gemfiles/3.2.gemfile
         
     | 
| 
       72 
72 
     | 
    
         
             
            - gemfiles/4.0.gemfile
         
     | 
| 
      
 73 
     | 
    
         
            +
            - gemfiles/4.1.gemfile
         
     | 
| 
      
 74 
     | 
    
         
            +
            - gemfiles/4.2.gemfile
         
     | 
| 
       73 
75 
     | 
    
         
             
            - lib/activerecord-import.rb
         
     | 
| 
       74 
76 
     | 
    
         
             
            - lib/activerecord-import/active_record/adapters/abstract_adapter.rb
         
     | 
| 
       75 
77 
     | 
    
         
             
            - lib/activerecord-import/active_record/adapters/em_mysql2_adapter.rb
         
     | 
| 
         @@ -159,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       159 
161 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       160 
162 
     | 
    
         
             
            requirements: []
         
     | 
| 
       161 
163 
     | 
    
         
             
            rubyforge_project: 
         
     | 
| 
       162 
     | 
    
         
            -
            rubygems_version: 2. 
     | 
| 
      
 164 
     | 
    
         
            +
            rubygems_version: 2.4.3
         
     | 
| 
       163 
165 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       164 
166 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       165 
167 
     | 
    
         
             
            summary: Bulk-loading extension for ActiveRecord
         
     | 
| 
         @@ -207,4 +209,3 @@ test_files: 
     | 
|
| 
       207 
209 
     | 
    
         
             
            - test/travis/database.yml
         
     | 
| 
       208 
210 
     | 
    
         
             
            - test/value_sets_bytes_parser_test.rb
         
     | 
| 
       209 
211 
     | 
    
         
             
            - test/value_sets_records_parser_test.rb
         
     | 
| 
       210 
     | 
    
         
            -
            has_rdoc: 
         
     |