activerecord-import 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +74 -8
  3. data/Brewfile +3 -1
  4. data/CHANGELOG.md +6 -0
  5. data/Gemfile +3 -1
  6. data/README.markdown +2 -2
  7. data/Rakefile +2 -0
  8. data/activerecord-import.gemspec +2 -1
  9. data/benchmarks/benchmark.rb +2 -0
  10. data/benchmarks/lib/base.rb +2 -0
  11. data/benchmarks/lib/cli_parser.rb +3 -1
  12. data/benchmarks/lib/float.rb +2 -0
  13. data/benchmarks/lib/mysql2_benchmark.rb +2 -0
  14. data/benchmarks/lib/output_to_csv.rb +2 -0
  15. data/benchmarks/lib/output_to_html.rb +4 -2
  16. data/benchmarks/models/test_innodb.rb +2 -0
  17. data/benchmarks/models/test_memory.rb +2 -0
  18. data/benchmarks/models/test_myisam.rb +2 -0
  19. data/benchmarks/schema/mysql2_schema.rb +2 -0
  20. data/gemfiles/4.2.gemfile +2 -0
  21. data/gemfiles/5.0.gemfile +2 -0
  22. data/gemfiles/5.1.gemfile +2 -0
  23. data/gemfiles/5.2.gemfile +2 -0
  24. data/gemfiles/6.0.gemfile +2 -0
  25. data/gemfiles/6.1.gemfile +2 -0
  26. data/gemfiles/7.0.gemfile +3 -0
  27. data/lib/activerecord-import/active_record/adapters/abstract_adapter.rb +2 -0
  28. data/lib/activerecord-import/active_record/adapters/jdbcmysql_adapter.rb +2 -0
  29. data/lib/activerecord-import/active_record/adapters/jdbcpostgresql_adapter.rb +2 -0
  30. data/lib/activerecord-import/active_record/adapters/jdbcsqlite3_adapter.rb +2 -0
  31. data/lib/activerecord-import/active_record/adapters/mysql2_adapter.rb +2 -0
  32. data/lib/activerecord-import/active_record/adapters/postgresql_adapter.rb +2 -0
  33. data/lib/activerecord-import/active_record/adapters/seamless_database_pool_adapter.rb +2 -0
  34. data/lib/activerecord-import/active_record/adapters/sqlite3_adapter.rb +2 -0
  35. data/lib/activerecord-import/adapters/abstract_adapter.rb +2 -0
  36. data/lib/activerecord-import/adapters/em_mysql2_adapter.rb +2 -0
  37. data/lib/activerecord-import/adapters/mysql2_adapter.rb +2 -0
  38. data/lib/activerecord-import/adapters/mysql_adapter.rb +2 -0
  39. data/lib/activerecord-import/adapters/postgresql_adapter.rb +37 -24
  40. data/lib/activerecord-import/adapters/sqlite3_adapter.rb +4 -2
  41. data/lib/activerecord-import/base.rb +3 -1
  42. data/lib/activerecord-import/import.rb +21 -10
  43. data/lib/activerecord-import/mysql2.rb +2 -0
  44. data/lib/activerecord-import/postgresql.rb +2 -0
  45. data/lib/activerecord-import/sqlite3.rb +2 -0
  46. data/lib/activerecord-import/synchronize.rb +2 -0
  47. data/lib/activerecord-import/value_sets_parser.rb +2 -0
  48. data/lib/activerecord-import/version.rb +3 -1
  49. data/lib/activerecord-import.rb +3 -1
  50. data/test/adapters/jdbcmysql.rb +2 -0
  51. data/test/adapters/jdbcpostgresql.rb +2 -0
  52. data/test/adapters/jdbcsqlite3.rb +2 -0
  53. data/test/adapters/makara_postgis.rb +2 -0
  54. data/test/adapters/mysql2.rb +2 -0
  55. data/test/adapters/mysql2_makara.rb +2 -0
  56. data/test/adapters/mysql2spatial.rb +2 -0
  57. data/test/adapters/postgis.rb +2 -0
  58. data/test/adapters/postgresql.rb +2 -0
  59. data/test/adapters/postgresql_makara.rb +2 -0
  60. data/test/adapters/seamless_database_pool.rb +2 -0
  61. data/test/adapters/spatialite.rb +2 -0
  62. data/test/adapters/sqlite3.rb +2 -0
  63. data/test/import_test.rb +21 -0
  64. data/test/jdbcmysql/import_test.rb +2 -0
  65. data/test/jdbcpostgresql/import_test.rb +2 -0
  66. data/test/jdbcsqlite3/import_test.rb +2 -0
  67. data/test/makara_postgis/import_test.rb +2 -0
  68. data/test/models/account.rb +2 -0
  69. data/test/models/alarm.rb +2 -0
  70. data/test/models/animal.rb +2 -0
  71. data/test/models/bike_maker.rb +2 -0
  72. data/test/models/book.rb +2 -0
  73. data/test/models/car.rb +2 -0
  74. data/test/models/card.rb +2 -0
  75. data/test/models/chapter.rb +2 -0
  76. data/test/models/customer.rb +2 -0
  77. data/test/models/deck.rb +2 -0
  78. data/test/models/dictionary.rb +2 -0
  79. data/test/models/discount.rb +2 -0
  80. data/test/models/end_note.rb +2 -0
  81. data/test/models/group.rb +2 -0
  82. data/test/models/order.rb +2 -0
  83. data/test/models/playing_card.rb +2 -0
  84. data/test/models/promotion.rb +2 -0
  85. data/test/models/question.rb +2 -0
  86. data/test/models/rule.rb +2 -0
  87. data/test/models/tag.rb +3 -0
  88. data/test/models/tag_alias.rb +5 -0
  89. data/test/models/topic.rb +2 -0
  90. data/test/models/user.rb +2 -0
  91. data/test/models/user_token.rb +2 -0
  92. data/test/models/vendor.rb +2 -0
  93. data/test/models/widget.rb +2 -0
  94. data/test/mysql2/import_test.rb +2 -0
  95. data/test/mysql2_makara/import_test.rb +2 -0
  96. data/test/mysqlspatial2/import_test.rb +2 -0
  97. data/test/postgis/import_test.rb +2 -0
  98. data/test/postgresql/import_test.rb +2 -0
  99. data/test/schema/generic_schema.rb +8 -0
  100. data/test/schema/jdbcpostgresql_schema.rb +2 -0
  101. data/test/schema/mysql2_schema.rb +2 -0
  102. data/test/schema/postgis_schema.rb +2 -0
  103. data/test/schema/postgresql_schema.rb +2 -0
  104. data/test/schema/sqlite3_schema.rb +2 -0
  105. data/test/schema/version.rb +2 -0
  106. data/test/sqlite3/import_test.rb +2 -0
  107. data/test/support/active_support/test_case_extensions.rb +2 -0
  108. data/test/support/assertions.rb +2 -0
  109. data/test/support/factories.rb +2 -0
  110. data/test/support/generate.rb +4 -2
  111. data/test/support/mysql/import_examples.rb +2 -1
  112. data/test/support/postgresql/import_examples.rb +40 -1
  113. data/test/support/shared_examples/on_duplicate_key_ignore.rb +2 -0
  114. data/test/support/shared_examples/on_duplicate_key_update.rb +2 -0
  115. data/test/support/shared_examples/recursive_import.rb +2 -0
  116. data/test/support/sqlite3/import_examples.rb +2 -1
  117. data/test/synchronize_test.rb +2 -0
  118. data/test/test_helper.rb +8 -0
  119. data/test/value_sets_bytes_parser_test.rb +2 -0
  120. data/test/value_sets_records_parser_test.rb +2 -0
  121. metadata +5 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 71623b61f085adce5254fc92ae524f595594cb5a960bb4bf0cb46d961ca9d203
4
- data.tar.gz: ce2200678685afd85dc0a887d967bae7e2e4220942ab2f8fc34bb949b8f5a31b
3
+ metadata.gz: 1b9c717d31cf5a4012568c9d8497947466408be7fef5610fba9143f7a911ed7c
4
+ data.tar.gz: 925bc5f645152eeb09f9583e9d50b9be67c4bfa8049c303571f82649262e4d5b
5
5
  SHA512:
6
- metadata.gz: 71bdd79b977164223d77c77ec796ac3914124318bb377c8aa044bc8085d0f7ba9536e35b99b6a670b0a4c825e4dfe49aef5a10626b92109b2ce7dd8cd529dbb1
7
- data.tar.gz: 89db7ca7f321a473391d2083a2dc6cb4fe8614069441c2a16601a0a3e4ab848e889eafba67b53fc2042d60b7095aab50926f4795d07930c0c90820527f07c28c
6
+ metadata.gz: 409d66e4d4e6e9ac31940cea097837d76c93cbeb4b95461323afc6a5f2ace736f945705e5ef33f2a851bb92c13c27e19ffbc14c7e776a0366f1d76091cafd51f
7
+ data.tar.gz: 6a0eaa48b85f7b1863dfb2b04c3c193a5e7e1108084556ba1b25fa303bccb40f62dd706ec8d4b5c3394239f167eb1807dfff4fe292edc1e4ad550719a9a63446
data/.rubocop.yml CHANGED
@@ -1,11 +1,11 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
 
3
- Lint/EndAlignment:
4
- AlignWith: variable
5
-
6
3
  Metrics/AbcSize:
7
4
  Enabled: false
8
5
 
6
+ Metrics/BlockLength:
7
+ Enabled: false
8
+
9
9
  Metrics/ClassLength:
10
10
  Enabled: false
11
11
 
@@ -24,8 +24,8 @@ Metrics/ModuleLength:
24
24
  Metrics/PerceivedComplexity:
25
25
  Enabled: false
26
26
 
27
- Style/AlignParameters:
28
- EnforcedStyle: with_fixed_indentation
27
+ Style/CommentedKeyword:
28
+ Enabled: false
29
29
 
30
30
  Style/ClassAndModuleChildren:
31
31
  Enabled: false
@@ -33,10 +33,25 @@ Style/ClassAndModuleChildren:
33
33
  Style/Documentation:
34
34
  Enabled: false
35
35
 
36
- Style/ElseAlignment:
36
+ Style/EvalWithLocation:
37
+ Enabled: false
38
+
39
+ Style/ExpandPathArguments:
40
+ Enabled: false
41
+
42
+ Style/GuardClause:
43
+ Enabled: false
44
+
45
+ Style/IfUnlessModifier:
46
+ Enabled: false
47
+
48
+ Style/NumericPredicate:
49
+ Enabled: false
50
+
51
+ Style/PercentLiteralDelimiters:
37
52
  Enabled: false
38
53
 
39
- Style/SpaceInsideParens:
54
+ Style/RedundantBegin:
40
55
  Enabled: false
41
56
 
42
57
  Style/SpecialGlobalVars:
@@ -45,5 +60,56 @@ Style/SpecialGlobalVars:
45
60
  Style/StringLiterals:
46
61
  Enabled: false
47
62
 
48
- Style/TrailingCommaInLiteral:
63
+ Style/SymbolArray:
64
+ Enabled: false
65
+
66
+ Style/TrailingCommaInArrayLiteral:
67
+ Enabled: false
68
+
69
+ Layout/AlignArguments:
70
+ Enabled: false
71
+
72
+ Layout/AlignParameters:
73
+ EnforcedStyle: with_fixed_indentation
74
+
75
+ Layout/EndAlignment:
76
+ EnforcedStyleAlignWith: variable
77
+
78
+ Layout/ElseAlignment:
79
+ Enabled: false
80
+
81
+ Layout/EmptyLineAfterGuardClause:
82
+ Enabled: false
83
+
84
+ Layout/IndentHeredoc:
85
+ Enabled: false
86
+
87
+ Layout/SpaceInsideParens:
88
+ Enabled: false
89
+
90
+ Layout/SpaceInsidePercentLiteralDelimiters:
91
+ Enabled: false
92
+
93
+ Lint/ErbNewArguments:
94
+ Enabled: false
95
+
96
+ Lint/MissingCopEnableDirective:
97
+ Enabled: false
98
+
99
+ Lint/PercentStringArray:
100
+ Enabled: false
101
+
102
+ Naming/HeredocDelimiterNaming:
103
+ Enabled: false
104
+
105
+ Naming/UncommunicativeMethodParamName:
106
+ Enabled: false
107
+
108
+ Security/YAMLLoad:
109
+ Enabled: false
110
+
111
+ Gemspec/RequiredRubyVersion:
112
+ Enabled: false
113
+
114
+ Bundler/OrderedGems:
49
115
  Enabled: false
data/Brewfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  brew "mysql"
2
4
  brew "postgresql"
3
- brew "sqlite"
5
+ brew "sqlite"
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## Changes in 1.4.1
2
+
3
+ ### Fixes
4
+
5
+ * Fix importing models that have required belongs_to associations and use composite primary keys. Thanks to @thoughtbot-summer vi \##783.
6
+
1
7
  ## Changes in 1.4.0
2
8
 
3
9
  ### New Features
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gemspec
@@ -13,7 +15,7 @@ pg_version = '0.9'
13
15
  pg_version = '1.1' if version >= 6.1
14
16
 
15
17
  group :development, :test do
16
- gem 'rubocop', '~> 0.40.0'
18
+ gem 'rubocop', '~> 0.71.0'
17
19
  gem 'rake'
18
20
  end
19
21
 
data/README.markdown CHANGED
@@ -268,9 +268,9 @@ Key | Options | Default | Descrip
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
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
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
- :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.
271
+ :on_duplicate_key_ignore | `true`/`false` | `false` | Allows skipping records with duplicate keys. See [here](#duplicate-key-ignore) for more details.
272
272
  :ignore | `true`/`false` | `false` | Alias for :on_duplicate_key_ignore.
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.
273
+ :on_duplicate_key_update | :all, `Array`, `Hash` | N/A | Allows upsert logic to be used. See [here](#duplicate-key-update) for more details.
274
274
  :synchronize | `Array` | N/A | An array of ActiveRecord instances. This synchronizes existing instances in memory with updates from the import.
275
275
  :timestamps | `true`/`false` | `true` | Enables/disables timestamps on imported records.
276
276
  :recursive | `true`/`false` | `false` | Imports has_many/has_one associations (PostgreSQL only).
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler"
2
4
  Bundler.setup
3
5
 
@@ -1,4 +1,5 @@
1
- # -*- encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
+
2
3
  require File.expand_path('../lib/activerecord-import/version', __FILE__)
3
4
 
4
5
  Gem::Specification.new do |gem|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pathname'
2
4
  require "fileutils"
3
5
  require "active_record"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class BenchmarkBase
2
4
  attr_reader :results
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'optparse'
2
4
  require 'ostruct'
3
5
 
@@ -8,7 +10,7 @@ require 'ostruct'
8
10
  # * t - the table types to test. ie: myisam, innodb, memory, temporary, etc.
9
11
  #
10
12
  module BenchmarkOptionParser
11
- BANNER = "Usage: ruby #{$0} [options]\nSee ruby #{$0} -h for more options.".freeze
13
+ BANNER = "Usage: ruby #{$0} [options]\nSee ruby #{$0} -h for more options."
12
14
 
13
15
  def self.print_banner
14
16
  puts BANNER
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Taken from http://www.programmingishard.com/posts/show/128
2
4
  # Posted by rbates
3
5
  class Float
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Mysql2Benchmark < BenchmarkBase
2
4
  def benchmark_all( array_of_cols_and_vals )
3
5
  methods = self.methods.find_all { |m| m =~ /benchmark_/ }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'csv'
2
4
 
3
5
  module OutputToCSV
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'erb'
2
4
 
3
5
  module OutputToHTML
4
- TEMPLATE_HEADER = <<"EOT".freeze
6
+ TEMPLATE_HEADER = <<"EOT"
5
7
  <div>
6
8
  All times are rounded to the nearest thousandth for display purposes. Speedups next to each time are computed
7
9
  before any rounding occurs. Also, all speedup calculations are computed by comparing a given time against
@@ -9,7 +11,7 @@ module OutputToHTML
9
11
  </div>
10
12
  EOT
11
13
 
12
- TEMPLATE = <<"EOT".freeze
14
+ TEMPLATE = <<"EOT"
13
15
  <style>
14
16
  td#benchmarkTitle {
15
17
  border: 1px solid black;
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class TestInnoDb < ActiveRecord::Base
2
4
  self.table_name = 'test_innodb'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class TestMemory < ActiveRecord::Base
2
4
  self.table_name = 'test_memory'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class TestMyISAM < ActiveRecord::Base
2
4
  self.table_name = 'test_myisam'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ActiveRecord::Schema.define do
2
4
  create_table :test_myisam, options: 'ENGINE=MyISAM', force: true do |t|
3
5
  t.column :my_name, :string, null: false
data/gemfiles/4.2.gemfile CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  gem 'activerecord', '~> 4.2.0'
2
4
  gem 'composite_primary_keys', '~> 8.0'
data/gemfiles/5.0.gemfile CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  gem 'activerecord', '~> 5.0.0'
2
4
  gem 'composite_primary_keys', '~> 9.0'
data/gemfiles/5.1.gemfile CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  gem 'activerecord', '~> 5.1.0'
2
4
  gem 'composite_primary_keys', '~> 10.0'
data/gemfiles/5.2.gemfile CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  gem 'activerecord', '~> 5.2.0'
2
4
  gem 'composite_primary_keys', '~> 11.0'
data/gemfiles/6.0.gemfile CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  gem 'activerecord', '~> 6.0.0'
2
4
  gem 'composite_primary_keys', '~> 12.0'
data/gemfiles/6.1.gemfile CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  gem 'activerecord', '~> 6.1.0'
2
4
  gem 'composite_primary_keys', '~> 13.0'
data/gemfiles/7.0.gemfile CHANGED
@@ -1 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  gem 'activerecord', '~> 7.0.0'
4
+ gem 'composite_primary_keys', '~> 14.0'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "activerecord-import/adapters/abstract_adapter"
2
4
 
3
5
  module ActiveRecord # :nodoc:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_record/connection_adapters/mysql2_adapter"
2
4
  require "activerecord-import/adapters/mysql2_adapter"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_record/connection_adapters/postgresql_adapter"
2
4
  require "activerecord-import/adapters/postgresql_adapter"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_record/connection_adapters/sqlite3_adapter"
2
4
  require "activerecord-import/adapters/sqlite3_adapter"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_record/connection_adapters/mysql2_adapter"
2
4
  require "activerecord-import/adapters/mysql2_adapter"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_record/connection_adapters/postgresql_adapter"
2
4
  require "activerecord-import/adapters/postgresql_adapter"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "seamless_database_pool"
2
4
  require "active_record/connection_adapters/seamless_database_pool_adapter"
3
5
  require "activerecord-import/adapters/mysql_adapter"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_record/connection_adapters/sqlite3_adapter"
2
4
  require "activerecord-import/adapters/sqlite3_adapter"
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord::Import::AbstractAdapter
2
4
  module InstanceMethods
3
5
  def next_value_for_sequence(sequence_name)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "activerecord-import/adapters/mysql_adapter"
2
4
 
3
5
  module ActiveRecord::Import::EMMysql2Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "activerecord-import/adapters/mysql_adapter"
2
4
 
3
5
  module ActiveRecord::Import::Mysql2Adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord::Import::MysqlAdapter
2
4
  include ActiveRecord::Import::ImportSupport
3
5
  include ActiveRecord::Import::OnDuplicateKeyUpdateSupport
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord::Import::PostgreSQLAdapter
2
4
  include ActiveRecord::Import::ImportSupport
3
5
  include ActiveRecord::Import::OnDuplicateKeyUpdateSupport
@@ -6,7 +8,7 @@ module ActiveRecord::Import::PostgreSQLAdapter
6
8
 
7
9
  def insert_many( sql, values, options = {}, *args ) # :nodoc:
8
10
  number_of_inserts = 1
9
- returned_values = []
11
+ returned_values = {}
10
12
  ids = []
11
13
  results = []
12
14
 
@@ -18,47 +20,53 @@ module ActiveRecord::Import::PostgreSQLAdapter
18
20
 
19
21
  sql2insert = base_sql + values.join( ',' ) + post_sql
20
22
 
21
- columns = returning_columns(options)
22
- if columns.blank? || (options[:no_returning] && !options[:recursive])
23
+ selections = returning_selections(options)
24
+ if selections.blank? || (options[:no_returning] && !options[:recursive])
23
25
  insert( sql2insert, *args )
24
26
  else
25
- returned_values = if columns.size > 1
27
+ returned_values = if selections.size > 1
26
28
  # Select composite columns
27
- select_rows( sql2insert, *args )
29
+ db_result = select_all( sql2insert, *args )
30
+ { values: db_result.rows, columns: db_result.columns }
28
31
  else
29
- select_values( sql2insert, *args )
32
+ { values: select_values( sql2insert, *args ) }
30
33
  end
31
34
  clear_query_cache if query_cache_enabled
32
35
  end
33
36
 
34
37
  if options[:returning].blank?
35
- ids = returned_values
38
+ ids = Array(returned_values[:values])
36
39
  elsif options[:primary_key].blank?
37
- results = returned_values
40
+ options[:returning_columns] ||= returned_values[:columns]
41
+ results = Array(returned_values[:values])
38
42
  else
39
43
  # split primary key and returning columns
40
- ids, results = split_ids_and_results(returned_values, columns, options)
44
+ ids, results, options[:returning_columns] = split_ids_and_results(returned_values, options)
41
45
  end
42
46
 
43
47
  ActiveRecord::Import::Result.new([], number_of_inserts, ids, results)
44
48
  end
45
49
 
46
- def split_ids_and_results(values, columns, options)
50
+ def split_ids_and_results( selections, options )
47
51
  ids = []
48
- results = []
52
+ returning_values = []
53
+
54
+ columns = Array(selections[:columns])
55
+ values = Array(selections[:values])
49
56
  id_indexes = Array(options[:primary_key]).map { |key| columns.index(key) }
50
- returning_indexes = Array(options[:returning]).map { |key| columns.index(key) }
57
+ returning_columns = columns.reject.with_index { |_, index| id_indexes.include?(index) }
58
+ returning_indexes = returning_columns.map { |column| columns.index(column) }
51
59
 
52
60
  values.each do |value|
53
61
  value_array = Array(value)
54
- ids << id_indexes.map { |i| value_array[i] }
55
- results << returning_indexes.map { |i| value_array[i] }
62
+ ids << id_indexes.map { |index| value_array[index] }
63
+ returning_values << returning_indexes.map { |index| value_array[index] }
56
64
  end
57
65
 
58
66
  ids.map!(&:first) if id_indexes.size == 1
59
- results.map!(&:first) if returning_indexes.size == 1
67
+ returning_values.map!(&:first) if returning_columns.size == 1
60
68
 
61
- [ids, results]
69
+ [ids, returning_values, returning_columns]
62
70
  end
63
71
 
64
72
  def next_value_for_sequence(sequence_name)
@@ -79,19 +87,24 @@ module ActiveRecord::Import::PostgreSQLAdapter
79
87
 
80
88
  sql += super(table_name, options)
81
89
 
82
- columns = returning_columns(options)
83
- unless columns.blank? || (options[:no_returning] && !options[:recursive])
84
- sql << " RETURNING \"#{columns.join('", "')}\""
90
+ selections = returning_selections(options)
91
+ unless selections.blank? || (options[:no_returning] && !options[:recursive])
92
+ sql << " RETURNING #{selections.join(', ')}"
85
93
  end
86
94
 
87
95
  sql
88
96
  end
89
97
 
90
- def returning_columns(options)
91
- columns = []
92
- columns += Array(options[:primary_key]) if options[:primary_key].present?
93
- columns |= Array(options[:returning]) if options[:returning].present?
94
- columns
98
+ def returning_selections(options)
99
+ selections = []
100
+ column_names = Array(options[:model].column_names)
101
+
102
+ selections += Array(options[:primary_key]) if options[:primary_key].present?
103
+ selections += Array(options[:returning]) if options[:returning].present?
104
+
105
+ selections.map do |selection|
106
+ column_names.include?(selection.to_s) ? "\"#{selection}\"" : selection
107
+ end
95
108
  end
96
109
 
97
110
  # Add a column to be updated on duplicate key update
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord::Import::SQLite3Adapter
2
4
  include ActiveRecord::Import::ImportSupport
3
5
  include ActiveRecord::Import::OnDuplicateKeyUpdateSupport
4
6
 
5
- MIN_VERSION_FOR_IMPORT = "3.7.11".freeze
6
- MIN_VERSION_FOR_UPSERT = "3.24.0".freeze
7
+ MIN_VERSION_FOR_IMPORT = "3.7.11"
8
+ MIN_VERSION_FOR_UPSERT = "3.24.0"
7
9
  SQLITE_LIMIT_COMPOUND_SELECT = 500
8
10
 
9
11
  # Override our conformance to ActiveRecord::Import::ImportSupport interface
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "pathname"
2
4
  require "active_record"
3
5
  require "active_record/version"
4
6
 
5
7
  module ActiveRecord::Import
6
- ADAPTER_PATH = "activerecord-import/active_record/adapters".freeze
8
+ ADAPTER_PATH = "activerecord-import/active_record/adapters"
7
9
 
8
10
  def self.base_adapter(adapter)
9
11
  case adapter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "ostruct"
2
4
 
3
5
  module ActiveRecord::Import::ConnectionAdapters; end
@@ -55,7 +57,7 @@ module ActiveRecord::Import #:nodoc:
55
57
  end
56
58
  end
57
59
 
58
- filter.instance_variable_set(:@attributes, attrs)
60
+ filter.instance_variable_set(:@attributes, attrs.flatten)
59
61
 
60
62
  if @validate_callbacks.respond_to?(:chain, true)
61
63
  @validate_callbacks.send(:chain).tap do |chain|
@@ -547,7 +549,7 @@ class ActiveRecord::Base
547
549
  alias import! bulk_import! unless ActiveRecord::Base.respond_to? :import!
548
550
 
549
551
  def import_helper( *args )
550
- options = { validate: true, timestamps: true, track_validation_failures: false }
552
+ options = { model: self, validate: true, timestamps: true, track_validation_failures: false }
551
553
  options.merge!( args.pop ) if args.last.is_a? Hash
552
554
  # making sure that current model's primary key is used
553
555
  options[:primary_key] = primary_key
@@ -875,19 +877,28 @@ class ActiveRecord::Base
875
877
  end
876
878
  end
877
879
 
878
- if models.size == import_result.results.size
879
- columns = Array(options[:returning])
880
- single_column = "#{columns.first}=" if columns.size == 1
881
- import_result.results.each_with_index do |result, index|
880
+ set_value = lambda do |model, column, value|
881
+ val = deserialize_value.call(column, value)
882
+ if model.attribute_names.include?(column)
883
+ model.send("#{column}=", val)
884
+ else
885
+ attributes = attributes_builder.build_from_database(model.attributes.merge(column => val))
886
+ model.instance_variable_set(:@attributes, attributes)
887
+ end
888
+ end
889
+
890
+ columns = Array(options[:returning_columns])
891
+ results = Array(import_result.results)
892
+ if models.size == results.size
893
+ single_column = columns.first if columns.size == 1
894
+ results.each_with_index do |result, index|
882
895
  model = models[index]
883
896
 
884
897
  if single_column
885
- val = deserialize_value.call(columns.first, result)
886
- model.send(single_column, val)
898
+ set_value.call(model, single_column, result)
887
899
  else
888
900
  columns.each_with_index do |column, col_index|
889
- val = deserialize_value.call(column, result[col_index])
890
- model.send("#{column}=", val)
901
+ set_value.call(model, column, result[col_index])
891
902
  end
892
903
  end
893
904
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  warn <<-MSG
2
4
  [DEPRECATION] loading activerecord-import via 'require "activerecord-import/<adapter-name>"'
3
5
  is deprecated. Update to autorequire using 'require "activerecord-import"'. See
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  warn <<-MSG
2
4
  [DEPRECATION] loading activerecord-import via 'require "activerecord-import/<adapter-name>"'
3
5
  is deprecated. Update to autorequire using 'require "activerecord-import"'. See
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  warn <<-MSG
2
4
  [DEPRECATION] loading activerecord-import via 'require "activerecord-import/<adapter-name>"'
3
5
  is deprecated. Update to autorequire using 'require "activerecord-import"'. See
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord # :nodoc:
2
4
  class Base # :nodoc:
3
5
  # Synchronizes the passed in ActiveRecord instances with data