active_record-pg_generate_series 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +210 -0
- data/Rakefile +6 -0
- data/active_record-pg_generate_series.gemspec +36 -0
- data/bin/benchmark.rb +40 -0
- data/bin/console +10 -0
- data/bin/db_setup.rb +21 -0
- data/bin/setup +10 -0
- data/connect_db.rb +8 -0
- data/database.yml +8 -0
- data/docker-compose.yml +9 -0
- data/lib/active_record/pg_generate_series.rb +5 -0
- data/lib/active_record/pg_generate_series/extension.rb +29 -0
- data/lib/active_record/pg_generate_series/sql_builder.rb +51 -0
- data/lib/active_record/pg_generate_series/version.rb +5 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3abc147b5395d80b72b6497be70369969188c48c
|
4
|
+
data.tar.gz: 9b50bd5f16b9c04f34a56de544a7cb4b67881cfa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1b0ec2c257d313850c9e7ae043c7cb16ee49caabb3c6040b36f47e886dfb8556ee9cdd39be2f1b4a1cf958ff29d1425682571543c4bb92a2378808200975cb5e
|
7
|
+
data.tar.gz: 566671d5f909c9e25e0b538ddbdce6edcbe086612b7b2d38cf4cae9c7fb4d214e5cbe238712db3040f45b290380062aa0961a0cc6543b0eaadfde4b8657de45e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at dev.ryu39@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 ryu39
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,210 @@
|
|
1
|
+
# ActiveRecord::PgGenerateSeries
|
2
|
+
|
3
|
+
[](https://travis-ci.org/ryu39/active_record-pg_generate_series)
|
4
|
+
|
5
|
+
This gem adds a feature which inserts records using PostgreSQL generate_series function to ActiveRecord.
|
6
|
+
|
7
|
+
Insertion using generate_series funciton is very fast.
|
8
|
+
It is about 300-400 times faster than iteration of ActiveRecord::Base#save and 30-40 times faster than bulk insert. (In authors env)
|
9
|
+
|
10
|
+
## Benchmark
|
11
|
+
|
12
|
+
I compared to iteration of ActiveRecord::Base#save(without validation) and
|
13
|
+
bulk insertion using [activerecord-import](https://github.com/zdennis/activerecord-import).
|
14
|
+
|
15
|
+
The average(3 times) of inserting 10,000 records is shown as follows.
|
16
|
+
This is measured in author's PC(MacBook Pro Retina 13-inch Early 2015).
|
17
|
+
|
18
|
+
| Target | Time(sec) |
|
19
|
+
|:---------------------------------|----------:|
|
20
|
+
| ActiveRecord#save | 37.442 |
|
21
|
+
| activerecord-import | 3.149 |
|
22
|
+
| active_record-pg_generate_series | 0.092 |
|
23
|
+
|
24
|
+
You can run benchmark with following commands.
|
25
|
+
|
26
|
+
$ docker-compose up -d
|
27
|
+
$ ./bin/setup
|
28
|
+
$ ruby bin/benchmark.rb
|
29
|
+
|
30
|
+
## Installation
|
31
|
+
|
32
|
+
Add this line to your application's Gemfile:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
gem 'active_record-pg_generate_series'
|
36
|
+
```
|
37
|
+
|
38
|
+
And then execute:
|
39
|
+
|
40
|
+
$ bundle
|
41
|
+
|
42
|
+
Or install it yourself as:
|
43
|
+
|
44
|
+
$ gem install active_record-pg_generate_series
|
45
|
+
|
46
|
+
## Usage
|
47
|
+
|
48
|
+
You can insert records by calling `ActiveRecord::Base.insert_using_generate_series`.
|
49
|
+
This function requires 3 parameters, `first`, `last` and block.
|
50
|
+
The parameters `first` and `last` are passed to PostgreSQL GENERATE_SERIES function.
|
51
|
+
The block specifies values of insert records.
|
52
|
+
|
53
|
+
Please see this example.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
# User is a subclass of ActiveRecord::Base
|
57
|
+
User.insert_using_generate_series(1, 10) do |sql|
|
58
|
+
sql.name = 'username'
|
59
|
+
sql.age = 16
|
60
|
+
sql.birth_date = Date.new(2000, 1, 1)
|
61
|
+
end
|
62
|
+
|
63
|
+
p User.all
|
64
|
+
# => #<ActiveRecord::Relation
|
65
|
+
# [#<User id: 1, type: nil, name: "username", age: 16, birth_date: "2000-01-01", disabled: false, created_at: "2016-11-23 08:52:25", updated_at: "2016-11-23 08:52:25">,
|
66
|
+
# #<User id: 2, type: nil, name: "username", age: 16, birth_date: "2000-01-01", disabled: false, created_at: "2016-11-23 08:52:25", updated_at: "2016-11-23 08:52:25">,
|
67
|
+
# :
|
68
|
+
# #<User id: 10, type: nil, name: "username", age: 16, birth_date: "2000-01-01", disabled: false, created_at: "2016-11-23 08:52:25", updated_at: "2016-11-23 08:52:25">]>
|
69
|
+
```
|
70
|
+
|
71
|
+
Note that `created_at` and `updated_at` are set automatically.
|
72
|
+
You can overwrite these values in block.
|
73
|
+
|
74
|
+
You can also use sequence value generated by GENERATE_SERIES function using `#raw` method with `seq` in sql block.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
User.insert_using_generate_series(1, 10) do |sql|
|
78
|
+
sql.name = raw("'username' || seq")
|
79
|
+
sql.age = raw("seq")
|
80
|
+
sql.birth_date = raw("'1999-12-31'::date + seq")
|
81
|
+
sql.disabled = raw("CASE seq % 2 WHEN 0 THEN true ELSE false END")
|
82
|
+
end
|
83
|
+
|
84
|
+
p User.all
|
85
|
+
# => #<ActiveRecord::Relation
|
86
|
+
# [#<User id: 11, type: nil, name: "username1", age: 1, birth_date: "2000-01-01", disabled: false, created_at: "2016-11-23 09:03:12", updated_at: "2016-11-23 09:03:12">,
|
87
|
+
# #<User id: 12, type: nil, name: "username2", age: 2, birth_date: "2000-01-02", disabled: true, created_at: "2016-11-23 09:03:12", updated_at: "2016-11-23 09:03:12">,
|
88
|
+
# :
|
89
|
+
# #<User id: 20, type: nil, name: "username10", age: 10, birth_date: "2000-01-10", disabled: true, created_at: "2016-11-23 09:03:12", updated_at: "2016-11-23 09:03:12">]>
|
90
|
+
```
|
91
|
+
|
92
|
+
When you use `#raw` method, please take care of sql injection because `#raw` method does not sanitize given string.
|
93
|
+
|
94
|
+
### STI support
|
95
|
+
|
96
|
+
When target is STI subclass, the type value(default is `type` column) is set automatically.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
# AdminUser is a subclass of User.
|
100
|
+
AdminUser.insert_using_generate_series(1, 10) do |sql|
|
101
|
+
sql.name = 'admin username'
|
102
|
+
sql.age = 16
|
103
|
+
sql.birth_date = Date.new(2000, 1, 1)
|
104
|
+
end
|
105
|
+
|
106
|
+
p AdminUser.all
|
107
|
+
# => #<ActiveRecord::Relation
|
108
|
+
# [#<AdminUser id: 21, type: "AdminUser", name: "admin username", age: 16, birth_date: "2000-01-01", disabled: false, created_at: "2016-11-23 09:17:22", updated_at: "2016-11-23 09:17:22">, #<AdminUser id: 22, type: "AdminUser", name: "admin username", age: 16, birth_date: "2000-01-01", disabled: false, created_at: "2016-11-23 09:17:22", updated_at: "2016-11-23 09:17:22">, 21, type: "AdminUser", name: "admin username", age: 16, birth_date: "2000-01-01", disabled: false, created_at: "2016-11-23 09:17:22", updated_at: "2016-11-23 09:17:22">,
|
109
|
+
# #<AdminUser id: 22, type: "AdminUser", name: "admin username", age: 16, birth_date: "2000-01-01", disabled: false, created_at: "2016-11-23 09:17:22", updated_at: "2016-11-23 09:17:22">,
|
110
|
+
# :
|
111
|
+
# #<AdminUser id: 30, type: "AdminUser", name: "admin username", age: 16, birth_date: "2000-01-01", disabled: false, created_at: "2016-11-23 09:17:22", updated_at: "2016-11-23 09:17:22">]>
|
112
|
+
```
|
113
|
+
|
114
|
+
### Options
|
115
|
+
|
116
|
+
#### Step
|
117
|
+
|
118
|
+
You can change step value of GENERATE_SERISE function (Default: 1) with `step` option.
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
User.insert_using_generate_series(1, 10, step: 5) do |sql|
|
122
|
+
sql.name = raw("'username' || seq")
|
123
|
+
sql.age = raw("seq")
|
124
|
+
sql.birth_date = raw("'1999-12-31'::date + seq")
|
125
|
+
sql.disabled = raw("CASE seq % 2 WHEN 0 THEN true ELSE false END")
|
126
|
+
end
|
127
|
+
|
128
|
+
p User.all
|
129
|
+
# => #<ActiveRecord::Relation
|
130
|
+
# [#<User id: 31, type: nil, name: "username1", age: 1, birth_date: "2000-01-01", disabled: false, created_at: "2016-11-23 10:00:47", updated_at: "2016-11-23 10:00:47">,
|
131
|
+
# #<User id: 32, type: nil, name: "username6", age: 6, birth_date: "2000-01-06", disabled: true, created_at: "2016-11-23 10:00:47", updated_at: "2016-11-23 10:00:47">]>
|
132
|
+
```
|
133
|
+
|
134
|
+
#### Sequence name
|
135
|
+
|
136
|
+
Also, you can change sequence name of GENERATE_SERISE function (Default: `seq`) with `seq_name` option.
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
User.insert_using_generate_series(1, 10, seq_name: 'new_seq') do |sql|
|
140
|
+
sql.name = raw("'username' || new_seq")
|
141
|
+
sql.age = raw("new_seq")
|
142
|
+
sql.birth_date = raw("'1999-12-31'::date + new_seq")
|
143
|
+
sql.disabled = raw("CASE new_seq % 2 WHEN 0 THEN true ELSE false END")
|
144
|
+
end
|
145
|
+
|
146
|
+
p User.all
|
147
|
+
# => #<ActiveRecord::Relation
|
148
|
+
# [#<User id: 33, type: nil, name: "username1", age: 1, birth_date: "2000-01-01", disabled: false, created_at: "2016-11-23 10:26:30", updated_at: "2016-11-23 10:26:30">,
|
149
|
+
# #<User id: 34, type: nil, name: "username2", age: 2, birth_date: "2000-01-02", disabled: true, created_at: "2016-11-23 10:26:30", updated_at: "2016-11-23 10:26:30">,
|
150
|
+
# #<User id: 35, type: nil, name: "username3", age: 3, birth_date: "2000-01-03", disabled: false, created_at: "2016-11-23 10:26:30", updated_at: "2016-11-23 10:26:30">,
|
151
|
+
# #<User id: 36, type: nil, name: "username4", age: 4, birth_date: "2000-01-04", disabled: true, created_at: "2016-11-23 10:26:30", updated_at: "2016-11-23 10:26:30">,
|
152
|
+
# #<User id: 37, type: nil, name: "username5", age: 5, birth_date: "2000-01-05", disabled: false, created_at: "2016-11-23 10:26:30", updated_at: "2016-11-23 10:26:30">,
|
153
|
+
# #<User id: 38, type: nil, name: "username6", age: 6, birth_date: "2000-01-06", disabled: true, created_at: "2016-11-23 10:26:30", updated_at: "2016-11-23 10:26:30">,
|
154
|
+
# #<User id: 39, type: nil, name: "username7", age: 7, birth_date: "2000-01-07", disabled: false, created_at: "2016-11-23 10:26:30", updated_at: "2016-11-23 10:26:30">,
|
155
|
+
# #<User id: 40, type: nil, name: "username8", age: 8, birth_date: "2000-01-08", disabled: true, created_at: "2016-11-23 10:26:30", updated_at: "2016-11-23 10:26:30">,
|
156
|
+
# #<User id: 41, type: nil, name: "username9", age: 9, birth_date: "2000-01-09", disabled: false, created_at: "2016-11-23 10:26:30", updated_at: "2016-11-23 10:26:30">,
|
157
|
+
# #<User id: 42, type: nil, name: "username10", age: 10, birth_date: "2000-01-10", disabled: true, created_at: "2016-11-23 10:26:30", updated_at: "2016-11-23 10:26:30">]>
|
158
|
+
```
|
159
|
+
|
160
|
+
#### Debug
|
161
|
+
|
162
|
+
When `insert_using_generate_series` is called with `debug` option, it does not execute sql but returns sql to be executed.
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
sql = User.insert_using_generate_series(1, 10, debug: true) do |sql|
|
166
|
+
sql.name = raw("'username' || seq")
|
167
|
+
sql.age = raw("seq")
|
168
|
+
sql.birth_date = raw("'1999-12-31'::date + seq")
|
169
|
+
sql.disabled = raw("CASE seq % 2 WHEN 0 THEN true ELSE false END")
|
170
|
+
end
|
171
|
+
|
172
|
+
puts sql
|
173
|
+
# => INSERT INTO
|
174
|
+
# "users" ("created_at","updated_at","name","age","birth_date","disabled")
|
175
|
+
# SELECT
|
176
|
+
# '2016-11-23 09:32:29.750549',
|
177
|
+
# '2016-11-23 09:32:29.750549',
|
178
|
+
# 'username' || seq,
|
179
|
+
# seq,
|
180
|
+
# '1999-12-31'::date + seq,
|
181
|
+
# CASE seq % 2 WHEN 0 THEN true ELSE false END
|
182
|
+
# FROM
|
183
|
+
# GENERATE_SERIES(1, 10, 1) AS "seq"
|
184
|
+
# ;
|
185
|
+
```
|
186
|
+
|
187
|
+
## Development
|
188
|
+
|
189
|
+
First docker and docker-compose is required.
|
190
|
+
|
191
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
192
|
+
Then, run `rake spec` to run the tests.
|
193
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
194
|
+
|
195
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
196
|
+
To release a new version, update the version number in `version.rb`,
|
197
|
+
and then run `bundle exec rake release`, which will create a git tag for the version,
|
198
|
+
push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
199
|
+
|
200
|
+
## Contributing
|
201
|
+
|
202
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ryu39/active_record-pg_generate_series.
|
203
|
+
This project is intended to be a safe, welcoming space for collaboration,
|
204
|
+
and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
205
|
+
|
206
|
+
|
207
|
+
## License
|
208
|
+
|
209
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
210
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_record/pg_generate_series/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'active_record-pg_generate_series'
|
8
|
+
spec.version = ActiveRecord::PgGenerateSeries::VERSION
|
9
|
+
spec.authors = ['ryu39']
|
10
|
+
spec.email = ['dev.ryu39@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Add a feature which inserts records using PostgreSQL generate_series function to ActiveRecord}
|
13
|
+
spec.description = <<EOS.gsub("\n", ' ')
|
14
|
+
This gem adds a feature which inserts records using PostgreSQL generate_series function to ActiveRecord.
|
15
|
+
Insertion using generate_series function is very fast, so it is useful
|
16
|
+
when you want to insert simple but many and many record, e.g. prepare for performance test.
|
17
|
+
EOS
|
18
|
+
spec.homepage = 'https://github.com/ryu39/active_record-pg_generate_series'
|
19
|
+
spec.license = 'MIT'
|
20
|
+
|
21
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
22
|
+
f.match(%r{^(test|spec|features)/})
|
23
|
+
end
|
24
|
+
spec.bindir = 'exe'
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.add_runtime_dependency 'activerecord'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
31
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
32
|
+
spec.add_development_dependency 'rspec', '>= 3.5'
|
33
|
+
spec.add_development_dependency 'pry'
|
34
|
+
spec.add_development_dependency 'pg'
|
35
|
+
spec.add_development_dependency 'activerecord-import'
|
36
|
+
end
|
data/bin/benchmark.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'active_record'
|
3
|
+
require 'activerecord-import'
|
4
|
+
require File.expand_path('../../connect_db.rb', __FILE__)
|
5
|
+
require File.expand_path('../../spec/models/user.rb', __FILE__)
|
6
|
+
require 'active_record/pg_generate_series'
|
7
|
+
|
8
|
+
require 'benchmark'
|
9
|
+
|
10
|
+
RECORD_NUM = 10_000
|
11
|
+
|
12
|
+
User.delete_all
|
13
|
+
|
14
|
+
GC.disable
|
15
|
+
Benchmark.bm(36) do |x|
|
16
|
+
x.report('iteration of ActiveRecord::Base#save') do
|
17
|
+
RECORD_NUM.times do |i|
|
18
|
+
user = User.new(name: "name#{i + 1}", age: i + 1, birth_date: Date.today + i + 1)
|
19
|
+
user.save(validate: false)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
x.report('bulk insert(activerecord-import)') do
|
24
|
+
users = RECORD_NUM.times.map do |i|
|
25
|
+
User.new(name: "name#{i + 1}", age: i + 1, birth_date: Date.today + i + 1)
|
26
|
+
end
|
27
|
+
User.import(users)
|
28
|
+
end
|
29
|
+
|
30
|
+
x.report('active_record-pg_generate_series') do
|
31
|
+
User.insert_using_generate_series(1, RECORD_NUM) do |sql|
|
32
|
+
sql.name = raw("'name' || seq")
|
33
|
+
sql.age = raw('seq')
|
34
|
+
sql.birth_date = raw("'2000-01-01'::date + seq")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
GC.enable
|
39
|
+
|
40
|
+
User.delete_all
|
data/bin/console
ADDED
data/bin/db_setup.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'yaml'
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
config_file = File.expand_path('../../database.yml', __FILE__)
|
6
|
+
config = YAML::load(ERB.new(IO.read(config_file)).result)['db']
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres'))
|
9
|
+
ActiveRecord::Base.connection.drop_database(config['database'])
|
10
|
+
ActiveRecord::Base.connection.create_database(config['database'])
|
11
|
+
ActiveRecord::Base.establish_connection(config)
|
12
|
+
|
13
|
+
ActiveRecord::Base.connection.create_table :users do |t|
|
14
|
+
t.string :type
|
15
|
+
t.string :name, null: false
|
16
|
+
t.integer :age, null: false
|
17
|
+
t.date :birth_date, null: false
|
18
|
+
t.boolean :disabled, null: false, default: false
|
19
|
+
|
20
|
+
t.timestamps
|
21
|
+
end
|
data/bin/setup
ADDED
data/connect_db.rb
ADDED
data/database.yml
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
db:
|
2
|
+
adapter: postgresql
|
3
|
+
encoding: utf8
|
4
|
+
database: pg_generate_series
|
5
|
+
pool: 5
|
6
|
+
username: "<%= ENV.fetch('POSTGRES_USER_NAME', 'postgres') %>"
|
7
|
+
password: "<%= ENV.fetch('POSTGRES_PASSWORD', 'postgres') %>"
|
8
|
+
host: "<%= ENV.fetch('POSTGRES_HOST', /\Atcp:\/\/([0-9.]+):/.match(ENV['DOCKER_HOST']) ? $1 : 'localhost') %>"
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'active_record/pg_generate_series/sql_builder'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module PgGenerateSeries
|
5
|
+
module Extension
|
6
|
+
# Execute INSERT SQL using GENERATE_SERIES function
|
7
|
+
#
|
8
|
+
# @param [Integer] first Required, first value of GENERATE_SERIES.
|
9
|
+
# @param [Integer] last Required, last value of GENERATE SERIES.
|
10
|
+
# @param [Integer] step Optional, step value of GENERATE_SERIES, default is 1.
|
11
|
+
# @param [String, Symbol] seq_name Optional, name of GENERATE_SERIES sequence, default is :seq.
|
12
|
+
# @param [boolean] debug Optional, if true then sql is returned, not executed.
|
13
|
+
# @param [Proc] block Required, block for setting selected columns.
|
14
|
+
def insert_using_generate_series(first, last, step: 1, seq_name: :seq, debug: false, &block)
|
15
|
+
builder = ActiveRecord::PgGenerateSeries::SqlBuilder.new(self, first, last, step, seq_name)
|
16
|
+
|
17
|
+
builder.send("#{inheritance_column}=", sti_name) unless descends_from_active_record?
|
18
|
+
Time.current.tap do |now|
|
19
|
+
builder.created_at = now if column_names.include?('created_at')
|
20
|
+
builder.updated_at = now if column_names.include?('updated_at')
|
21
|
+
end
|
22
|
+
builder.instance_exec(builder, &block)
|
23
|
+
|
24
|
+
sql = builder.to_sql
|
25
|
+
debug ? sql : connection.execute(sql)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module PgGenerateSeries
|
5
|
+
class SqlBuilder
|
6
|
+
extend Forwardable
|
7
|
+
def_delegators :@ar_class, :connection, :sanitize, :quoted_table_name
|
8
|
+
|
9
|
+
def initialize(ar_class, first, last, step, seq_name)
|
10
|
+
@ar_class = ar_class
|
11
|
+
@first = first
|
12
|
+
@last = last
|
13
|
+
@step = step
|
14
|
+
@seq_name = seq_name
|
15
|
+
@select_items = {}
|
16
|
+
|
17
|
+
ar_class.column_names.each do |col|
|
18
|
+
define_singleton_method "#{col}=" do |val|
|
19
|
+
@select_items[col] = val
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_sql
|
25
|
+
<<EOS
|
26
|
+
INSERT INTO
|
27
|
+
#{quoted_table_name} (#{@select_items.keys.map { |col| connection.quote_column_name(col) }.join(',')})
|
28
|
+
SELECT
|
29
|
+
#{@select_items.map { |_, val| "#{val.is_a?(Raw) ? val.str : sanitize(val)}" }.join(",\n ")}
|
30
|
+
FROM
|
31
|
+
GENERATE_SERIES(#{@first.to_i}, #{@last.to_i}, #{@step.to_i}) AS #{connection.quote_column_name(@seq_name)}
|
32
|
+
;
|
33
|
+
EOS
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
class Raw
|
39
|
+
attr_reader :str
|
40
|
+
|
41
|
+
def initialize(str)
|
42
|
+
@str = str
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def raw(str)
|
47
|
+
Raw.new(str)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record-pg_generate_series
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ryu39
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.5'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pg
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activerecord-import
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: 'This gem adds a feature which inserts records using PostgreSQL generate_series
|
112
|
+
function to ActiveRecord. Insertion using generate_series function is very fast,
|
113
|
+
so it is useful when you want to insert simple but many and many record, e.g. prepare
|
114
|
+
for performance test. '
|
115
|
+
email:
|
116
|
+
- dev.ryu39@gmail.com
|
117
|
+
executables: []
|
118
|
+
extensions: []
|
119
|
+
extra_rdoc_files: []
|
120
|
+
files:
|
121
|
+
- ".gitignore"
|
122
|
+
- ".rspec"
|
123
|
+
- ".travis.yml"
|
124
|
+
- CODE_OF_CONDUCT.md
|
125
|
+
- Gemfile
|
126
|
+
- LICENSE.txt
|
127
|
+
- README.md
|
128
|
+
- Rakefile
|
129
|
+
- active_record-pg_generate_series.gemspec
|
130
|
+
- bin/benchmark.rb
|
131
|
+
- bin/console
|
132
|
+
- bin/db_setup.rb
|
133
|
+
- bin/setup
|
134
|
+
- connect_db.rb
|
135
|
+
- database.yml
|
136
|
+
- docker-compose.yml
|
137
|
+
- lib/active_record/pg_generate_series.rb
|
138
|
+
- lib/active_record/pg_generate_series/extension.rb
|
139
|
+
- lib/active_record/pg_generate_series/sql_builder.rb
|
140
|
+
- lib/active_record/pg_generate_series/version.rb
|
141
|
+
homepage: https://github.com/ryu39/active_record-pg_generate_series
|
142
|
+
licenses:
|
143
|
+
- MIT
|
144
|
+
metadata: {}
|
145
|
+
post_install_message:
|
146
|
+
rdoc_options: []
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
requirements: []
|
160
|
+
rubyforge_project:
|
161
|
+
rubygems_version: 2.6.7
|
162
|
+
signing_key:
|
163
|
+
specification_version: 4
|
164
|
+
summary: Add a feature which inserts records using PostgreSQL generate_series function
|
165
|
+
to ActiveRecord
|
166
|
+
test_files: []
|