Sha256: 196a2905d937794fee75af212673e52750c24f9d39aca3a7629a18dbdce2a7ed

Contents?: true

Size: 1.89 KB

Versions: 11

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop checks whether the migration implements
      # either a `change` method or both an `up` and a `down`
      # method.
      #
      # @example
      #   # bad
      #   class SomeMigration < ActiveRecord::Migration[6.0]
      #     def up
      #       # up migration
      #     end
      #
      #     # <----- missing down method
      #   end
      #
      #   class SomeMigration < ActiveRecord::Migration[6.0]
      #     # <----- missing up method
      #
      #     def down
      #       # down migration
      #     end
      #   end
      #
      #   # good
      #   class SomeMigration < ActiveRecord::Migration[6.0]
      #     def change
      #       # reversible migration
      #     end
      #   end
      #
      #   # good
      #   class SomeMigration < ActiveRecord::Migration[6.0]
      #     def up
      #       # up migration
      #     end
      #
      #     def down
      #       # down migration
      #     end
      #   end
      class ReversibleMigrationMethodDefinition < Base
        MSG = 'Migrations must contain either a `change` method, or ' \
              'both an `up` and a `down` method.'

        def_node_matcher :migration_class?, <<~PATTERN
          (class
            (const nil? _)
            (send
              (const (const {nil? cbase} :ActiveRecord) :Migration)
              :[]
              (float _))
            _)
        PATTERN

        def_node_matcher :change_method?, <<~PATTERN
          [ #migration_class? `(def :change (args) _) ]
        PATTERN

        def_node_matcher :up_and_down_methods?, <<~PATTERN
          [ #migration_class? `(def :up (args) _) `(def :down (args) _) ]
        PATTERN

        def on_class(node)
          return if change_method?(node) || up_and_down_methods?(node)

          add_offense(node)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rubocop-rails-2.13.1 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.13.0 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.12.4 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.12.3 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.12.2 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.12.1 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.12.0 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.11.3 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.11.2 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.11.1 lib/rubocop/cop/rails/reversible_migration_method_definition.rb
rubocop-rails-2.11.0 lib/rubocop/cop/rails/reversible_migration_method_definition.rb