rubocop-sequel 0.3.6 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8264c5365683296b4ca9330ef776756e3c04ac55bb1ccdb9b2e76ea42d587e95
4
- data.tar.gz: 53417ac0da345eba715ef2ce39a7ac60dbd93a0b6a2d174400ff3928bdb8814c
3
+ metadata.gz: 43689260646cb1428cd6629924d01648a005939d8f1094a6fcbb88ecc1a7fc68
4
+ data.tar.gz: b95f6400f9d2857ac7ce9d32c8931a71db14ba6e8cd86e128289fe1ef836d95b
5
5
  SHA512:
6
- metadata.gz: aa8a47c34b42c304ba81f83725298b2e7ed6ba5c5da8432c5869e10f22e728fe3009132e1d7a144492f95e509908cbd3378d055d1bb9ff164c58363794df9210
7
- data.tar.gz: a664d80d57e0477053dd8aaf45dc052c306714e610c7f312e1f656b20f0a11d7f9e17394ec63a9bf61353214f10a3d97b0059d826f85ef01399777bfc4225ed0
6
+ metadata.gz: a78387c550cc09564f2fffee05a5dda77c370130dd41f9c8b8a0f8bb26683813b97c1c2540250574c95ae7a192743d1873f59b82cdf02de501ff1d679a029bb5
7
+ data.tar.gz: 2e91b8df57f918c221d2d2319037264d347b84eaa082ed6f378d85f5023f9b6bcf19a5b2854553cc56707e986551c9bf5dd2ff1f17aaa1e04a3e5bd7c94221a9
data/config/default.yml CHANGED
@@ -17,7 +17,7 @@ Sequel/IrreversibleMigration:
17
17
  Reference: https://www.rubydoc.info/gems/rubocop-sequel/RuboCop/Cop/Sequel/IrreversibleMigration
18
18
  Enabled: true
19
19
  VersionAdded: 0.3.5
20
- VersionChanged: 0.3.6
20
+ VersionChanged: 0.3.8
21
21
 
22
22
  Sequel/JSONColumn:
23
23
  Description: >-
@@ -28,7 +28,7 @@ module RuboCop
28
28
  set_column_allow_null
29
29
  ].freeze
30
30
 
31
- MSG = 'Avoid using "%<name>s" inside a "change" block. Use "up" & "down" blocks instead.'
31
+ MSG = 'Using "%<name>s" inside a "change" block may cause an irreversible migration. Use "up" & "down" instead.'
32
32
  PRIMARY_KEY_MSG = 'Avoid using "add_primary_key" with an array argument inside a "change" block.'
33
33
 
34
34
  def on_block(node)
@@ -44,15 +44,35 @@ module RuboCop
44
44
  private
45
45
 
46
46
  def validate_node(node)
47
- name = node.method_name
47
+ return if within_create_table_block?(node)
48
48
 
49
- add_offense(node.loc.selector, message: format(MSG, name: name)) unless VALID_CHANGE_METHODS.include?(name)
49
+ return if part_of_method_call?(node)
50
50
 
51
- return unless name == :add_primary_key
51
+ add_offense(node.loc.selector, message: format(MSG, name: node.method_name)) unless valid_change_method?(node)
52
52
 
53
- return unless node.arguments.any?(&:array_type?)
53
+ add_offense(node.loc.selector, message: PRIMARY_KEY_MSG) if invalid_primary_key_method?(node)
54
+ end
55
+
56
+ def valid_change_method?(node)
57
+ VALID_CHANGE_METHODS.include?(node.method_name)
58
+ end
59
+
60
+ def invalid_primary_key_method?(node)
61
+ return false unless node.method_name == :add_primary_key
62
+
63
+ node.arguments.any?(&:array_type?)
64
+ end
65
+
66
+ def within_create_table_block?(node)
67
+ return true if node.method_name == :create_table
68
+
69
+ node.each_ancestor(:block).any? do |ancestor|
70
+ ancestor.method_name == :create_table
71
+ end
72
+ end
54
73
 
55
- add_offense(node.loc.selector, message: PRIMARY_KEY_MSG)
74
+ def part_of_method_call?(node)
75
+ node.each_ancestor(:send).count.positive?
56
76
  end
57
77
  end
58
78
  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.6'
7
+ STRING = '0.3.8'
8
8
  end
9
9
  end
10
10
  end
@@ -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.6'
15
+ gem.version = '0.3.8'
16
16
  gem.metadata['rubygems_mfa_required'] = 'true'
17
17
 
18
18
  gem.required_ruby_version = '>= 2.5'
@@ -39,6 +39,54 @@ RSpec.describe RuboCop::Cop::Sequel::IrreversibleMigration do
39
39
  expect(offenses).to be_empty
40
40
  end
41
41
 
42
+ describe 'and using a create_table block' do
43
+ let(:source) do
44
+ <<~SOURCE
45
+ change do
46
+ create_table(:artists) do
47
+ primary_key :id
48
+ String :name, null: false
49
+ end
50
+ end
51
+ SOURCE
52
+ end
53
+
54
+ it 'does not register any offenses' do
55
+ offenses = inspect_source_within_migration(source)
56
+ expect(offenses).to be_empty
57
+ end
58
+ end
59
+
60
+ describe 'and using a create_table block and alter_table block' do
61
+ let(:source) do
62
+ <<~SOURCE
63
+ change do
64
+ alter_table(:stores) do
65
+ drop_column(:products, :name)
66
+ drop_index(:products, :price)
67
+ end
68
+
69
+ add_column :books, :name, String
70
+
71
+ create_table(:artists) do
72
+ primary_key :id
73
+ String :name, null: false
74
+ end
75
+ end
76
+ SOURCE
77
+ end
78
+ let(:expected_methods) { %w[drop_column drop_index] }
79
+
80
+ it 'only registers offenses from within alter_table block' do
81
+ messages = inspect_source_within_migration(source).map(&:message)
82
+ expected_methods_present = expected_methods.all? do |method|
83
+ messages.any? { |message| message.include?(method) }
84
+ end
85
+
86
+ expect(expected_methods_present).to be(true)
87
+ end
88
+ end
89
+
42
90
  describe 'and an array is passed into `add_primary_key`' do
43
91
  let(:source) do
44
92
  <<~SOURCE
@@ -55,6 +103,52 @@ RSpec.describe RuboCop::Cop::Sequel::IrreversibleMigration do
55
103
  expect(offenses.size).to eq(1)
56
104
  end
57
105
  end
106
+
107
+ describe 'and a method is used within an argument' do
108
+ let(:source) do
109
+ <<~SOURCE
110
+ change do
111
+ alter_table(:stores) do
112
+ add_column(:products, JSON, null: false, default: Sequel.pg_json({}))
113
+ add_constraint(
114
+ :only_one_user,
115
+ (
116
+ Sequel.cast(Sequel.~(user_id: nil), Integer) +
117
+ Sequel.cast(Sequel.~(owner_id: nil), Integer)
118
+ ) => 1,
119
+ )
120
+ end
121
+ end
122
+ SOURCE
123
+ end
124
+
125
+ it 'does not register an offense with valid methods' do
126
+ offenses = inspect_source_within_migration(source)
127
+ expect(offenses).to be_empty
128
+ end
129
+ end
130
+
131
+ describe 'and an invalid change method contains another invalid change method as an argument' do
132
+ let(:source) do
133
+ <<~SOURCE
134
+ change do
135
+ alter_table(:stores) do
136
+ drop_column(:products, JSON, null: false, default: Sequel.pg_json({}))
137
+ end
138
+ end
139
+ SOURCE
140
+ end
141
+
142
+ it 'only registers 1 offense' do
143
+ offenses = inspect_source_within_migration(source)
144
+ expect(offenses.size).to eq(1)
145
+ end
146
+
147
+ it 'only registers an offense for the parent method' do
148
+ offenses = inspect_source_within_migration(source)
149
+ expect(offenses.first.message).to include('drop_column')
150
+ end
151
+ end
58
152
  end
59
153
 
60
154
  context 'when inside an up block' do
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.6
4
+ version: 0.3.8
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-25 00:00:00.000000000 Z
11
+ date: 2024-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop