activerecord-insert_many 0.4.0 → 0.4.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ccd2651adecf544c3a1114e179191335e72f9b66
4
- data.tar.gz: 26019103155000fc02819745814b203512f4c19c
3
+ metadata.gz: 6dbaae41683d3696dc009ab77f2336300b575aca
4
+ data.tar.gz: 5e27992b665f5eabba30d6c4a78142c26ae38ffb
5
5
  SHA512:
6
- metadata.gz: a63e7b85373bc4833138af076a2246ccbf8758544b40aa8b82e534706c595500550c5753ff976584590c782ff37052520de6d9c92293700ecf417d276e5b35a9
7
- data.tar.gz: 6cc7d788ca3b631f9a73ffe479624ac1b06873885b3609afba3c367df94ce0c715e88c7903b3b5330712d56bb3b4199242ef5ff775a8038fcceaff7180432387
6
+ metadata.gz: 71cfbc32243757c8a395c3db1728fd850fc342b91e7cf87c151891be13b61cad1ec705e5c8d4275d38724b52fb1e82749a6f46af8493650147838c18c93185e0
7
+ data.tar.gz: 568d4744662f878b7edea647faad93878b7cb0affd753030fd134bd54c60046f49055e8abf85c7c59ecc8cbd7ce4438a95c030c88c673625719470d34154df90
@@ -21,10 +21,20 @@ module ActiveRecord
21
21
  end
22
22
  return returning && [] if fixtures.empty?
23
23
 
24
+ sql = insert_many_sql(fixtures, table_name, options.merge(returning: returning))
25
+
26
+ result = execute sql, "Fixture Insert"
27
+
28
+ returning ? result.to_a : result
29
+ end
30
+
31
+ def insert_many_sql(fixtures, table_name, options={})
32
+ returning = options[:returning]
33
+
24
34
  columns = schema_cache.columns_hash(table_name)
25
35
 
26
36
  sample = fixtures.first
27
- key_list = sample.map { |name, value| quote_column_name(name) }
37
+ key_list = sample.map { |name, _value| quote_column_name(name) }
28
38
  returning = returning.map { |name| quote_column_name(name) } if returning
29
39
 
30
40
  value_lists = fixtures.map do |fixture|
@@ -39,7 +49,7 @@ module ActiveRecord
39
49
  end
40
50
  end
41
51
 
42
- prepare_binds_for_database(binds).map do |value|
52
+ binds.map(&:value_for_database).map do |value|
43
53
  begin
44
54
  quote(value)
45
55
  rescue TypeError
@@ -48,20 +58,20 @@ module ActiveRecord
48
58
  end
49
59
  end
50
60
 
51
- sql = "INSERT INTO #{quote_table_name(table_name)} (#{key_list.join(', ')}) VALUES #{value_lists.map { |value| "(#{value.join(', ')})" }.join(",")}"
61
+ sql = "INSERT INTO #{quote_table_name(table_name)} (#{key_list.join(",")}) VALUES #{value_lists.map { |value| "(#{value.join(",")})" }.join(", ")}"
52
62
 
53
63
  if conflict = options[:on_conflict]
54
64
  raise ArgumentError, "To use the :on_conflict option, you must be using Postgres >= 9.5" unless supports_on_conflict?
55
65
 
56
- conflict_column = conflict.fetch(:column, schema_cache.primary_keys(table_name))
57
- raise ArgumentError, "To use the :on_conflict option, you must specify :column" unless conflict_column
66
+ conflict_columns = Array.wrap(conflict.fetch(:column, schema_cache.primary_keys(table_name)))
67
+ raise ArgumentError, "To use the :on_conflict option, you must specify :column" unless conflict_columns.any?
58
68
 
59
- conflict_column = quote_column_name(conflict_column)
69
+ conflict_columns = conflict_columns.map(&method(:quote_column_name))
60
70
  case conflict_do = conflict.fetch(:do)
61
71
  when :nothing
62
- sql << " ON CONFLICT(#{conflict_column}) DO NOTHING"
72
+ sql << " ON CONFLICT(#{conflict_columns.join(",")}) DO NOTHING"
63
73
  when :update
64
- sql << " ON CONFLICT(#{conflict_column}) DO UPDATE SET #{(key_list - [conflict_column]).map { |key| "#{key} = excluded.#{key}" }.join(", ")}"
74
+ sql << " ON CONFLICT(#{conflict_columns.join(",")}) DO UPDATE SET #{(key_list - conflict_columns).map { |key| "#{key} = excluded.#{key}" }.join(", ")}"
65
75
  else
66
76
  raise ArgumentError, "#{conflict_do.inspect} is an unknown value for conflict[:do]; must be :nothing or :update"
67
77
  end
@@ -69,9 +79,7 @@ module ActiveRecord
69
79
 
70
80
  sql << " RETURNING #{returning.join(",")}" if returning
71
81
 
72
- result = execute sql, "Fixture Insert"
73
-
74
- returning ? result.to_a : result
82
+ sql
75
83
  end
76
84
 
77
85
  def supports_on_conflict?
@@ -1,5 +1,5 @@
1
1
  module Activerecord
2
2
  module InsertMany
3
- VERSION = "0.4.0"
3
+ VERSION = "0.4.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-insert_many
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Booth
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-10-17 00:00:00.000000000 Z
12
+ date: 2017-06-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -174,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  version: '0'
175
175
  requirements: []
176
176
  rubyforge_project:
177
- rubygems_version: 2.5.1
177
+ rubygems_version: 2.6.11
178
178
  signing_key:
179
179
  specification_version: 4
180
180
  summary: Adds a method for bulk-inserted records using ActiveRecord