diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-08 21:11:09 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-08 21:11:09 +0300 |
commit | da576e4a0b8e1adc3df559a163b01c962a565ef5 (patch) | |
tree | f563a8746fd54a2d638f6b421955220af24c8489 /rubocop | |
parent | b5bdf6e5219b3b57107aee49ba7c103affb65dd9 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/migration/schema_addition_methods_no_post.rb | 35 | ||||
-rw-r--r-- | rubocop/migration_helpers.rb | 12 |
2 files changed, 47 insertions, 0 deletions
diff --git a/rubocop/cop/migration/schema_addition_methods_no_post.rb b/rubocop/cop/migration/schema_addition_methods_no_post.rb new file mode 100644 index 00000000000..5bb5bb63f0c --- /dev/null +++ b/rubocop/cop/migration/schema_addition_methods_no_post.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require_relative '../../migration_helpers' + +module RuboCop + module Cop + module Migration + # Cop that checks that no background batched migration helpers are called by regular migrations. + class SchemaAdditionMethodsNoPost < RuboCop::Cop::Base + include MigrationHelpers + + MSG = "This method may not be used in post migrations. Please see documentation here: https://docs.gitlab.com/ee/development/migration_style_guide.html#choose-an-appropriate-migration-type" + + FORBIDDEN_METHODS = %w[ + add_column + create_table + ].freeze + + SYMBOLIZED_MATCHER = FORBIDDEN_METHODS.map { |w| ":#{w}" }.join(' | ') + + def_node_matcher :on_forbidden_method, <<~PATTERN + (send nil? {#{SYMBOLIZED_MATCHER}} ...) + PATTERN + + def on_send(node) + return unless time_enforced?(node) + + on_forbidden_method(node) do + add_offense(node, message: MSG) + end + end + end + end + end +end diff --git a/rubocop/migration_helpers.rb b/rubocop/migration_helpers.rb index 16a9aa53cd3..22f3931be73 100644 --- a/rubocop/migration_helpers.rb +++ b/rubocop/migration_helpers.rb @@ -49,6 +49,14 @@ module RuboCop dirname(node).end_with?('db/post_migrate', 'db/geo/post_migrate') end + # Returns true if we've defined an 'EnforcedSince' variable in rubocop.yml and the migration version + # is greater. + def time_enforced?(node) + return false unless enforced_since + + version(node) > enforced_since + end + def version(node) File.basename(node.location.expression.source_buffer.name).split('_').first.to_i end @@ -80,5 +88,9 @@ module RuboCop def rubocop_path File.expand_path(__dir__) end + + def enforced_since + @enforced_since ||= config.for_cop(name)['EnforcedSince'] + end end end |