active_record-pg_generate_series 0.1.2 → 0.1.3

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
- SHA1:
3
- metadata.gz: d9174923b9800d2bbe387a990d821623dd588fb2
4
- data.tar.gz: cf2171a8c525c9cf5bc0034e8572025037b755fc
2
+ SHA256:
3
+ metadata.gz: 6ccc06b80e89caec144b1fb367890a3ec144a05d970f98353bdd6cfd6572b3c4
4
+ data.tar.gz: 744ffc5269f8eae834e74b9b648c8e30c143297ff159d7101028c320bb6e37bc
5
5
  SHA512:
6
- metadata.gz: ddc6aee50a0941fd1eaadb7e0920df4093743cbe253f451c2cab733f05214989ba9cebaac830e630a473f28fbb51fa935e613848e3a1ab4e786408de21486b8d
7
- data.tar.gz: e8101995e0f940943e0f5993c46e88cb5e4516c48c8eef26c97322cf40b35da60961cab0026d6b4f579b7412c198e259b384abe1b35d30ca449f445c0a5a37a5
6
+ metadata.gz: 9c06c3a30b09429c4bc9f7d9e9b385cb62d56c0c558737c2e0b24d5f634d619bd23fcb829b5fa944a0ffeedc90584146abe79e36a4b5df7188d40a7efcd31f49
7
+ data.tar.gz: 19b2c60bafdd3ac851337a7dfb1de3a890607a18ecf4b6da8b419f094c8c774ef04d80b1fb4737842d54805f872064a4d935e5b75a6aaa66e26b2b721f07715a
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'active_record'
3
4
  require 'active_record/pg_generate_series/version'
4
5
  require 'active_record/pg_generate_series/extension'
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'active_record/pg_generate_series/sql_builder'
3
4
 
4
5
  module ActiveRecord
@@ -10,7 +11,8 @@ module ActiveRecord
10
11
  # @param [Integer] first Required, first value of GENERATE_SERIES.
11
12
  # @param [Integer] last Required, last value of GENERATE SERIES.
12
13
  # @param [Integer] step Optional, step value of GENERATE_SERIES, default is 1.
13
- # @param [String, Symbol] seq_name Optional, name of GENERATE_SERIES sequence, default is :seq.
14
+ # @param [String, Symbol] seq_name Optional, name of GENERATE_SERIES sequence,
15
+ # default is :seq.
14
16
  # @param [boolean] debug Optional, if true then sql is returned, not executed.
15
17
  # @param [Proc] block Required, block for setting selected columns.
16
18
  def insert_using_generate_series(first, last, step: 1, seq_name: :seq, debug: false, &block)
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'forwardable'
3
4
 
4
5
  module ActiveRecord
@@ -6,7 +7,7 @@ module ActiveRecord
6
7
  # SQL builder using PostgreSQL GENERATE_SERIES function.
7
8
  class SqlBuilder
8
9
  extend Forwardable
9
- def_delegators :@ar_class, :connection, :sanitize, :quoted_table_name
10
+ def_delegators :@ar_class, :connection, :quoted_table_name
10
11
 
11
12
  def initialize(ar_class, first, last, step, seq_name)
12
13
  @ar_class = ar_class
@@ -28,7 +29,7 @@ module ActiveRecord
28
29
  INSERT INTO
29
30
  #{quoted_table_name} (#{@select_items.keys.map { |col| connection.quote_column_name(col) }.join(',')})
30
31
  SELECT
31
- #{@select_items.values.map { |val| val.is_a?(Raw) ? val.str : sanitize(val) }.join(",\n ")}
32
+ #{@select_items.values.map { |val| val.is_a?(Raw) ? val.str : connection.quote(val) }.join(",\n ")}
32
33
  FROM
33
34
  GENERATE_SERIES(#{@first.to_i}, #{@last.to_i}, #{@step.to_i}) AS #{connection.quote_column_name(@seq_name)}
34
35
  ;
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ActiveRecord
3
4
  module PgGenerateSeries
4
- VERSION = '0.1.2'.freeze
5
+ VERSION = '0.1.3'.freeze
5
6
  end
6
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record-pg_generate_series
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ryu39
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-26 00:00:00.000000000 Z
11
+ date: 2018-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -28,30 +28,30 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.13'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.13'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -67,19 +67,19 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.5'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rubocop
70
+ name: ryu39cop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: 0.49.1.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: 0.49.1.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: pry
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -132,28 +132,7 @@ executables: []
132
132
  extensions: []
133
133
  extra_rdoc_files: []
134
134
  files:
135
- - ".codeclimate.yml"
136
- - ".gitignore"
137
- - ".rspec"
138
- - ".rubocop.yml"
139
- - ".travis.yml"
140
- - CHANGELOG.md
141
- - CODE_OF_CONDUCT.md
142
- - Gemfile
143
135
  - LICENSE.txt
144
- - README.md
145
- - Rakefile
146
- - active_record-pg_generate_series.gemspec
147
- - bin/benchmark.rb
148
- - bin/console
149
- - bin/db_setup.rb
150
- - bin/setup
151
- - connect_db.rb
152
- - database.yml
153
- - docker-compose.yml
154
- - gemfiles/active_record_32.gemfile
155
- - gemfiles/active_record_42.gemfile
156
- - gemfiles/active_record_50.gemfile
157
136
  - lib/active_record/pg_generate_series.rb
158
137
  - lib/active_record/pg_generate_series/extension.rb
159
138
  - lib/active_record/pg_generate_series/sql_builder.rb
@@ -178,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
157
  version: '0'
179
158
  requirements: []
180
159
  rubyforge_project:
181
- rubygems_version: 2.6.8
160
+ rubygems_version: 2.7.6
182
161
  signing_key:
183
162
  specification_version: 4
184
163
  summary: Add a feature which inserts records using PostgreSQL generate_series function
@@ -1,16 +0,0 @@
1
- ---
2
- engines:
3
- duplication:
4
- enabled: true
5
- config:
6
- languages:
7
- - ruby
8
- fixme:
9
- enabled: true
10
- rubocop:
11
- enabled: true
12
- ratings:
13
- paths:
14
- - "**.rb"
15
- exclude_paths:
16
- - spec/
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --format documentation
2
- --color
@@ -1,63 +0,0 @@
1
- AllCops:
2
- DisplayCopNames: true
3
-
4
- ##################### Linting #############################
5
-
6
- ##################### Metrics #############################
7
-
8
- Metrics/AbcSize:
9
- Max: 20
10
-
11
- Metrics/BlockLength:
12
- Exclude:
13
- - 'spec/**/*'
14
- - '*.gemspec'
15
-
16
- Metrics/LineLength:
17
- Max: 120
18
-
19
- Metrics/MethodLength:
20
- Max: 30
21
-
22
- Metrics/ParameterLists:
23
- CountKeywordArgs: false
24
-
25
- ##################### Performance #############################
26
-
27
- Performance/FlatMap:
28
- EnabledForFlattenWithoutParams: false
29
-
30
- Performance/RedundantBlockCall:
31
- Enabled: false
32
-
33
- ##################### Rails ##################################
34
-
35
- Rails/SafeNavigation:
36
- ConvertTry: true
37
-
38
- Rails/SaveBang:
39
- Enabled: true
40
-
41
- ##################### Security ##################################
42
-
43
- Security/YAMLLoad:
44
- Enabled: false
45
-
46
- ##################### Style #################################
47
-
48
- Style/AutoResourceCleanup:
49
- Enabled: true
50
-
51
- Style/Encoding:
52
- Enabled: true
53
- EnforcedStyle: never
54
-
55
- Style/IfUnlessModifier:
56
- Enabled: false
57
-
58
- Style/MultilineMethodCallIndentation:
59
- Enabled: false
60
-
61
- ##################### Bundler #################################
62
- Bundler/OrderedGems:
63
- Enabled: false
@@ -1,41 +0,0 @@
1
- language: ruby
2
-
3
- rvm:
4
- - 2.4.0
5
- - 2.3.3
6
- - 2.2.6
7
- - 2.1.10
8
- - 2.0.0
9
-
10
- gemfile:
11
- - gemfiles/active_record_50.gemfile
12
- - gemfiles/active_record_42.gemfile
13
- - gemfiles/active_record_32.gemfile
14
-
15
- sudo: false
16
- services:
17
- - postgresql
18
-
19
- before_install:
20
- - 'gem update bundler --no-document'
21
- before_script:
22
- - 'bundle exec ruby bin/db_setup.rb'
23
- script:
24
- - 'bundle exec rspec'
25
- after_success:
26
- - 'bundle exec codeclimate-test-reporter'
27
-
28
- cache: bundler
29
-
30
- matrix:
31
- exclude:
32
- - rvm: 2.0.0
33
- gemfile: gemfiles/active_record_50.gemfile
34
- - rvm: 2.1.10
35
- gemfile: gemfiles/active_record_50.gemfile
36
- - rvm: 2.3.3
37
- gemfile: gemfiles/active_record_32.gemfile
38
- - rvm: 2.4.0
39
- gemfile: gemfiles/active_record_32.gemfile
40
- - rvm: 2.4.0
41
- gemfile: gemfiles/active_record_42.gemfile
@@ -1,14 +0,0 @@
1
- # Change logs
2
-
3
- ## v0.1.2
4
-
5
- * Add rubocop and fix its offenses. (#4)
6
- * Add codeclimate and fix its errors. (#5, #6)
7
-
8
- ## v0.1.1
9
-
10
- * Add frozen string literal comment. (#3)
11
-
12
- ## v0.1.0
13
-
14
- * First release.
@@ -1,74 +0,0 @@
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 DELETED
@@ -1,8 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- group :test do
4
- gem 'simplecov'
5
- gem 'codeclimate-test-reporter', '~> 1.0.0'
6
- end
7
-
8
- gemspec
data/README.md DELETED
@@ -1,214 +0,0 @@
1
- # ActiveRecord::PgGenerateSeries
2
-
3
- [![Gem Version](https://badge.fury.io/rb/active_record-pg_generate_series.svg)](https://badge.fury.io/rb/active_record-pg_generate_series)
4
- [![Build Status](https://travis-ci.org/ryu39/active_record-pg_generate_series.svg?branch=master)](https://travis-ci.org/ryu39/active_record-pg_generate_series)
5
- [![Code Climate](https://codeclimate.com/github/ryu39/active_record-pg_generate_series/badges/gpa.svg)](https://codeclimate.com/github/ryu39/active_record-pg_generate_series)
6
- [![Test Coverage](https://codeclimate.com/github/ryu39/active_record-pg_generate_series/badges/coverage.svg)](https://codeclimate.com/github/ryu39/active_record-pg_generate_series/coverage)
7
- [![Issue Count](https://codeclimate.com/github/ryu39/active_record-pg_generate_series/badges/issue_count.svg)](https://codeclimate.com/github/ryu39/active_record-pg_generate_series)
8
-
9
- This gem adds a feature which inserts records using PostgreSQL [GENERATE_SERIES](https://www.postgresql.org/docs/current/static/functions-srf.html) function to ActiveRecord.
10
-
11
- Insertion using GENERATE_SERIES function is very fast.
12
- It is about 300-400 times faster than iteration of ActiveRecord::Base#save and 30-40 times faster than bulk insert. (In authors env)
13
-
14
- ## Benchmark
15
-
16
- I compared to iteration of ActiveRecord::Base#save(without validation) and
17
- bulk insertion using [activerecord-import](https://github.com/zdennis/activerecord-import).
18
-
19
- The average(3 times) of inserting 10,000 records is shown as follows.
20
- This is measured in author's PC(MacBook Pro Retina 13-inch Early 2015).
21
-
22
- | Target | Time(sec) |
23
- |:---------------------------------|----------:|
24
- | ActiveRecord#save | 37.442 |
25
- | activerecord-import | 3.149 |
26
- | active_record-pg_generate_series | 0.092 |
27
-
28
- You can run benchmark with following commands.
29
-
30
- $ docker-compose up -d
31
- $ ./bin/setup
32
- $ ruby bin/benchmark.rb
33
-
34
- ## Installation
35
-
36
- Add this line to your application's Gemfile:
37
-
38
- ```ruby
39
- gem 'active_record-pg_generate_series'
40
- ```
41
-
42
- And then execute:
43
-
44
- $ bundle
45
-
46
- Or install it yourself as:
47
-
48
- $ gem install active_record-pg_generate_series
49
-
50
- ## Usage
51
-
52
- You can insert records by calling `ActiveRecord::Base.insert_using_generate_series`.
53
- This function requires 3 parameters, `first`, `last` and block.
54
- The parameters `first` and `last` are passed to PostgreSQL GENERATE_SERIES function.
55
- The block specifies values of insert records.
56
-
57
- Please see this example.
58
-
59
- ```ruby
60
- # User is a subclass of ActiveRecord::Base
61
- User.insert_using_generate_series(1, 10) do |sql|
62
- sql.name = 'username'
63
- sql.age = 16
64
- sql.birth_date = Date.new(2000, 1, 1)
65
- end
66
-
67
- p User.all
68
- # => #<ActiveRecord::Relation
69
- # [#<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">,
70
- # #<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">,
71
- # :
72
- # #<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">]>
73
- ```
74
-
75
- Note that `created_at` and `updated_at` are set automatically.
76
- You can overwrite these values in block.
77
-
78
- You can also use sequence value generated by GENERATE_SERIES function using `#raw` method with `seq` in sql block.
79
-
80
- ```ruby
81
- User.insert_using_generate_series(1, 10) do |sql|
82
- sql.name = raw("'username' || seq")
83
- sql.age = raw("seq")
84
- sql.birth_date = raw("'1999-12-31'::date + seq")
85
- sql.disabled = raw("CASE seq % 2 WHEN 0 THEN true ELSE false END")
86
- end
87
-
88
- p User.all
89
- # => #<ActiveRecord::Relation
90
- # [#<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">,
91
- # #<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">,
92
- # :
93
- # #<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">]>
94
- ```
95
-
96
- When you use `#raw` method, please take care of sql injection because `#raw` method does not sanitize given string.
97
-
98
- ### STI support
99
-
100
- When target is STI subclass, the type value(default is `type` column) is set automatically.
101
-
102
- ```ruby
103
- # AdminUser is a subclass of User.
104
- AdminUser.insert_using_generate_series(1, 10) do |sql|
105
- sql.name = 'admin username'
106
- sql.age = 16
107
- sql.birth_date = Date.new(2000, 1, 1)
108
- end
109
-
110
- p AdminUser.all
111
- # => #<ActiveRecord::Relation
112
- # [#<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">,
113
- # #<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">,
114
- # :
115
- # #<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">]>
116
- ```
117
-
118
- ### Options
119
-
120
- #### Step
121
-
122
- You can change step value of GENERATE_SERIES function (Default: 1) with `step` option.
123
-
124
- ```ruby
125
- User.insert_using_generate_series(1, 10, step: 5) do |sql|
126
- sql.name = raw("'username' || seq")
127
- sql.age = raw("seq")
128
- sql.birth_date = raw("'1999-12-31'::date + seq")
129
- sql.disabled = raw("CASE seq % 2 WHEN 0 THEN true ELSE false END")
130
- end
131
-
132
- p User.all
133
- # => #<ActiveRecord::Relation
134
- # [#<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">,
135
- # #<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">]>
136
- ```
137
-
138
- #### Sequence name
139
-
140
- Also, you can change sequence name of GENERATE_SERIES function (Default: `seq`) with `seq_name` option.
141
-
142
- ```ruby
143
- User.insert_using_generate_series(1, 10, seq_name: 'new_seq') do |sql|
144
- sql.name = raw("'username' || new_seq")
145
- sql.age = raw("new_seq")
146
- sql.birth_date = raw("'1999-12-31'::date + new_seq")
147
- sql.disabled = raw("CASE new_seq % 2 WHEN 0 THEN true ELSE false END")
148
- end
149
-
150
- p User.all
151
- # => #<ActiveRecord::Relation
152
- # [#<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">,
153
- # #<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">,
154
- # #<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">,
155
- # #<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">,
156
- # #<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">,
157
- # #<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">,
158
- # #<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">,
159
- # #<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">,
160
- # #<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">,
161
- # #<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">]>
162
- ```
163
-
164
- #### Debug
165
-
166
- When `insert_using_generate_series` is called with `debug` option, it does not execute sql but returns sql to be executed.
167
-
168
- ```ruby
169
- sql = User.insert_using_generate_series(1, 10, debug: true) do |sql|
170
- sql.name = raw("'username' || seq")
171
- sql.age = raw("seq")
172
- sql.birth_date = raw("'1999-12-31'::date + seq")
173
- sql.disabled = raw("CASE seq % 2 WHEN 0 THEN true ELSE false END")
174
- end
175
-
176
- puts sql
177
- # => INSERT INTO
178
- # "users" ("created_at","updated_at","name","age","birth_date","disabled")
179
- # SELECT
180
- # '2016-11-23 09:32:29.750549',
181
- # '2016-11-23 09:32:29.750549',
182
- # 'username' || seq,
183
- # seq,
184
- # '1999-12-31'::date + seq,
185
- # CASE seq % 2 WHEN 0 THEN true ELSE false END
186
- # FROM
187
- # GENERATE_SERIES(1, 10, 1) AS "seq"
188
- # ;
189
- ```
190
-
191
- ## Development
192
-
193
- First docker and docker-compose is required.
194
-
195
- After checking out the repo, run `bin/setup` to install dependencies.
196
- Then, run `rake spec` to run the tests.
197
- You can also run `bin/console` for an interactive prompt that will allow you to experiment.
198
-
199
- To install this gem onto your local machine, run `bundle exec rake install`.
200
- To release a new version, update the version number in `version.rb`,
201
- and then run `bundle exec rake release`, which will create a git tag for the version,
202
- push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
203
-
204
- ## Contributing
205
-
206
- Bug reports and pull requests are welcome on GitHub at https://github.com/ryu39/active_record-pg_generate_series.
207
- This project is intended to be a safe, welcoming space for collaboration,
208
- and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
209
-
210
-
211
- ## License
212
-
213
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
214
-
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require 'bundler/gem_tasks'
2
- require 'rspec/core/rake_task'
3
- require 'rubocop/rake_task'
4
-
5
- RSpec::Core::RakeTask.new(:spec)
6
- RuboCop::RakeTask.new(:spec)
7
-
8
- task default: :spec
@@ -1,36 +0,0 @@
1
- lib = File.expand_path('../lib', __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require 'active_record/pg_generate_series/version'
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = 'active_record-pg_generate_series'
7
- spec.version = ActiveRecord::PgGenerateSeries::VERSION
8
- spec.authors = ['ryu39']
9
- spec.email = ['dev.ryu39@gmail.com']
10
-
11
- spec.summary = 'Add a feature which inserts records using PostgreSQL generate_series function to ActiveRecord'
12
- spec.description = <<EOS.tr("\n", ' ')
13
- This gem adds a feature which inserts records using PostgreSQL generate_series function to ActiveRecord.
14
- Insertion using generate_series function is very fast, so it is useful
15
- when you want to insert simple but many and many record, e.g. prepare for performance test.
16
- EOS
17
- spec.homepage = 'https://github.com/ryu39/active_record-pg_generate_series'
18
- spec.license = 'MIT'
19
-
20
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
- f.match(%r{^(test|spec|features)/})
22
- end
23
- spec.bindir = 'exe'
24
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
- spec.require_paths = ['lib']
26
-
27
- spec.add_runtime_dependency 'activerecord'
28
-
29
- spec.add_development_dependency 'bundler', '~> 1.13'
30
- spec.add_development_dependency 'rake', '~> 10.0'
31
- spec.add_development_dependency 'rspec', '>= 3.5'
32
- spec.add_development_dependency 'rubocop'
33
- spec.add_development_dependency 'pry'
34
- spec.add_development_dependency 'pg'
35
- spec.add_development_dependency 'activerecord-import'
36
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'bundler/setup'
3
- require 'active_record'
4
- require 'activerecord-import'
5
- require File.expand_path('../../connect_db.rb', __FILE__)
6
- require File.expand_path('../../spec/models/user.rb', __FILE__)
7
- require 'active_record/pg_generate_series'
8
-
9
- require 'benchmark'
10
-
11
- RECORD_NUM = 10_000
12
-
13
- def build_user(num)
14
- User.new(name: "name#{num + 1}", age: num + 1, birth_date: Date.today + num + 1)
15
- end
16
-
17
- User.delete_all
18
-
19
- GC.disable
20
- Benchmark.bm(36) do |x|
21
- x.report('iteration of ActiveRecord::Base#save') do
22
- RECORD_NUM.times do |i|
23
- user = build_user(i)
24
- user.save(validate: false)
25
- end
26
- end
27
-
28
- x.report('bulk insert(activerecord-import)') do
29
- users = Array.new(RECORD_NUM) { |i| build_user(i) }
30
- User.import(users)
31
- end
32
-
33
- x.report('active_record-pg_generate_series') do
34
- User.insert_using_generate_series(1, RECORD_NUM) do |sql|
35
- sql.name = raw("'name' || seq")
36
- sql.age = raw('seq')
37
- sql.birth_date = raw("'2000-01-01'::date + seq")
38
- end
39
- end
40
- end
41
- GC.enable
42
-
43
- User.delete_all
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup'
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 'pry'
9
-
10
- Pry.start
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'erb'
3
- require 'yaml'
4
- require 'active_record'
5
-
6
- config_file = File.expand_path('../../database.yml', __FILE__)
7
- config = YAML.load(ERB.new(IO.read(config_file)).result)['db']
8
-
9
- ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres'))
10
- ActiveRecord::Base.connection.drop_database(config['database'])
11
- ActiveRecord::Base.connection.create_database(config['database'])
12
- ActiveRecord::Base.establish_connection(config)
13
-
14
- ActiveRecord::Base.connection.create_table :users do |t|
15
- t.string :type
16
- t.string :name, null: false
17
- t.integer :age, null: false
18
- t.date :birth_date, null: false
19
- t.boolean :disabled, null: false, default: false
20
-
21
- t.timestamps
22
- end
data/bin/setup DELETED
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- docker-compose up -d db
9
-
10
- ruby $( dirname $0 )/db_setup.rb
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'erb'
3
- require 'yaml'
4
- require 'active_record'
5
-
6
- config_file = File.expand_path('../database.yml', __FILE__)
7
- config = YAML.load(ERB.new(IO.read(config_file)).result)['db']
8
-
9
- ActiveRecord::Base.establish_connection(config)
@@ -1,8 +0,0 @@
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') %>"
@@ -1,9 +0,0 @@
1
- version: '2'
2
-
3
- services:
4
- db:
5
- image: postgres:latest
6
- environment:
7
- POSTGRES_PASSWORD: postgres
8
- ports:
9
- - '5432:5432'
@@ -1,10 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'activerecord', '~> 3.2.0', require: 'active_record'
4
-
5
- group :test do
6
- gem 'simplecov'
7
- gem 'codeclimate-test-reporter', '~> 1.0.0'
8
- end
9
-
10
- gemspec path: '../'
@@ -1,9 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'activerecord', '~> 4.2.0', require: 'active_record'
4
- group :test do
5
- gem 'simplecov'
6
- gem 'codeclimate-test-reporter', '~> 1.0.0'
7
- end
8
-
9
- gemspec path: '../'
@@ -1,9 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'activerecord', '~> 5.0.0', require: 'active_record'
4
- group :test do
5
- gem 'simplecov'
6
- gem 'codeclimate-test-reporter', '~> 1.0.0'
7
- end
8
-
9
- gemspec path: '../'