activerecord-insert_many 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/activerecord/insert_many.rb +19 -11
- data/lib/activerecord/insert_many/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6dbaae41683d3696dc009ab77f2336300b575aca
|
4
|
+
data.tar.gz: 5e27992b665f5eabba30d6c4a78142c26ae38ffb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,
|
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
|
-
|
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(
|
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
|
-
|
57
|
-
raise ArgumentError, "To use the :on_conflict option, you must specify :column" unless
|
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
|
-
|
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(#{
|
72
|
+
sql << " ON CONFLICT(#{conflict_columns.join(",")}) DO NOTHING"
|
63
73
|
when :update
|
64
|
-
sql << " ON CONFLICT(#{
|
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
|
-
|
73
|
-
|
74
|
-
returning ? result.to_a : result
|
82
|
+
sql
|
75
83
|
end
|
76
84
|
|
77
85
|
def supports_on_conflict?
|
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.
|
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:
|
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.
|
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
|