Class: RuboCop::Cop::Rails::ReversibleMigration

Inherits:
Base
  • Object
show all
Includes:
MigrationsHelper
Defined in:
lib/rubocop/cop/rails/reversible_migration.rb

Overview

Checks whether the change method of the migration file is reversible.

Examples:

# bad
def change
  change_table :users do |t|
    t.remove :name
  end
end

# good
def change
  change_table :users do |t|
    t.remove :name, type: :string
  end
end

# good
def change
  create_table :users do |t|
    t.string :name
  end
end
# drop_table

# bad
def change
  drop_table :users
end

# good
def change
  drop_table :users do |t|
    t.string :name
  end
end
# change_column_default

# bad
def change
  change_column_default(:suppliers, :qualification, 'new')
end

# good
def change
  change_column_default(:posts, :state, from: nil, to: "draft")
end
# remove_column

# bad
def change
  remove_column(:suppliers, :qualification)
end

# good
def change
  remove_column(:suppliers, :qualification, :string)
end
# remove_foreign_key

# bad
def change
  remove_foreign_key :accounts, column: :owner_id
end

# good
def change
  remove_foreign_key :accounts, :branches
end

# good
def change
  remove_foreign_key :accounts, to_table: :branches
end
# change_table

# bad
def change
  change_table :users do |t|
    t.remove :name
    t.change_default :authorized, 1
    t.change :price, :string
  end
end

# good
def change
  change_table :users do |t|
    t.string :name
  end
end
# remove_columns

# bad
def change
  remove_columns :users, :name, :email
end

# good
def change
  reversible do |dir|
    dir.up do
      remove_columns :users, :name, :email
    end

    dir.down do
      add_column :users, :name, :string
      add_column :users, :email, :string
    end
  end
end

# good (Rails >= 6.1, see https://github.com/rails/rails/pull/36589)
def change
  remove_columns :users, :name, :email, type: :string
end
# remove_index

# bad
def change
  remove_index :users, name: :index_users_on_email
end

# good
def change
  remove_index :users, :email
end

# good
def change
  remove_index :users, column: :email
end

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from MigrationsHelper

#in_migration?

Instance Method Details

#on_block(node) ⇒ Object Also known as: on_numblock



199
200
201
202
203
204
205
# File 'lib/rubocop/cop/rails/reversible_migration.rb', line 199

def on_block(node)
  return unless in_migration?(node) && within_change_method?(node)
  return if within_reversible_or_up_only_block?(node)
  return if node.body.nil?

  check_change_table_node(node.send_node, node.body)
end

#on_send(node) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rubocop/cop/rails/reversible_migration.rb', line 186

def on_send(node)
  return unless in_migration?(node) && within_change_method?(node)
  return if within_reversible_or_up_only_block?(node)

  check_irreversible_schema_statement_node(node)
  check_drop_table_node(node)
  check_reversible_hash_node(node)
  check_remove_column_node(node)
  check_remove_foreign_key_node(node)
  check_remove_columns_node(node)
  check_remove_index_node(node)
end