postgres-migration-cops 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 71e94e1d2931b2029aa8efa2ffec426c90dd9d22d50233ab6dbcf7c5dce6722c
4
+ data.tar.gz: 73425f5019bf182bf60131f9de10b6358be8662ec023f58af8e5089806e8ecb8
5
+ SHA512:
6
+ metadata.gz: f517ef527f1b720d7ffe2c96810307ec1b3ba8aadbbcf62adc991a917551fd40c7c9909c121b48acf598d663b2fc29ba4ba0a84ddb7d4cc481bce597bda7d8f3
7
+ data.tar.gz: 368d5c3717c91ffdd109f166e97bba146d7b93c70d1c90f6b41a889bbd726468af8bbbc9c403ef576a4240a7badce46caca96d273b13f6fa3cb8da01f1375a6f
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.7
7
+ before_install: gem install bundler -v 2.0.1
@@ -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 gbatterbee1@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
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ ruby '2.3.7'
6
+ gem 'rubocop'
7
+
8
+ group :development do
9
+ gem 'rspec'
10
+ gem 'rspec-core'
11
+ gem 'rubocop-rspec'
12
+ gem 'solargraph'
13
+ end
14
+
15
+ # Specify your gem's dependencies in postgres-migration-cops.gemspec
16
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,85 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ postgres-migration-cops (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ backport (0.3.0)
11
+ diff-lcs (1.3)
12
+ htmlentities (4.3.4)
13
+ jaro_winkler (1.5.2)
14
+ kramdown (1.17.0)
15
+ mini_portile2 (2.1.0)
16
+ nokogiri (1.6.8.1)
17
+ mini_portile2 (~> 2.1.0)
18
+ parallel (1.14.0)
19
+ parser (2.6.0.0)
20
+ ast (~> 2.4.0)
21
+ powerpack (0.1.2)
22
+ psych (3.1.0)
23
+ rainbow (3.0.0)
24
+ rake (10.4.2)
25
+ reverse_markdown (1.1.0)
26
+ nokogiri
27
+ rspec (3.8.0)
28
+ rspec-core (~> 3.8.0)
29
+ rspec-expectations (~> 3.8.0)
30
+ rspec-mocks (~> 3.8.0)
31
+ rspec-core (3.8.0)
32
+ rspec-support (~> 3.8.0)
33
+ rspec-expectations (3.8.2)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.8.0)
36
+ rspec-mocks (3.8.0)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.8.0)
39
+ rspec-support (3.8.0)
40
+ rubocop (0.65.0)
41
+ jaro_winkler (~> 1.5.1)
42
+ parallel (~> 1.10)
43
+ parser (>= 2.5, != 2.5.1.1)
44
+ powerpack (~> 0.1)
45
+ psych (>= 3.1.0)
46
+ rainbow (>= 2.2.2, < 4.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (~> 1.4.0)
49
+ rubocop-rspec (1.32.0)
50
+ rubocop (>= 0.60.0)
51
+ ruby-progressbar (1.10.0)
52
+ solargraph (0.31.3)
53
+ backport (~> 0.3)
54
+ htmlentities (~> 4.3, >= 4.3.4)
55
+ jaro_winkler (~> 1.5)
56
+ kramdown (~> 1.16)
57
+ parser (~> 2.3)
58
+ reverse_markdown (~> 1.0, >= 1.0.5)
59
+ rubocop (~> 0.52)
60
+ thor (~> 0.19, >= 0.19.4)
61
+ tilt (~> 2.0)
62
+ yard (~> 0.9)
63
+ thor (0.20.0)
64
+ tilt (2.0.9)
65
+ unicode-display_width (1.4.1)
66
+ yard (0.9.18)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ bundler (~> 2.0)
73
+ postgres-migration-cops!
74
+ rake (~> 10.0)
75
+ rspec
76
+ rspec-core
77
+ rubocop
78
+ rubocop-rspec
79
+ solargraph
80
+
81
+ RUBY VERSION
82
+ ruby 2.3.7p456
83
+
84
+ BUNDLED WITH
85
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 G Batterbee
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,43 @@
1
+ # Postgres::Migration::Cops
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/postgres/migration/cops`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'postgres-migration-cops'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install postgres-migration-cops
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/postgres-migration-cops. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Postgres::Migration::Cops project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/postgres-migration-cops/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'postgres/migration/cops'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/examples.txt ADDED
@@ -0,0 +1,12 @@
1
+ example_id | status | run_time |
2
+ --------------------------------------------------------- | ------ | --------------- |
3
+ ./spec/lib/cops/add_indexes_concurrently_spec.rb[1:1:1:1] | passed | 0.03992 seconds |
4
+ ./spec/lib/cops/add_indexes_concurrently_spec.rb[1:1:2:1] | passed | 0.00308 seconds |
5
+ ./spec/lib/cops/add_indexes_concurrently_spec.rb[1:1:3:1] | passed | 0.00307 seconds |
6
+ ./spec/lib/cops/add_indexes_concurrently_spec.rb[1:2:1:1] | passed | 0.00261 seconds |
7
+ ./spec/lib/cops/add_indexes_concurrently_spec.rb[1:2:2:1] | passed | 0.00272 seconds |
8
+ ./spec/lib/cops/add_indexes_concurrently_spec.rb[1:2:3:1] | passed | 0.00143 seconds |
9
+ ./spec/lib/cops/use_add_index_spec.rb[1:1:1] | passed | 0.00276 seconds |
10
+ ./spec/lib/cops/use_add_index_spec.rb[1:2:1] | passed | 0.00454 seconds |
11
+ ./spec/lib/cops/use_add_index_spec.rb[1:3:1] | passed | 0.00166 seconds |
12
+ ./spec/lib/cops/use_add_index_spec.rb[1:4:1] | passed | 0.00154 seconds |
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module PostgresMigrationCops
6
+ # Check that indexes are added concurrently with disable_ddl_transaction!
7
+ # https://thoughtbot.com/blog/how-to-create-postgres-indexes-concurrently-in
8
+ #
9
+ # @example
10
+ # # bad
11
+ # class SomeTableMigrations < ActiveRecord::Migration
12
+ # def change
13
+ # add_column :table, :some_column_1, :integer, default: 0, null: false
14
+ # add_column :table, :other_column, :string
15
+ # add_column :table, :doneit_at, :datetime
16
+ # add_index :table,
17
+ # :other_column,
18
+ # unique: true
19
+ # end
20
+ # end
21
+ #
22
+ # # good
23
+ # class SomeTableMigrations < ActiveRecord::Migration
24
+ # disable_ddl_transaction!
25
+ # def change
26
+ # add_column :table, :some_column_1, :integer, default: 0, null: false
27
+ # add_column :table, :other_column, :string
28
+ # add_column :table, :doneit_at, :datetime
29
+ # add_index :table,
30
+ # :other_column,
31
+ # unique: true,
32
+ # algorithm: :concurrently
33
+ # end
34
+ # end
35
+ class AddIndexesConcurrently < Cop
36
+ IGNORE_DDL = 'Concurrent indexes require "disable_ddl_transaction!"'
37
+ CONCURRENTLY = "Indexes should be added with 'algorithm: :concurrently'"
38
+
39
+ def on_class(class_node)
40
+ is_migration = class_node.children.any? { |n| is_migration?(n) }
41
+ return unless is_migration
42
+
43
+ index_nodes = find_index_methods(class_node)
44
+ return unless creating_indexes?(index_nodes)
45
+
46
+ ensure_disable_dll_transaction_offense(class_node, index_nodes)
47
+ ensure_indexes_added_non_concurrently_offenses(index_nodes)
48
+ end
49
+
50
+ private
51
+
52
+ def creating_indexes?(index_nodes)
53
+ index_nodes.count.positive?
54
+ end
55
+
56
+ def ensure_disable_dll_transaction_offense(class_node, index_nodes)
57
+ return if disable_ddl_transaction_declared?(class_node)
58
+
59
+ has_concurrent = concurrent_indexes?(index_nodes)
60
+ add_offense(class_node, message: IGNORE_DDL) if has_concurrent
61
+ end
62
+
63
+ def disable_ddl_transaction_declared?(node)
64
+ begins = find__begin node
65
+ begins.any? { |b| b.children.any? { |n| has_disable_ddl?(n) } }
66
+ end
67
+
68
+ def concurrent_indexes?(index_nodes)
69
+ index_nodes
70
+ .select { |n| n.source.include?('concurrent') }
71
+ .count
72
+ .positive?
73
+ end
74
+
75
+ def ensure_indexes_added_non_concurrently_offenses(index_nodes)
76
+ index_nodes
77
+ .reject { |n| n.source.include?('concurrently') }
78
+ .to_a.each { |n| add_offense(n, message: CONCURRENTLY) }
79
+ end
80
+
81
+ def find_index_methods(node)
82
+ sends = find__send node
83
+ sends.select { |n| n.source.include?('add_index') }
84
+ end
85
+
86
+ def_node_matcher :is_migration?, <<-PATTERN
87
+ (const ... :Migration)
88
+ PATTERN
89
+
90
+ def_node_search :find__send, <<-PATTERN
91
+ send
92
+ PATTERN
93
+
94
+ def_node_matcher :is_add_index?, <<-PATTERN
95
+ (send nil :add_index ... )
96
+ PATTERN
97
+
98
+ def_node_search :find__begin, <<-PATTERN
99
+ begin
100
+ PATTERN
101
+
102
+ def_node_matcher :has_disable_ddl?, <<-PATTERN
103
+ (... :disable_ddl_transaction!)
104
+ PATTERN
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,221 @@
1
+ require './lib/cops/add_indexes_concurrently'
2
+
3
+ # frozen_string_literal: true
4
+
5
+ RSpec.describe RuboCop::Cop::PostgresMigrationCops::AddIndexesConcurrently do
6
+ subject(:cop) { described_class.new }
7
+
8
+ context 'when disable_ddl_transaction! is not declared' do
9
+ context 'and it is not a migration' do
10
+ it 'does not register an ignore ddl offence' do
11
+ expect_no_offenses(<<-RUBY)
12
+ class SomeTableMigrations
13
+ def change
14
+ add_column :table, :some_column_1, :integer, default: 0, null: false
15
+ add_column :table, :other_column, :string
16
+ add_column :table, :doneit_at, :datetime
17
+ add_index :table,
18
+ :other_column,
19
+ unique: true,
20
+ algorithm: :concurrently
21
+ end
22
+ end
23
+ RUBY
24
+ end
25
+ end
26
+
27
+ context 'and an index is not created concurrently' do
28
+ it 'registers an concurrent index offence' do
29
+ expect_offense(<<-RUBY)
30
+ class SomeTableMigrations < ActiveRecord::Migration
31
+ def change
32
+ add_column :table, :some_column_1, :integer, default: 0, null: false
33
+ add_column :table, :other_column, :string
34
+ add_column :table, :doneit_at, :datetime
35
+ add_index :table,
36
+ ^^^^^^^^^^^^^^^^^^ Indexes should be added with 'algorithm: :concurrently'
37
+ :other_column,
38
+ unique: true
39
+ end
40
+ end
41
+ RUBY
42
+ end
43
+ end
44
+
45
+ context 'and an index is created concurrently' do
46
+ it 'registers an ignore ddl offence' do
47
+ expect_offense(<<-RUBY)
48
+ class SomeTableMigrations < ActiveRecord::Migration
49
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Concurrent indexes require "disable_ddl_transaction!"
50
+ def change
51
+ add_column :table, :some_column_1, :integer, default: 0, null: false
52
+ add_column :table, :other_column, :string
53
+ add_column :table, :doneit_at, :datetime
54
+ add_index :table,
55
+ :other_column,
56
+ unique: true,
57
+ algorithm: :concurrently
58
+ end
59
+ end
60
+ RUBY
61
+ end
62
+ end
63
+ end
64
+
65
+ context 'when disable_ddl_transaction! is declared' do
66
+ context 'when not a migration' do
67
+ it 'does not register an offense ' do
68
+ expect_no_offenses(<<-RUBY)
69
+ class SomeTableMigrations
70
+ disable_ddl_transaction!
71
+ def change
72
+ add_column :table, :doneit_at, :datetime, :index
73
+ add_index :table,
74
+ :other_column,
75
+ unique: true,
76
+ algorithm: :concurrently
77
+ end
78
+ end
79
+ RUBY
80
+ end
81
+ end
82
+
83
+ context 'and an index is registered concurrently' do
84
+ it 'does not register an offense' do
85
+ expect_no_offenses(<<-RUBY)
86
+ class SomeTableMigrations < ActiveRecord::Migration
87
+ disable_ddl_transaction!
88
+ def change
89
+ add_column :table, :doneit_at, :datetime
90
+ add_index :table,
91
+ :other_column,
92
+ unique: true,
93
+ algorithm: :concurrently
94
+ end
95
+ end
96
+ RUBY
97
+ end
98
+ end
99
+
100
+ context 'and an index is not registered concurrently' do
101
+ it 'registers an concurrency offense' do
102
+ expect_offense(<<-RUBY)
103
+ class SomeTableMigrations < ActiveRecord::Migration
104
+ disable_ddl_transaction!
105
+ def change
106
+ add_column :table, :doneit_at, :datetime, :index
107
+ add_index :table,
108
+ ^^^^^^^^^^^^^^^^^^ Indexes should be added with 'algorithm: :concurrently'
109
+ :other_column,
110
+ unique: true
111
+ end
112
+ end
113
+ RUBY
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ # code = '!something.empty?'
120
+ # source = RuboCop::ProcessedSource.new(code, RUBY_VERSION.to_f)
121
+ # node = source.ast
122
+ # # => s(:send, s(:send, s(:send, nil, :something), :empty?), :!)
123
+ # The node has a few attributes that can be useful in the journey:
124
+
125
+ # node.type # => :send
126
+ # node.children # => [s(:send, s(:send, nil, :something), :empty?), :!]
127
+ # node.source # => "!something.empty?"
128
+
129
+ # xit 'registers an offense when index is added concurrently' do
130
+ # expect_offense(<<-RUBY)
131
+ # class SomeTableMigrations < ActiveRecord::Migration
132
+ # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Concurrent indexes require "disable_ddl_transaction!" at the top of the class
133
+ # disable_ddl_transaction!
134
+ # def change
135
+ # add_column :table, :some_column_1, :integer, default: 0, null: false
136
+ # add_column :table, :other_column, :string
137
+ # add_column :table, :doneit_at, :datetime
138
+ # add_index :table,
139
+ # :other_column,
140
+ # unique: true,
141
+ # algorithm: :concurrently
142
+ # end
143
+ # end
144
+ # RUBY
145
+ # # end
146
+
147
+ # xit 'xxxregisters an offense when index not added concurrently' do
148
+ # expect_offense(<<-RUBY)
149
+ # class SomeTableMigrations < ActiveRecord::Migration
150
+ # disable_ddl_transaction!
151
+ # ^^^^^^^^^^^^^^^^^^^^^^^^ Concurrent indexes require "disable_ddl_transaction!" at the top of the class
152
+
153
+ # def change
154
+ # add_column :table, :some_column_1, :integer, default: 0, null: false
155
+ # add_column :table, :other_column, :string
156
+ # add_column :table, :doneit_at, :datetime
157
+ # add_index :table,
158
+ # :other_column,
159
+ # unique: true,
160
+ # algorithm: :concurrently
161
+ # end
162
+ # end
163
+ # RUBY
164
+ # end
165
+ # xit 'does not register an offense when index not added concurrently' do
166
+ # expect_no_offenses(<<-RUBY)
167
+ # class SomeTableMigrations < ActiveRecord::Migration
168
+ # disable_ddl_transaction!
169
+ # def change
170
+ # add_column :table, :some_column_1, :integer, default: 0, null: false
171
+ # add_column :table, :other_column, :string
172
+ # add_column :table, :doneit_at, :datetime
173
+ # add_index :table,
174
+ # :other_column,
175
+ # unique: true
176
+ # end
177
+ # end
178
+ # RUBY
179
+ # end
180
+
181
+ # xit 'does not register an offense when index not a migration' do
182
+ # expect_no_offenses(<<-RUBY)
183
+ # class SomeTableMigrations < ActiveRecord::Migration
184
+ # disable_ddl_transaction!
185
+ # def change
186
+ # add_column :table, :some_column_1, :integer, default: 0, null: false
187
+ # add_column :table, :other_column, :string
188
+ # add_column :table, :doneit_at, :datetime
189
+ # add_index :table,
190
+ # :other_column,
191
+ # unique: true
192
+ # end
193
+ # end
194
+ # RUBY
195
+ # end
196
+
197
+ # xit 'does not register an offense when not a migration' do
198
+ # expect_no_offenses(<<-RUBY)
199
+ # class SomeTableMigrations
200
+ # def change
201
+ # add_column :table, :doneit_at, :datetime, :index
202
+ # add_index :table,
203
+ # :other_column,
204
+ # unique: true
205
+ # end
206
+ # end
207
+ # RUBY
208
+ # end
209
+ # end
210
+
211
+ # class SomeTableMigrations < ActiveRecord::Migration
212
+ # def change
213
+ # add_column :table, :some_column_1, :integer, default: 0, null: false
214
+ # add_column :table, :other_column, :string
215
+ # add_column :table, :doneit_at, :datetime
216
+ # add_index :table,
217
+ # :other_column,
218
+ # unique: true,
219
+ # algorithm: :concurrently
220
+ # end
221
+ # end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module PostgresMigrationCops
6
+ # Check that indexes are added explicitly with add_index
7
+ # to ensure concurrency can be enabled
8
+ #
9
+ # @example
10
+ # # bad
11
+ # class SomeTableMigrations < ActiveRecord::Migration
12
+ # def change
13
+ # add_column :table, :some_column_1, :integer, :index
14
+ # add_column :table, :other_column, :string, index: true
15
+ # end
16
+ # end
17
+ #
18
+ # # good
19
+ # class SomeTableMigrations < ActiveRecord::Migration
20
+ # disable_ddl_transaction!
21
+ # def change
22
+ # add_column :table, :some_column_1, :integer
23
+ # add_column :table, :other_column, :string
24
+ # add_column :table, :doneit_at, :datetime
25
+ # add_index :table,
26
+ # :some_column_1,
27
+ # algorithm: :concurrently
28
+ # add_index :table,
29
+ # :other_column,
30
+ # algorithm: :concurrently
31
+
32
+ # end
33
+ # end
34
+ class UseAddIndex < Cop
35
+ MSG = "Add indexes using 'add_index ... algorithm: :concurrently'"
36
+
37
+ def on_class(class_node)
38
+ @is_migration = class_node.children.any? { |n| is_migration?(n) }
39
+ end
40
+
41
+ def on_send(send_node)
42
+ return unless @is_migration
43
+
44
+ add_offense(send_node) if contains_index?(send_node)
45
+ end
46
+
47
+ private
48
+
49
+ def_node_matcher :is_migration?, <<-PATTERN
50
+ (const ... :Migration)
51
+ PATTERN
52
+
53
+ def contains_index?(node)
54
+ return true if node.children.any? { |n| is_index_symbol?(n) }
55
+
56
+ contains_hash_index?(node)
57
+ end
58
+
59
+ def contains_hash_index?(node)
60
+ opts_node = node.children.last
61
+ return unless opts_node.class == RuboCop::AST::HashNode
62
+
63
+ opts_node.children.any? { |n| is_index_pair? n }
64
+ end
65
+
66
+ def_node_matcher :is_index_symbol?, <<-PATTERN
67
+ (sym :index)
68
+ PATTERN
69
+
70
+ def_node_matcher :is_index_pair?, <<-PATTERN
71
+ (pair
72
+ (sym :index)
73
+ (true))
74
+ PATTERN
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,89 @@
1
+ require './lib/cops/use_add_index'
2
+
3
+ # frozen_string_literal: true
4
+
5
+ RSpec.describe RuboCop::Cop::PostgresMigrationCops::UseAddIndex do
6
+ subject(:cop) { described_class.new }
7
+
8
+ context 'when index is declared as an option' do
9
+ it 'it registers an offence' do
10
+ expect_offense(<<-RUBY)
11
+ class SomeTableMigrations < ActiveRecord::Migration
12
+ def change
13
+ add_column :table, :some_column_1, :integer, :index
14
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add indexes using 'add_index ... algorithm: :concurrently'
15
+ add_column :table, :other_column, :string
16
+ add_column :table, :doneit_at, :datetime
17
+ add_index :table,
18
+ :other_column,
19
+ unique: true
20
+
21
+ create_table :examples do |t|
22
+ t.references :category, :index
23
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add indexes using 'add_index ... algorithm: :concurrently'
24
+ t.integer :number_of_participants, :index
25
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add indexes using 'add_index ... algorithm: :concurrently'
26
+ end
27
+ end
28
+ end
29
+ RUBY
30
+ end
31
+ end
32
+
33
+ context 'when index is declared as a hash value option' do
34
+ it 'it registers an offence' do
35
+ expect_offense(<<-RUBY)
36
+ class SomeTableMigrations < ActiveRecord::Migration
37
+ def change
38
+ add_column :table, :some_column_1, :integer, index: true
39
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add indexes using 'add_index ... algorithm: :concurrently'
40
+ add_column :table, :other_column, :string
41
+ add_column :table, :doneit_at, :datetime
42
+ add_index :table,
43
+ :other_column,
44
+ unique: true
45
+
46
+ create_table :examples do |t|
47
+ t.references :category, index: true
48
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add indexes using 'add_index ... algorithm: :concurrently'
49
+ t.integer :number_of_participantsr, index: true
50
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add indexes using 'add_index ... algorithm: :concurrently'
51
+ end
52
+ end
53
+ end
54
+ RUBY
55
+ end
56
+ end
57
+
58
+ context 'when it is not a migration' do
59
+ it 'does not register an offence' do
60
+ expect_no_offenses(<<-RUBY)
61
+ class SomeTableMigrations
62
+ def change
63
+ add_column :table, :some_column_1, :integer, index: true
64
+ add_column :table, :other_column, :string, :index
65
+ add_column :table, :doneit_at, :datetime
66
+ add_index :table,
67
+ :other_column,
68
+ unique: true
69
+ end
70
+ end
71
+ RUBY
72
+ end
73
+ end
74
+
75
+ context 'when disable_ddl_transaction! is sepcified' do
76
+ it 'does not register an offence' do
77
+ expect_no_offenses(<<-RUBY)
78
+ class SomeTableMigrations < ActiveRecord::Migration
79
+ disable_ddl_transaction!
80
+ def change
81
+ add_column :table, :some_column_1, :integer
82
+ add_column :table, :other_column, :string
83
+ add_column :table, :doneit_at, :datetime
84
+ end
85
+ end
86
+ RUBY
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+
5
+ # cops
6
+ require_relative './cops/add_indexes_concurrently'
7
+ require_relative './cops/use_add_index'
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ # require 'postgres/migration/cops/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'postgres-migration-cops'
9
+ spec.version = '1.0.0' # Postgres::Migration::Cops::VERSION
10
+ spec.authors = ['G Batterbee']
11
+ spec.email = ['gbatterbee1@gmail.com']
12
+
13
+ spec.summary = 'Write a short summary, because RubyGems requires one.'
14
+ spec.description = 'Write a longer description or delete this line.'
15
+ # spec.homepage = "Put your gem's website or public repo URL here."
16
+ spec.license = 'MIT'
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ # if spec.respond_to?(:metadata)
21
+ # spec.metadata["allowed_push_host"] = "Set to 'http://mygemserver.com'"
22
+
23
+ # spec.metadata["homepage_uri"] = spec.homepage
24
+ # spec.metadata["source_code_uri"] = "Put your gem's public repo URL here."
25
+ # spec.metadata["changelog_uri"] = "Put your gem's CHANGELOG.md URL here."
26
+ # else
27
+ # raise "RubyGems 2.0 or newer is required to protect against " \
28
+ # "public gem pushes."
29
+ # end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
34
+ `git ls-files -z`.split("\x0").reject do |f|
35
+ f.match(%r{^(test|spec|features)/})
36
+ end
37
+ end
38
+ spec.bindir = 'exe'
39
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
40
+ spec.require_paths = ['lib']
41
+
42
+ spec.add_development_dependency 'bundler', '~> 2.0'
43
+ spec.add_development_dependency 'rake', '~> 10.0'
44
+ spec.add_development_dependency 'rspec', '~> 3.0'
45
+ end
data/rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ require: ./lib/cops/add-indexes-concurrently
2
+ ./lib/cops/require_disable_ddl_transaction
data/spec_helper.rb ADDED
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+ require 'rubocop/rspec/support'
5
+
6
+ # This file was generated by the `rspec --init` command. Conventionally, all
7
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
8
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
9
+ # this file to always be loaded, without a need to explicitly require it in any
10
+ # files.
11
+ #
12
+ # Given that it is always loaded, you are encouraged to keep this file as
13
+ # light-weight as possible. Requiring heavyweight dependencies from this file
14
+ # will add to the boot time of your test suite on EVERY test run, even for an
15
+ # individual file that may not need all of that loaded. Instead, consider making
16
+ # a separate helper file that requires the additional dependencies and performs
17
+ # the additional setup, and require it from the spec files that actually need
18
+ # it.
19
+ #
20
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
21
+ RSpec.configure do |config|
22
+ config.include RuboCop::RSpec::ExpectOffense
23
+
24
+ # rspec-expectations config goes here. You can use an alternate
25
+ # assertion/expectation library such as wrong or the stdlib/minitest
26
+ # assertions if you prefer.
27
+ config.expect_with :rspec do |expectations|
28
+ # This option will default to `true` in RSpec 4. It makes the `description`
29
+ # and `failure_message` of custom matchers include text for helper methods
30
+ # defined using `chain`, e.g.:
31
+ # be_bigger_than(2).and_smaller_than(4).description
32
+ # # => "be bigger than 2 and smaller than 4"
33
+ # ...rather than:
34
+ # # => "be bigger than 2"
35
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
36
+ end
37
+
38
+ # rspec-mocks config goes here. You can use an alternate test double
39
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
40
+ config.mock_with :rspec do |mocks|
41
+ # Prevents you from mocking or stubbing a method that does not exist on
42
+ # a real object. This is generally recommended, and will default to
43
+ # `true` in RSpec 4.
44
+ mocks.verify_partial_doubles = true
45
+ end
46
+
47
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
48
+ # have no way to turn it off -- the option exists only for backwards
49
+ # compatibility in RSpec 3). It causes shared context metadata to be
50
+ # inherited by the metadata hash of host groups and examples, rather than
51
+ # triggering implicit auto-inclusion in groups with matching metadata.
52
+ # config.shared_context_metadata_behavior = :apply_to_host_groups
53
+
54
+ # The settings below are suggested to provide a good initial experience
55
+ # with RSpec, but feel free to customize to your heart's content.
56
+ # # This allows you to limit a spec run to individual examples or groups
57
+ # # you care about by tagging them with `:focus` metadata. When nothing
58
+ # # is tagged with `:focus`, all examples get run. RSpec also provides
59
+ # # aliases for `it`, `describe`, and `context` that include `:focus`
60
+ # # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
61
+ # config.filter_run_when_matching :focus
62
+ #
63
+ # # Allows RSpec to persist some state between runs in order to support
64
+ # # the `--only-failures` and `--next-failure` CLI options. We recommend
65
+ # # you configure your source control system to ignore this file.
66
+ config.example_status_persistence_file_path = 'spec/examples.txt'
67
+ #
68
+ # # Limits the available syntax to the non-monkey patched syntax that is
69
+ # # recommended. For more details, see:
70
+ # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
71
+ # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
72
+ # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
73
+ # config.disable_monkey_patching!
74
+ #
75
+ # # This setting enables warnings. It's recommended, but in some cases may
76
+ # # be too noisy due to issues in dependencies.
77
+ # config.warnings = true
78
+ #
79
+ # # Many RSpec users commonly either run the entire suite or an individual
80
+ # # file, and it's useful to allow more verbose output when running an
81
+ # # individual spec file.
82
+ # if config.files_to_run.one?
83
+ # # Use the documentation formatter for detailed output,
84
+ # # unless a formatter has already been configured
85
+ # # (e.g. via a command-line flag).
86
+ # config.default_formatter = 'doc'
87
+ # end
88
+ #
89
+ # # Print the 10 slowest examples and example groups at the
90
+ # # end of the spec run, to help surface which specs are running
91
+ # # particularly slow.
92
+ # config.profile_examples = 10
93
+ #
94
+ # # Run specs in random order to surface order dependencies. If you find an
95
+ # # order dependency and want to debug it, you can fix the order by providing
96
+ # # the seed, which is printed after each run.
97
+ # # --seed 1234
98
+ # config.order = :random
99
+ #
100
+ # # Seed global randomization in this process using the `--seed` CLI option.
101
+ # # Setting this allows you to use `--seed` to deterministically reproduce
102
+ # # test failures related to randomization by passing the same `--seed` value
103
+ # # as the one that triggered the failure.
104
+ # Kernel.srand config.seed
105
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postgres-migration-cops
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - G Batterbee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - gbatterbee1@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - examples.txt
74
+ - lib/cops/add_indexes_concurrently.rb
75
+ - lib/cops/add_indexes_concurrently_spec.rb
76
+ - lib/cops/use_add_index.rb
77
+ - lib/cops/use_add_index_spec.rb
78
+ - lib/postgres-migration-cops.rb
79
+ - postgres-migration-cops.gemspec
80
+ - rubocop.yml
81
+ - spec_helper.rb
82
+ homepage:
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.7.8
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Write a short summary, because RubyGems requires one.
106
+ test_files: []