Class: RuboCop::Cop::Migration::RenameTable

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/migration/rename_table.rb

Overview

Avoid renaming tables that are in use.

It will cause errors in your application. A safer approach is to:

  1. Create a new table

  2. Write to both tables

  3. Backfill data from the old table to new table

  4. Move reads from the old table to the new table

  5. Stop writing to the old table

  6. Drop the old table

Examples:

# bad
class RenameUsersToAccouts < ActiveRecord::Migration[7.0]
  def change
    rename_table :users, :accounts
  end
end

# good
class AddAccounts < ActiveRecord::Migration[7.0]
  def change
    create_table :accounts do |t|
      t.string :name, null: false
    end
  end
end

class RemoveUsers < ActiveRecord::Migration[7.0]
  def change
    remove_table :users
  end
end

Constant Summary collapse

MSG =
'Avoid renaming tables that are in use.'
RESTRICT_ON_SEND =
i[
  rename_table
].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


52
53
54
55
56
# File 'lib/rubocop/cop/migration/rename_table.rb', line 52

def on_send(node)
  return unless bad?(node)

  add_offense(node)
end