activerecord-import 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c61743fafaad0de04ccf2c6bff4439fe66aed03e951e5c42e4f7926cf8dac39
4
- data.tar.gz: c58d8992957e546b73bb7f7b90eaf2a741f007d2109a16db0e3b3d71b376e1da
3
+ metadata.gz: b3360334cbd71089351c8211214bb4aedf0bd3e65305513b17c9fb09e3f1cf19
4
+ data.tar.gz: 2b97e3a1c6c2b39970dd3bb2a00a608d6e105cddac19d3f9ebaf28f8f30ece69
5
5
  SHA512:
6
- metadata.gz: 98638b63235eae1c16f27d3be932b8ce4e8f236b24350766792a4fc563d199cb1d048d25efa5d22abf3a21017fa07b9474c3168a4be6c1ba5aaa6e0843d584a3
7
- data.tar.gz: 5c62bed2684f1b6d60d7e26b8ad13c6b7173a64835eeca9474082122425ab1f340f9063868224eda2539878210887701022e1ff6e6a37593872682bf90768d3a
6
+ metadata.gz: 4585ba6ff2300d94fbd646d5f5a07cc254311cb9a47eacc7b1266eab8df5d9a53ca3018232e6c2517f05e1aef0dafaff20942bd1150ef518d688940f795bf2a9
7
+ data.tar.gz: 532506d134f067323d0490a913739089e55bf17893f602c752e5e963af2d7d799f5200a72cc48e7c780992e9f82c68273dc7af308034664ba898ed5292e199c4
@@ -0,0 +1,73 @@
1
+ name: Test
2
+ on: [push, pull_request]
3
+ jobs:
4
+ test:
5
+ services:
6
+ postgres:
7
+ image: postgis/postgis:10-2.5
8
+ env:
9
+ POSTGRES_USER: postgres
10
+ POSTGRES_PASSWORD: postgres
11
+ ports:
12
+ - 5432:5432
13
+ # Set health checks to wait until postgres has started
14
+ options: >-
15
+ --health-cmd pg_isready
16
+ --health-interval 10s
17
+ --health-timeout 5s
18
+ --health-retries 5
19
+ strategy:
20
+ fail-fast: false
21
+ matrix:
22
+ ruby:
23
+ - 2.6
24
+ env:
25
+ - AR_VERSION: 6.1
26
+ - AR_VERSION: 6.0
27
+ - AR_VERSION: 5.2
28
+ - AR_VERSION: 5.1
29
+ include:
30
+ - ruby: 2.4
31
+ env:
32
+ AR_VERSION: 5.0
33
+ - ruby: 2.4
34
+ env:
35
+ AR_VERSION: 4.2
36
+ runs-on: ubuntu-latest
37
+ env:
38
+ AR_VERSION: ${{ matrix.env.AR_VERSION }}
39
+ DB_DATABASE: activerecord_import_test
40
+ steps:
41
+ - uses: actions/checkout@v2
42
+ - uses: ruby/setup-ruby@v1
43
+ with:
44
+ ruby-version: ${{ matrix.ruby }}
45
+ - name: Setup Bundler 1.x for Ruby 2.3
46
+ if: ${{ matrix.ruby == '2.3' }}
47
+ run: echo "BUNDLER_VERSION=1.17.3" >> $GITHUB_ENV
48
+ - name: Set up databases
49
+ run: |
50
+ sudo /etc/init.d/mysql start
51
+ mysql -e 'CREATE DATABASE ${{ env.DB_DATABASE }} CHARACTER SET utf8 COLLATE utf8_general_ci;' -u root -proot
52
+ psql -h localhost -U postgres -c 'create database ${{ env.DB_DATABASE }};'
53
+ psql -h localhost -U postgres -d ${{ env.DB_DATABASE }} -c 'create extension if not exists hstore;'
54
+ psql -h localhost -U postgres -c 'create extension if not exists postgis;'
55
+ psql -h localhost -U postgres -c 'create extension if not exists "uuid-ossp";'
56
+ cp test/github/database.yml test/database.yml
57
+ env:
58
+ PGPASSWORD: postgres
59
+ - name: Install dependencies
60
+ run : AR_VERSION=${{ env.AR_VERSION }} bundle install
61
+ - name: Run tests
62
+ run: |
63
+ bundle exec rake test:mysql2
64
+ bundle exec rake test:mysql2_makara
65
+ bundle exec rake test:mysql2spatial
66
+ bundle exec rake test:postgis
67
+ bundle exec rake test:postgresql
68
+ bundle exec rake test:postgresql_makara
69
+ bundle exec rake test:seamless_database_pool
70
+ bundle exec rake test:spatialite
71
+ bundle exec rake test:sqlite3
72
+ - name: Run Rubocop
73
+ run: bundle exec rubocop
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## Changes in 1.2.0
2
+
3
+ ### Fixes
4
+
5
+ * Update JDBC MySQL adapter to use mysql2 connection adapter. Thanks to @terencechow via \##744.
6
+ * Fix importing STI models with ActiveRecord 6. Thanks to @clemens1483 via \##743.
7
+ * Use polymorphic_name instead of base_class.name for imports. Thanks to @kmhajjar via \##741.
8
+ * Fix compatibility issue with composite primary keys. Thanks to @dlanileonardo via \##737.
9
+ * Prevent double validation of associations on recursive import.
10
+
1
11
  ## Changes in 1.1.0
2
12
 
3
13
  ### New Features
@@ -27,7 +37,7 @@
27
37
 
28
38
  * Handle after_initialize callbacks. Thanks to @AhMohsen46 via \#691 and
29
39
  \#692.
30
- * Fix regression introduced in 1.0.4. Explicity allow adapters to
40
+ * Fix regression introduced in 1.0.4. Explicitly allow adapters to
31
41
  support on duplicate key update. Thanks to @dsobiera, @jkowens via \#696.
32
42
 
33
43
  ## Changes in 1.0.5
@@ -36,7 +46,7 @@
36
46
 
37
47
  * Allow serialized attributes to be returned from import. Thanks to @timanovsky, @jkowens via \#660.
38
48
  * Return ActiveRecord::Connection from
39
- ActiveREcord::Base#establish_connection. Thanks to @reverentF via
49
+ ActiveRecord::Base#establish_connection. Thanks to @reverentF via
40
50
  \#663.
41
51
  * Support PostgreSQL array. Thanks to @ujihisa via \#669.
42
52
  * Skip loading association ids when column changed. Thanks to @Aristat
@@ -91,7 +101,7 @@
91
101
  * Fix import issue for models with Postgresql json/jsonb fields. Thanks
92
102
  to @stokarenko via \#594.
93
103
  * Fix issue importing models with timestamps that contain timezone
94
- information. Thaks to @dekaikiwi, @jkowens via \#598.
104
+ information. Thanks to @dekaikiwi, @jkowens via \#598.
95
105
  * Ignore :no_returning when using :recursive option. Thanks to @dgollahon, @jkowens
96
106
  via \#599.
97
107
 
data/Gemfile CHANGED
@@ -26,7 +26,7 @@ platforms :ruby do
26
26
  end
27
27
 
28
28
  platforms :jruby do
29
- gem "jdbc-mysql"
29
+ gem 'jdbc-mysql', '< 8', require: false
30
30
  gem "jdbc-postgres"
31
31
  gem "activerecord-jdbcsqlite3-adapter", "~> 1.3"
32
32
  gem "activerecord-jdbcmysql-adapter", "~> 1.3"
data/README.markdown CHANGED
@@ -1,4 +1,4 @@
1
- # Activerecord-Import [![Build Status](https://travis-ci.org/zdennis/activerecord-import.svg?branch=master)](https://travis-ci.org/zdennis/activerecord-import)
1
+ # Activerecord-Import ![Build Status](https://github.com/zdennis/activerecord-import/actions/workflows/test.yaml/badge.svg)
2
2
 
3
3
  Activerecord-Import is a library for bulk inserting data using ActiveRecord.
4
4
 
@@ -266,8 +266,8 @@ Key | Options | Default | Descrip
266
266
  ------------------------- | --------------------- | ------------------ | -----------
267
267
  :validate | `true`/`false` | `true` | Whether or not to run `ActiveRecord` validations (uniqueness skipped). This option will always be true when using `import!`.
268
268
  :validate_uniqueness | `true`/`false` | `false` | Whether or not to run uniqueness validations, has potential pitfalls, use with caution (requires `>= v0.27.0`).
269
- :validate_with_context | `Symbol` |`:create`/`:update` | Allows passing an ActiveModel validation context for each model. Default is `:create` for new records and `:update` for existing ones.
270
- :track_validation_failures| `true`/`false` | `false` | When this is set to true, `failed_instances` will be an array of arrays, with each inner array having the form `[:index_in_dataset, :object_with_errors]`
269
+ :validate_with_context | `Symbol` |`:create`/`:update` | Allows passing an ActiveModel validation context for each model. Default is `:create` for new records and `:update` for existing ones.
270
+ :track_validation_failures| `true`/`false` | `false` | When this is set to true, `failed_instances` will be an array of arrays, with each inner array having the form `[:index_in_dataset, :object_with_errors]`
271
271
  :on_duplicate_key_ignore | `true`/`false` | `false` | Allows skipping records with duplicate keys. See [here](https://github.com/zdennis/activerecord-import/#duplicate-key-ignore) for more details.
272
272
  :ignore | `true`/`false` | `false` | Alias for :on_duplicate_key_ignore.
273
273
  :on_duplicate_key_update | :all, `Array`, `Hash` | N/A | Allows upsert logic to be used. See [here](https://github.com/zdennis/activerecord-import/#duplicate-key-update) for more details.
@@ -392,7 +392,7 @@ Book.import books, validate_uniqueness: true
392
392
 
393
393
  ### Return Info
394
394
 
395
- The `import` method returns a `Result` object that responds to `failed_instances` and `num_inserts`. Additionally, for users of Postgres, there will be two arrays `ids` and `results` that can be accessed`.
395
+ The `import` method returns a `Result` object that responds to `failed_instances` and `num_inserts`. Additionally, for users of Postgres, there will be two arrays `ids` and `results` that can be accessed.
396
396
 
397
397
  ```ruby
398
398
  articles = [
@@ -1,6 +1,6 @@
1
- require "active_record/connection_adapters/mysql_adapter"
2
- require "activerecord-import/adapters/mysql_adapter"
1
+ require "active_record/connection_adapters/mysql2_adapter"
2
+ require "activerecord-import/adapters/mysql2_adapter"
3
3
 
4
- class ActiveRecord::ConnectionAdapters::MysqlAdapter
5
- include ActiveRecord::Import::MysqlAdapter
4
+ class ActiveRecord::ConnectionAdapters::Mysql2Adapter
5
+ include ActiveRecord::Import::Mysql2Adapter
6
6
  end
@@ -49,7 +49,7 @@ module ActiveRecord::Import #:nodoc:
49
49
  associations = klass.reflect_on_all_associations(:belongs_to)
50
50
  associations.each do |assoc|
51
51
  if (index = attrs.index(assoc.name))
52
- key = assoc.foreign_key.to_sym
52
+ key = assoc.foreign_key.is_a?(Array) ? assoc.foreign_key.map(&:to_sym) : assoc.foreign_key.to_sym
53
53
  attrs[index] = key unless attrs.include?(key)
54
54
  end
55
55
  end
@@ -734,7 +734,7 @@ class ActiveRecord::Base
734
734
  set_attributes_and_mark_clean(models, return_obj, timestamps, options)
735
735
 
736
736
  # if there are auto-save associations on the models we imported that are new, import them as well
737
- import_associations(models, options.dup) if options[:recursive]
737
+ import_associations(models, options.dup.merge(validate: false)) if options[:recursive]
738
738
  end
739
739
 
740
740
  return_obj
@@ -775,21 +775,22 @@ class ActiveRecord::Base
775
775
  unless scope_columns.blank?
776
776
  scope_columns.zip(scope_values).each do |name, value|
777
777
  name_as_sym = name.to_sym
778
- next if column_names.include?(name_as_sym)
779
-
780
- is_sti = (name_as_sym == inheritance_column.to_sym && self < base_class)
781
- value = Array(value).first if is_sti
782
-
778
+ next if column_names.include?(name_as_sym) || name_as_sym == inheritance_column.to_sym
783
779
  column_names << name_as_sym
784
780
  array_of_attributes.each { |attrs| attrs << value }
785
781
  end
786
782
  end
787
783
 
784
+ if finder_needs_type_condition?
785
+ unless column_names.include?(inheritance_column.to_sym)
786
+ column_names << inheritance_column.to_sym
787
+ array_of_attributes.each { |attrs| attrs << sti_name }
788
+ end
789
+ end
790
+
788
791
  columns = column_names.each_with_index.map do |name, i|
789
792
  column = columns_hash[name.to_s]
790
-
791
793
  raise ActiveRecord::Import::MissingColumnError.new(name.to_s, i) if column.nil?
792
-
793
794
  column
794
795
  end
795
796
 
@@ -961,8 +962,13 @@ class ActiveRecord::Base
961
962
  changed_objects.each do |child|
962
963
  child.public_send("#{association_reflection.foreign_key}=", model.id)
963
964
  # For polymorphic associations
965
+ association_name = if model.class.respond_to?(:polymorphic_name)
966
+ model.class.polymorphic_name
967
+ else
968
+ model.class.base_class
969
+ end
964
970
  association_reflection.type.try do |type|
965
- child.public_send("#{type}=", model.class.base_class.name)
971
+ child.public_send("#{type}=", association_name)
966
972
  end
967
973
  end
968
974
  associated_objects_by_class[model.class.name][association_reflection.name].concat changed_objects
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Import
3
- VERSION = "1.1.0".freeze
3
+ VERSION = "1.2.0".freeze
4
4
  end
5
5
  end
@@ -1,7 +1,8 @@
1
1
  common: &common
2
2
  username: root
3
- password:
3
+ password: root
4
4
  encoding: utf8
5
+ collation: utf8_general_ci
5
6
  host: localhost
6
7
  database: activerecord_import_test
7
8
 
@@ -37,6 +38,7 @@ oracle:
37
38
  postgresql: &postgresql
38
39
  <<: *common
39
40
  username: postgres
41
+ password: postgres
40
42
  adapter: postgresql
41
43
  min_messages: warning
42
44
 
data/test/import_test.rb CHANGED
@@ -169,7 +169,17 @@ describe "#import" do
169
169
  assert_difference "Dictionary.count", +1 do
170
170
  Dictionary.import dictionaries
171
171
  end
172
- assert_equal "Dictionary", Dictionary.first.type
172
+ assert_equal "Dictionary", Dictionary.last.type
173
+ end
174
+
175
+ it "should import arrays successfully" do
176
+ columns = [:author_name, :title]
177
+ values = [["Noah Webster", "Webster's Dictionary"]]
178
+
179
+ assert_difference "Dictionary.count", +1 do
180
+ Dictionary.import columns, values
181
+ end
182
+ assert_equal "Dictionary", Dictionary.last.type
173
183
  end
174
184
  end
175
185
 
@@ -661,6 +671,14 @@ describe "#import" do
661
671
  assert_equal [val1, val2], scope.map(&column).sort
662
672
  end
663
673
 
674
+ context "for cards and decks" do
675
+ it "works when the polymorphic name is different than base class name" do
676
+ deck = Deck.create(id: 1, name: 'test')
677
+ deck.cards.import [:id, :deck_type], [[1, 'PlayingCard']]
678
+ assert_equal deck.cards.first.deck_type, "PlayingCard"
679
+ end
680
+ end
681
+
664
682
  it "works importing array of hashes" do
665
683
  scope.import [{ column => val1 }, { column => val2 }]
666
684
 
@@ -0,0 +1,3 @@
1
+ class Card < ActiveRecord::Base
2
+ belongs_to :deck, polymorphic: true
3
+ end
@@ -0,0 +1,6 @@
1
+ class Deck < ActiveRecord::Base
2
+ has_many :cards
3
+ def self.polymorphic_name
4
+ "PlayingCard"
5
+ end
6
+ end
@@ -0,0 +1,2 @@
1
+ class PlayingCard < ActiveRecord::Base
2
+ end
@@ -52,6 +52,20 @@ ActiveRecord::Schema.define do
52
52
  t.string :name
53
53
  end
54
54
 
55
+ create_table :cards, force: :cascade do |t|
56
+ t.string :name
57
+ t.string :deck_type
58
+ t.integer :deck_id
59
+ end
60
+
61
+ create_table :decks, force: :cascade do |t|
62
+ t.string :name
63
+ end
64
+
65
+ create_table :playing_cards, force: :cascade do |t|
66
+ t.string :name
67
+ end
68
+
55
69
  create_table :books, force: :cascade do |t|
56
70
  t.string :title, null: false
57
71
  t.string :publisher, null: false, default: 'Default Publisher'
data/test/test_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'pathname'
2
+ require 'rake'
2
3
  test_dir = Pathname.new File.dirname(__FILE__)
3
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
5
  $LOAD_PATH.unshift(File.dirname(__FILE__))
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: 1.1.0
4
+ version: 1.2.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: 2021-05-15 00:00:00.000000000 Z
11
+ date: 2021-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -45,10 +45,10 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - ".github/workflows/test.yaml"
48
49
  - ".gitignore"
49
50
  - ".rubocop.yml"
50
51
  - ".rubocop_todo.yml"
51
- - ".travis.yml"
52
52
  - Brewfile
53
53
  - CHANGELOG.md
54
54
  - Gemfile
@@ -114,6 +114,7 @@ files:
114
114
  - test/adapters/spatialite.rb
115
115
  - test/adapters/sqlite3.rb
116
116
  - test/database.yml.sample
117
+ - test/github/database.yml
117
118
  - test/import_test.rb
118
119
  - test/jdbcmysql/import_test.rb
119
120
  - test/jdbcpostgresql/import_test.rb
@@ -125,11 +126,14 @@ files:
125
126
  - test/models/bike_maker.rb
126
127
  - test/models/book.rb
127
128
  - test/models/car.rb
129
+ - test/models/card.rb
128
130
  - test/models/chapter.rb
131
+ - test/models/deck.rb
129
132
  - test/models/dictionary.rb
130
133
  - test/models/discount.rb
131
134
  - test/models/end_note.rb
132
135
  - test/models/group.rb
136
+ - test/models/playing_card.rb
133
137
  - test/models/promotion.rb
134
138
  - test/models/question.rb
135
139
  - test/models/rule.rb
@@ -164,7 +168,6 @@ files:
164
168
  - test/support/sqlite3/import_examples.rb
165
169
  - test/synchronize_test.rb
166
170
  - test/test_helper.rb
167
- - test/travis/database.yml
168
171
  - test/value_sets_bytes_parser_test.rb
169
172
  - test/value_sets_records_parser_test.rb
170
173
  homepage: https://github.com/zdennis/activerecord-import
@@ -186,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
189
  - !ruby/object:Gem::Version
187
190
  version: '0'
188
191
  requirements: []
189
- rubygems_version: 3.0.8
192
+ rubygems_version: 3.0.9
190
193
  signing_key:
191
194
  specification_version: 4
192
195
  summary: Bulk insert extension for ActiveRecord
@@ -205,6 +208,7 @@ test_files:
205
208
  - test/adapters/spatialite.rb
206
209
  - test/adapters/sqlite3.rb
207
210
  - test/database.yml.sample
211
+ - test/github/database.yml
208
212
  - test/import_test.rb
209
213
  - test/jdbcmysql/import_test.rb
210
214
  - test/jdbcpostgresql/import_test.rb
@@ -216,11 +220,14 @@ test_files:
216
220
  - test/models/bike_maker.rb
217
221
  - test/models/book.rb
218
222
  - test/models/car.rb
223
+ - test/models/card.rb
219
224
  - test/models/chapter.rb
225
+ - test/models/deck.rb
220
226
  - test/models/dictionary.rb
221
227
  - test/models/discount.rb
222
228
  - test/models/end_note.rb
223
229
  - test/models/group.rb
230
+ - test/models/playing_card.rb
224
231
  - test/models/promotion.rb
225
232
  - test/models/question.rb
226
233
  - test/models/rule.rb
@@ -255,6 +262,5 @@ test_files:
255
262
  - test/support/sqlite3/import_examples.rb
256
263
  - test/synchronize_test.rb
257
264
  - test/test_helper.rb
258
- - test/travis/database.yml
259
265
  - test/value_sets_bytes_parser_test.rb
260
266
  - test/value_sets_records_parser_test.rb
data/.travis.yml DELETED
@@ -1,76 +0,0 @@
1
- language: ruby
2
- cache: bundler
3
- rvm:
4
- - 2.5.8
5
-
6
- env:
7
- global:
8
- # https://github.com/discourse/discourse/blob/master/.travis.yml
9
- - RUBY_GC_MALLOC_LIMIT=50000000
10
- matrix:
11
- - AR_VERSION=5.1
12
- - AR_VERSION=5.2
13
- - AR_VERSION=6.0
14
- - AR_VERSION=6.1
15
-
16
- matrix:
17
- include:
18
- - rvm: 2.3.8
19
- env: AR_VERSION=3.2
20
- - rvm: 2.3.8
21
- env: AR_VERSION=4.0
22
- - rvm: 2.3.8
23
- env: AR_VERSION=4.1
24
- - rvm: 2.3.8
25
- env: AR_VERSION=4.2
26
- - rvm: 2.3.8
27
- env: AR_VERSION=5.0
28
-
29
- fast_finish: true
30
-
31
- addons:
32
- postgresql: "10"
33
- apt:
34
- sources:
35
- - travis-ci/sqlite3
36
- - mysql-5.7-trusty
37
- packages:
38
- - sqlite3
39
- - mysql-server
40
- - mysql-client
41
- - postgresql-10-postgis-2.4
42
-
43
- before_install:
44
- - sudo apt-get update
45
- - gem update --system
46
- - sudo mysql -e "use mysql; update user set authentication_string=PASSWORD('') where User='root'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;"
47
- - sudo mysql_upgrade
48
- - sudo service mysql restart
49
-
50
- before_script:
51
- - mysql -e 'create database activerecord_import_test;'
52
- - psql -c 'create database activerecord_import_test;' -U postgres
53
- - psql activerecord_import_test -c 'create extension if not exists hstore;' -U postgres
54
- - psql -c 'create extension if not exists postgis;' -U postgres
55
- - psql -c 'create extension if not exists "uuid-ossp";' -U postgres
56
- - cp test/travis/database.yml test/database.yml
57
-
58
- script:
59
- - bundle exec rake test:mysql2
60
- - bundle exec rake test:mysql2_makara
61
- - bundle exec rake test:mysql2spatial
62
- - bundle exec rake test:postgis
63
- - bundle exec rake test:postgresql
64
- - bundle exec rake test:postgresql_makara
65
- - bundle exec rake test:seamless_database_pool
66
- - bundle exec rake test:spatialite
67
- - bundle exec rake test:sqlite3
68
- - bundle exec rubocop
69
-
70
- dist: xenial
71
-
72
- services:
73
- - mysql
74
- - postgresql
75
-
76
- sudo: required