Skip to content

Commit 4214608

Browse files
committed
[Fix #1410] Make registered cops aware of AllCops: MigratedSchemaVersion
When implementing #1383, the detection of `AllCops: MigratedSchemaVersion` was initially considered only for cops related to migrations. However, feedback received later, such as in #1410, indicated that it is also expected to apply to `Style`, `Lint`, and other categories. This suggestion is reasonable, as warnings for migrated files may not be limited to database columns but could also include Ruby programming logic. Excluding `Style` and `Lint` from this consideration would not align with this feedback. This PR modifies the behavior so that all registered cops can detect the value of `AllCops: MigratedSchemaVersion`. Fixes #1410.
1 parent b1fbd49 commit 4214608

14 files changed

+79
-38
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1410](https://github.com/rubocop/rubocop-rails/issues/1410): Make registered cops aware of `AllCops: MigratedSchemaVersion`. ([@koic][])

lib/rubocop-rails.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
require_relative 'rubocop/cop/rails_cops'
1717

18+
require_relative 'rubocop/rails/migration_file_skippable'
19+
RuboCop::Rails::MigrationFileSkippable.apply_to_cops!
20+
1821
RuboCop::Cop::Style::HashExcept.minimum_target_ruby_version(2.0)
1922

2023
RuboCop::Cop::Style::InverseMethods.singleton_class.prepend(

lib/rubocop/cop/mixin/migrations_helper.rb

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,6 @@ def in_migration?(node)
2121
migration_class?(class_node)
2222
end
2323
end
24-
25-
# rubocop:disable Style/DocumentDynamicEvalDefinition
26-
%i[on_send on_csend on_block on_numblock on_class].each do |method|
27-
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
28-
def #{method}(node)
29-
return if already_migrated_file?
30-
31-
super if method(__method__).super_method
32-
end
33-
RUBY
34-
end
35-
# rubocop:enable Style/DocumentDynamicEvalDefinition
36-
37-
private
38-
39-
def already_migrated_file?
40-
return false unless migrated_schema_version
41-
42-
match_data = File.basename(processed_source.file_path).match(/(?<timestamp>\d{14})/)
43-
schema_version = match_data['timestamp'] if match_data
44-
45-
return false unless schema_version
46-
47-
schema_version <= migrated_schema_version.to_s # Ignore applied migration files.
48-
end
49-
50-
def migrated_schema_version
51-
config.for_all_cops.fetch('MigratedSchemaVersion', nil)
52-
end
5324
end
5425
end
5526
end

lib/rubocop/cop/rails/add_column_index.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Rails
1919
class AddColumnIndex < Base
2020
extend AutoCorrector
2121
include RangeHelp
22-
prepend MigrationsHelper
22+
include MigrationsHelper
2323

2424
MSG = '`add_column` does not accept an `index` key, use `add_index` instead.'
2525
RESTRICT_ON_SEND = %i[add_column].freeze

lib/rubocop/cop/rails/bulk_change_table.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module Rails
6565
# end
6666
class BulkChangeTable < Base
6767
include DatabaseTypeResolvable
68-
prepend MigrationsHelper
68+
include MigrationsHelper
6969

7070
MSG_FOR_CHANGE_TABLE = <<~MSG.chomp
7171
You can combine alter queries using `bulk: true` options.

lib/rubocop/cop/rails/dangerous_column_names.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Rails
1414
# # good
1515
# add_column :users, :saved
1616
class DangerousColumnNames < Base # rubocop:disable Metrics/ClassLength
17-
prepend MigrationsHelper
17+
include MigrationsHelper
1818

1919
COLUMN_TYPE_METHOD_NAMES = %i[
2020
bigint

lib/rubocop/cop/rails/migration_class_name.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Rails
2020
#
2121
class MigrationClassName < Base
2222
extend AutoCorrector
23-
prepend MigrationsHelper
23+
include MigrationsHelper
2424

2525
MSG = 'Replace with `%<camelized_basename>s` that matches the file name.'
2626

lib/rubocop/cop/rails/not_null_column.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module Rails
4141
# change_column_null :products, :category_id, false
4242
class NotNullColumn < Base
4343
include DatabaseTypeResolvable
44-
prepend MigrationsHelper
44+
include MigrationsHelper
4545

4646
MSG = 'Do not add a NOT NULL column without a default value.'
4747
RESTRICT_ON_SEND = %i[add_column add_reference].freeze

lib/rubocop/cop/rails/reversible_migration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ module Rails
151151
# remove_index :users, column: :email
152152
# end
153153
class ReversibleMigration < Base
154-
prepend MigrationsHelper
154+
include MigrationsHelper
155155

156156
MSG = '%<action>s is not reversible.'
157157

lib/rubocop/cop/rails/reversible_migration_method_definition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module Rails
4343
# end
4444
# end
4545
class ReversibleMigrationMethodDefinition < Base
46-
prepend MigrationsHelper
46+
include MigrationsHelper
4747

4848
MSG = 'Migrations must contain either a `change` method, or both an `up` and a `down` method.'
4949

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy