rubocop-sequel 0.3.5 → 0.3.6

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
2
  SHA256:
3
- metadata.gz: af5f93d560c9a6dcb0216440641d3b108a466f5f536d296f1602ab9e38cc9d9e
4
- data.tar.gz: e76e3f9bc6f6a768d500955c88e94b2b05c70486e6978317542cbf7d808e6e5b
3
+ metadata.gz: 8264c5365683296b4ca9330ef776756e3c04ac55bb1ccdb9b2e76ea42d587e95
4
+ data.tar.gz: 53417ac0da345eba715ef2ce39a7ac60dbd93a0b6a2d174400ff3928bdb8814c
5
5
  SHA512:
6
- metadata.gz: d7eb9f7f0c40b0ad3cf542fabd76d3ab506505e0d4f40cf831b6de91719186b57750e13036a600b303928cbc28ebf5d2dfec68802c692b9b3580b8943821ad2d
7
- data.tar.gz: 5cd74af9570a31c507b45fe5258234e2204cb112bbb7fb051f5531d8e08ffcfd1bdeb93d8757d9c8e77d37babc16e7e6b3dee2c3729631c205ea1cf5ef8b1d07
6
+ metadata.gz: aa8a47c34b42c304ba81f83725298b2e7ed6ba5c5da8432c5869e10f22e728fe3009132e1d7a144492f95e509908cbd3378d055d1bb9ff164c58363794df9210
7
+ data.tar.gz: a664d80d57e0477053dd8aaf45dc052c306714e610c7f312e1f656b20f0a11d7f9e17394ec63a9bf61353214f10a3d97b0059d826f85ef01399777bfc4225ed0
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  .tags
2
2
  *.gem
3
3
  Gemfile.lock
4
+ .DS_Store
data/config/default.yml CHANGED
@@ -16,8 +16,8 @@ Sequel/IrreversibleMigration:
16
16
  Warns against using certain methods inside a migration file's `change` block that would cause the migration to become irreversible.
17
17
  Reference: https://www.rubydoc.info/gems/rubocop-sequel/RuboCop/Cop/Sequel/IrreversibleMigration
18
18
  Enabled: true
19
- VersionAdded: 0.3.4
20
- VersionChanged: 0.3.4
19
+ VersionAdded: 0.3.5
20
+ VersionChanged: 0.3.6
21
21
 
22
22
  Sequel/JSONColumn:
23
23
  Description: >-
@@ -5,6 +5,8 @@ module RuboCop
5
5
  module Sequel
6
6
  # ConcurrentIndex looks for non-concurrent index creation.
7
7
  class ConcurrentIndex < Base
8
+ include Helpers::Migration
9
+
8
10
  MSG = 'Specify `concurrently` option when creating or dropping an index.'
9
11
  RESTRICT_ON_SEND = %i[add_index drop_index].freeze
10
12
 
@@ -13,6 +15,8 @@ module RuboCop
13
15
  MATCHER
14
16
 
15
17
  def on_send(node)
18
+ return unless within_sequel_migration?(node)
19
+
16
20
  indexes?(node) do |args|
17
21
  add_offense(node.loc.selector, message: MSG) if offensive?(args)
18
22
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sequel
6
+ module Helpers
7
+ # Migration contains helper methods for detecting if a node is inside a `Sequel.migration` block
8
+ module Migration
9
+ extend NodePattern::Macros
10
+
11
+ def_node_matcher :sequel_migration_block?, <<~MATCHER
12
+ (block
13
+ (send
14
+ (const nil? :Sequel) :migration ...)
15
+ ...)
16
+ MATCHER
17
+
18
+ def within_sequel_migration?(node)
19
+ node.each_ancestor(:block).any? { |ancestor| sequel_migration_block?(ancestor) }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -5,6 +5,8 @@ module RuboCop
5
5
  module Sequel
6
6
  # IrreversibleMigration looks for methods inside a `change` block that cannot be reversed.
7
7
  class IrreversibleMigration < Base
8
+ include Helpers::Migration
9
+
8
10
  # https://sequel.jeremyevans.net/rdoc/files/doc/migration_rdoc.html#label-A+Basic+Migration
9
11
  VALID_CHANGE_METHODS = %i[
10
12
  create_table
@@ -26,11 +28,12 @@ module RuboCop
26
28
  set_column_allow_null
27
29
  ].freeze
28
30
 
29
- MSG = 'Avoid using `%<name>s` inside a `change` block. Use `up` & `down` blocks instead.'
30
- PRIMARY_KEY_MSG = 'Avoid using `add_primary_key` with an array argument inside a `change` block.'
31
+ MSG = 'Avoid using "%<name>s" inside a "change" block. Use "up" & "down" blocks instead.'
32
+ PRIMARY_KEY_MSG = 'Avoid using "add_primary_key" with an array argument inside a "change" block.'
31
33
 
32
34
  def on_block(node)
33
35
  return unless node.method_name == :change
36
+ return unless within_sequel_migration?(node)
34
37
 
35
38
  body = node.body
36
39
  return unless body
@@ -5,6 +5,8 @@ module RuboCop
5
5
  module Sequel
6
6
  # JSONColumn looks for non-JSONB columns.
7
7
  class JSONColumn < Base
8
+ include Helpers::Migration
9
+
8
10
  MSG = 'Use JSONB rather than JSON or hstore'
9
11
  RESTRICT_ON_SEND = %i[add_column].freeze
10
12
 
@@ -22,12 +24,14 @@ module RuboCop
22
24
 
23
25
  def on_send(node)
24
26
  return unless json_or_hstore?(node)
27
+ return unless within_sequel_migration?(node)
25
28
 
26
29
  add_offense(node.loc.selector, message: MSG)
27
30
  end
28
31
 
29
32
  def on_block(node)
30
33
  return unless node.send_node.method_name == :create_table
34
+ return unless within_sequel_migration?(node)
31
35
 
32
36
  node.each_node(:send) do |method|
33
37
  next unless column_method?(method) || column_type?(method)
@@ -5,6 +5,8 @@ module RuboCop
5
5
  module Sequel
6
6
  # PartialConstraint looks for missed usage of partial indexes.
7
7
  class PartialConstraint < Base
8
+ include Helpers::Migration
9
+
8
10
  MSG = "Constraint can't be partial, use where argument with index"
9
11
  RESTRICT_ON_SEND = %i[add_unique_constraint].freeze
10
12
 
@@ -14,6 +16,7 @@ module RuboCop
14
16
 
15
17
  def on_send(node)
16
18
  return unless add_partial_constraint?(node)
19
+ return unless within_sequel_migration?(node)
17
20
 
18
21
  add_offense(node.loc.selector, message: MSG)
19
22
  end
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Sequel
5
5
  # This module holds the RuboCop Sequel version information.
6
6
  module Version
7
- STRING = '0.3.5'
7
+ STRING = '0.3.6'
8
8
  end
9
9
  end
10
10
  end
@@ -7,6 +7,8 @@ require 'rubocop/sequel/inject'
7
7
 
8
8
  RuboCop::Sequel::Inject.defaults!
9
9
 
10
+ require_relative 'rubocop/cop/sequel/helpers/migration'
11
+
10
12
  require 'rubocop/cop/sequel/concurrent_index'
11
13
  require 'rubocop/cop/sequel/irreversible_migration'
12
14
  require 'rubocop/cop/sequel/json_column'
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
12
12
  gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
13
13
  gem.name = 'rubocop-sequel'
14
14
  gem.require_paths = ['lib']
15
- gem.version = '0.3.5'
15
+ gem.version = '0.3.6'
16
16
  gem.metadata['rubygems_mfa_required'] = 'true'
17
17
 
18
18
  gem.required_ruby_version = '>= 2.5'
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.describe RuboCop::Cop::Sequel::ConcurrentIndex do
4
+ include Spec::Helpers::Migration
5
+
4
6
  subject(:cop) { described_class.new }
5
7
 
6
8
  context 'without the concurrent option' do
7
9
  it 'registers an offense without options' do
8
- offenses = inspect_source(<<~SOURCE)
10
+ offenses = inspect_source_within_migration(<<~SOURCE)
9
11
  add_index(:products, :name)
10
12
  drop_index(:products, :name)
11
13
  SOURCE
@@ -13,7 +15,7 @@ RSpec.describe RuboCop::Cop::Sequel::ConcurrentIndex do
13
15
  end
14
16
 
15
17
  it 'registers an offense with other options' do
16
- offenses = inspect_source(<<~SOURCE)
18
+ offenses = inspect_source_within_migration(<<~SOURCE)
17
19
  add_index(:products, :name, unique: true)
18
20
  drop_index(:products, :name, unique: true)
19
21
  SOURCE
@@ -21,7 +23,7 @@ RSpec.describe RuboCop::Cop::Sequel::ConcurrentIndex do
21
23
  end
22
24
 
23
25
  it 'registers an offense with composite index' do
24
- offenses = inspect_source(<<~SOURCE)
26
+ offenses = inspect_source_within_migration(<<~SOURCE)
25
27
  add_index(:products, [:name, :price], unique: true)
26
28
  drop_index(:products, [:name, :price])
27
29
  SOURCE
@@ -30,7 +32,7 @@ RSpec.describe RuboCop::Cop::Sequel::ConcurrentIndex do
30
32
  end
31
33
 
32
34
  it 'does not register an offense when using concurrent option' do
33
- offenses = inspect_source(<<~SOURCE)
35
+ offenses = inspect_source_within_migration(<<~SOURCE)
34
36
  add_index(:products, :name, unique: true, concurrently: true)
35
37
  drop_index(:products, :name, concurrently: true)
36
38
  SOURCE
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spec
4
+ module Helpers
5
+ module Migration
6
+ extend CopHelper
7
+
8
+ def inspect_source_within_migration(source, file = nil)
9
+ migration_wrapped_source = <<~SOURCE
10
+ Sequel.migration do
11
+ #{source}
12
+ end
13
+ SOURCE
14
+
15
+ inspect_source(migration_wrapped_source, file)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.describe RuboCop::Cop::Sequel::IrreversibleMigration do
4
+ include Spec::Helpers::Migration
5
+
4
6
  subject(:cop) { described_class.new }
5
7
 
6
8
  context 'when inside a change block' do
@@ -28,12 +30,12 @@ RSpec.describe RuboCop::Cop::Sequel::IrreversibleMigration do
28
30
  end
29
31
 
30
32
  it 'registers an offense when there is an invalid method' do
31
- offenses = inspect_source(invalid_source)
33
+ offenses = inspect_source_within_migration(invalid_source)
32
34
  expect(offenses.size).to eq(2)
33
35
  end
34
36
 
35
37
  it 'does not register an offense with valid methods' do
36
- offenses = inspect_source(valid_source)
38
+ offenses = inspect_source_within_migration(valid_source)
37
39
  expect(offenses).to be_empty
38
40
  end
39
41
 
@@ -49,7 +51,7 @@ RSpec.describe RuboCop::Cop::Sequel::IrreversibleMigration do
49
51
  end
50
52
 
51
53
  it 'registers an offense' do
52
- offenses = inspect_source(source)
54
+ offenses = inspect_source_within_migration(source)
53
55
  expect(offenses.size).to eq(1)
54
56
  end
55
57
  end
@@ -68,6 +70,19 @@ RSpec.describe RuboCop::Cop::Sequel::IrreversibleMigration do
68
70
  SOURCE
69
71
  end
70
72
 
73
+ it 'does not register an offense with any methods' do
74
+ offenses = inspect_source_within_migration(source)
75
+ expect(offenses).to be_empty
76
+ end
77
+ end
78
+
79
+ context 'when a change block is used outside of a Sequel migration' do
80
+ let(:source) do
81
+ <<~SOURCE
82
+ it { expect { subject }.to change { document_count(user_id) }.by(-1) }
83
+ SOURCE
84
+ end
85
+
71
86
  it 'does not register an offense with any methods' do
72
87
  offenses = inspect_source(source)
73
88
  expect(offenses).to be_empty
@@ -1,48 +1,50 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.describe RuboCop::Cop::Sequel::JSONColumn do
4
+ include Spec::Helpers::Migration
5
+
4
6
  subject(:cop) { described_class.new }
5
7
 
6
8
  context 'with add_column' do
7
9
  it 'registers an offense when using json type' do
8
- offenses = inspect_source('add_column(:products, :type, :json)')
10
+ offenses = inspect_source_within_migration('add_column(:products, :type, :json)')
9
11
  expect(offenses.size).to eq(1)
10
12
  end
11
13
 
12
14
  it 'registers an offense when using hstore type' do
13
- offenses = inspect_source('add_column(:products, :type, :hstore)')
15
+ offenses = inspect_source_within_migration('add_column(:products, :type, :hstore)')
14
16
  expect(offenses.size).to eq(1)
15
17
  end
16
18
 
17
19
  it 'does not register an offense when using jsonb' do
18
- offenses = inspect_source('add_column(:products, :type, :jsonb)')
20
+ offenses = inspect_source_within_migration('add_column(:products, :type, :jsonb)')
19
21
  expect(offenses).to be_empty
20
22
  end
21
23
  end
22
24
 
23
25
  context 'with create_table' do
24
26
  it 'registers an offense when using json as a method' do
25
- offenses = inspect_source('create_table(:products) { json :type, default: {} }')
27
+ offenses = inspect_source_within_migration('create_table(:products) { json :type, default: {} }')
26
28
  expect(offenses.size).to eq(1)
27
29
  end
28
30
 
29
31
  it 'registers an offense when using the column method with hstore' do
30
- offenses = inspect_source('create_table(:products) { column :type, :hstore }')
32
+ offenses = inspect_source_within_migration('create_table(:products) { column :type, :hstore }')
31
33
  expect(offenses.size).to eq(1)
32
34
  end
33
35
 
34
36
  it 'does not register an offense when using jsonb as column type`' do
35
- offenses = inspect_source('create_table(:products) { column :type, :jsonb }')
37
+ offenses = inspect_source_within_migration('create_table(:products) { column :type, :jsonb }')
36
38
  expect(offenses).to be_empty
37
39
  end
38
40
 
39
41
  it 'does not register an offense when using jsonb' do
40
- offenses = inspect_source('create_table(:products) { jsonb :type }')
42
+ offenses = inspect_source_within_migration('create_table(:products) { jsonb :type }')
41
43
  expect(offenses).to be_empty
42
44
  end
43
45
 
44
46
  it 'does not register an offense when using a simple type' do
45
- offenses = inspect_source('create_table(:products) { integer :type, default: 0 }')
47
+ offenses = inspect_source_within_migration('create_table(:products) { integer :type, default: 0 }')
46
48
  expect(offenses).to be_empty
47
49
  end
48
50
  end
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.describe RuboCop::Cop::Sequel::PartialConstraint do
4
+ include Spec::Helpers::Migration
5
+
4
6
  subject(:cop) { described_class.new }
5
7
 
6
8
  it 'registers an offense when using where for constraint' do
7
- offenses = inspect_source(<<~RUBY)
9
+ offenses = inspect_source_within_migration(<<~RUBY)
8
10
  add_unique_constraint %i[col_1 col_2], where: "state != 'deleted'"
9
11
  RUBY
10
12
 
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,8 @@ require 'rubocop'
4
4
  require 'rubocop/rspec/support'
5
5
  require 'rubocop-sequel'
6
6
 
7
+ require_relative 'rubocop/cop/sequel/helpers/migration'
8
+
7
9
  RSpec.configure do |config|
8
10
  config.include RuboCop::RSpec::ExpectOffense
9
11
  config.disable_monkey_patching!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timothée Peignier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-23 00:00:00.000000000 Z
11
+ date: 2024-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -43,6 +43,7 @@ files:
43
43
  - config/obsoletion.yml
44
44
  - lib/rubocop-sequel.rb
45
45
  - lib/rubocop/cop/sequel/concurrent_index.rb
46
+ - lib/rubocop/cop/sequel/helpers/migration.rb
46
47
  - lib/rubocop/cop/sequel/irreversible_migration.rb
47
48
  - lib/rubocop/cop/sequel/json_column.rb
48
49
  - lib/rubocop/cop/sequel/migration_name.rb
@@ -54,6 +55,7 @@ files:
54
55
  - rubocop-sequel.gemspec
55
56
  - spec/project_spec.rb
56
57
  - spec/rubocop/cop/sequel/concurrent_index_spec.rb
58
+ - spec/rubocop/cop/sequel/helpers/migration.rb
57
59
  - spec/rubocop/cop/sequel/irreversible_migration_spec.rb
58
60
  - spec/rubocop/cop/sequel/json_column_spec.rb
59
61
  - spec/rubocop/cop/sequel/migration_name_spec.rb