activerecord-insert_many 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +15 -2
- data/README.md +6 -4
- data/Rakefile +7 -0
- data/activerecord-insert_many.gemspec +6 -0
- data/lib/activerecord/insert_many/version.rb +1 -1
- data/lib/activerecord/insert_many.rb +39 -7
- metadata +86 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ccd2651adecf544c3a1114e179191335e72f9b66
|
4
|
+
data.tar.gz: 26019103155000fc02819745814b203512f4c19c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a63e7b85373bc4833138af076a2246ccbf8758544b40aa8b82e534706c595500550c5753ff976584590c782ff37052520de6d9c92293700ecf417d276e5b35a9
|
7
|
+
data.tar.gz: 6cc7d788ca3b631f9a73ffe479624ac1b06873885b3609afba3c367df94ce0c715e88c7903b3b5330712d56bb3b4199242ef5ff775a8038fcceaff7180432387
|
data/.travis.yml
CHANGED
@@ -1,4 +1,17 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
- 2.1
|
4
|
-
|
3
|
+
- 2.3.1
|
4
|
+
|
5
|
+
# Use Postgres 9.5
|
6
|
+
# https://www.brandur.org/fragments/postgres-95-travis
|
7
|
+
dist: trusty
|
8
|
+
sudo: required
|
9
|
+
addons:
|
10
|
+
postgresql: "9.5"
|
11
|
+
|
12
|
+
before_install: gem update bundler
|
13
|
+
script: bundle exec rake test
|
14
|
+
|
15
|
+
# To stop Travis from running tests for a new commit,
|
16
|
+
# add the following to your commit message: [ci skip]
|
17
|
+
# You should add this when you edit documentation or comments, etc.
|
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
# Activerecord::InsertMany
|
2
2
|
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/activerecord/insert_many`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
3
|
|
5
|
-
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/activerecord-insert_many.svg)](https://rubygems.org/gems/activerecord-insert_many)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/cph/activerecord-insert_many.svg)](https://codeclimate.com/github/cph/activerecord-insert_many)
|
6
|
+
[![Build Status](https://travis-ci.org/cph/activerecord-insert_many.svg)](https://travis-ci.org/cph/activerecord-insert_many)
|
7
|
+
|
8
|
+
ActiveRecord::InsertMany adds the class method `insert_many` to ActiveRecord objects allowing you to bulk-insert records.
|
6
9
|
|
7
10
|
## Installation
|
8
11
|
|
@@ -32,5 +35,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
35
|
|
33
36
|
## Contributing
|
34
37
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
36
|
-
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cph/activerecord-insert_many.
|
data/Rakefile
CHANGED
@@ -21,4 +21,10 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.10"
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest-reporters"
|
25
|
+
spec.add_development_dependency "minitest-reporters-turn_reporter"
|
26
|
+
spec.add_development_dependency "pg"
|
27
|
+
spec.add_development_dependency "database_cleaner"
|
28
|
+
spec.add_development_dependency "shoulda-context"
|
29
|
+
spec.add_development_dependency "pry"
|
24
30
|
end
|
@@ -5,19 +5,27 @@ require "active_record/fixtures"
|
|
5
5
|
|
6
6
|
module ActiveRecord
|
7
7
|
module InsertMany
|
8
|
-
def insert_many(fixtures)
|
9
|
-
connection.insert_many(fixtures, table_name)
|
8
|
+
def insert_many(fixtures, options={})
|
9
|
+
connection.insert_many(fixtures, table_name, options)
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
module InsertManyStatement
|
14
|
-
def insert_many(fixtures, table_name=self.table_name)
|
15
|
-
|
14
|
+
def insert_many(fixtures, table_name=self.table_name, options={})
|
15
|
+
returning = options[:returning]
|
16
|
+
if returning
|
17
|
+
raise ArgumentError, "To use :returning, you must be using Postgres" unless supports_returning?
|
18
|
+
elsif supports_returning?
|
19
|
+
primary_keys = schema_cache.primary_keys(table_name)
|
20
|
+
returning = Array(primary_keys) if primary_keys
|
21
|
+
end
|
22
|
+
return returning && [] if fixtures.empty?
|
16
23
|
|
17
24
|
columns = schema_cache.columns_hash(table_name)
|
18
25
|
|
19
26
|
sample = fixtures.first
|
20
27
|
key_list = sample.map { |name, value| quote_column_name(name) }
|
28
|
+
returning = returning.map { |name| quote_column_name(name) } if returning
|
21
29
|
|
22
30
|
value_lists = fixtures.map do |fixture|
|
23
31
|
|
@@ -40,10 +48,34 @@ module ActiveRecord
|
|
40
48
|
end
|
41
49
|
end
|
42
50
|
|
43
|
-
|
44
|
-
|
51
|
+
sql = "INSERT INTO #{quote_table_name(table_name)} (#{key_list.join(', ')}) VALUES #{value_lists.map { |value| "(#{value.join(', ')})" }.join(",")}"
|
52
|
+
|
53
|
+
if conflict = options[:on_conflict]
|
54
|
+
raise ArgumentError, "To use the :on_conflict option, you must be using Postgres >= 9.5" unless supports_on_conflict?
|
55
|
+
|
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
|
58
|
+
|
59
|
+
conflict_column = quote_column_name(conflict_column)
|
60
|
+
case conflict_do = conflict.fetch(:do)
|
61
|
+
when :nothing
|
62
|
+
sql << " ON CONFLICT(#{conflict_column}) DO NOTHING"
|
63
|
+
when :update
|
64
|
+
sql << " ON CONFLICT(#{conflict_column}) DO UPDATE SET #{(key_list - [conflict_column]).map { |key| "#{key} = excluded.#{key}" }.join(", ")}"
|
65
|
+
else
|
66
|
+
raise ArgumentError, "#{conflict_do.inspect} is an unknown value for conflict[:do]; must be :nothing or :update"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
sql << " RETURNING #{returning.join(",")}" if returning
|
71
|
+
|
72
|
+
result = execute sql, "Fixture Insert"
|
73
|
+
|
74
|
+
returning ? result.to_a : result
|
75
|
+
end
|
45
76
|
|
46
|
-
|
77
|
+
def supports_on_conflict?
|
78
|
+
self.class.name == "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter" && postgresql_version >= 90500
|
47
79
|
end
|
48
80
|
|
49
81
|
def supports_returning?
|
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
|
+
version: 0.4.0
|
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-
|
12
|
+
date: 2016-10-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -53,6 +53,90 @@ dependencies:
|
|
53
53
|
- - "~>"
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '10.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: minitest-reporters
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: minitest-reporters-turn_reporter
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: pg
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: database_cleaner
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: shoulda-context
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: pry
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
type: :development
|
134
|
+
prerelease: false
|
135
|
+
version_requirements: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
56
140
|
description:
|
57
141
|
email:
|
58
142
|
- luke.booth@cph.org
|