Class: RuboCop::Cop::Rails::BulkChangeTable

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

Overview

Checks whether alter queries are combinable. If combinable queries are detected, it suggests to you to use ‘change_table` with `bulk: true` instead. This option causes the migration to generate a single ALTER TABLE statement combining multiple column alterations.

The ‘bulk` option is only supported on the MySQL and the PostgreSQL (5.2 later) adapter; thus it will automatically detect an adapter from `development` environment in `config/database.yml` or the environment variable `DATABASE_URL` when the `Database` option is not set. If the adapter is not `mysql2`, `trilogy`, `postgresql`, or `postgis`, this Cop ignores offenses.

Examples:

# bad
def change
  add_column :users, :name, :string, null: false
  add_column :users, :nickname, :string

  # ALTER TABLE `users` ADD `name` varchar(255) NOT NULL
  # ALTER TABLE `users` ADD `nickname` varchar(255)
end

# good
def change
  change_table :users, bulk: true do |t|
    t.string :name, null: false
    t.string :nickname
  end

  # ALTER TABLE `users` ADD `name` varchar(255) NOT NULL,
  #                     ADD `nickname` varchar(255)
end
# bad
def change
  change_table :users do |t|
    t.string :name, null: false
    t.string :nickname
  end
end

# good
def change
  change_table :users, bulk: true do |t|
    t.string :name, null: false
    t.string :nickname
  end
end

# good
# When you don't want to combine alter queries.
def change
  change_table :users, bulk: false do |t|
    t.string :name, null: false
    t.string :nickname
  end
end

Defined Under Namespace

Classes: AlterMethodsRecorder

Constant Summary collapse

MSG_FOR_CHANGE_TABLE =
<<~MSG.chomp
  You can combine alter queries using `bulk: true` options.
MSG
MSG_FOR_ALTER_METHODS =
<<~MSG.chomp
  You can use `change_table :%<table>s, bulk: true` to combine alter queries.
MSG
MIGRATION_METHODS =
%i[change up down].freeze
COMBINABLE_TRANSFORMATIONS =
%i[
  primary_key
  column
  string
  text
  integer
  bigint
  float
  decimal
  numeric
  datetime
  timestamp
  time
  date
  binary
  boolean
  json
  virtual
  remove
  change
  timestamps
  remove_timestamps
].freeze
COMBINABLE_ALTER_METHODS =
%i[
  add_column
  remove_column
  remove_columns
  change_column
  add_timestamps
  remove_timestamps
].freeze
MYSQL_COMBINABLE_TRANSFORMATIONS =
%i[rename index remove_index].freeze
MYSQL_COMBINABLE_ALTER_METHODS =
%i[rename_column add_index remove_index].freeze
POSTGRESQL_COMBINABLE_TRANSFORMATIONS =
%i[change_default].freeze
POSTGRESQL_COMBINABLE_TRANSFORMATIONS_SINCE_6_1 =
%i[change_null].freeze
POSTGRESQL_COMBINABLE_ALTER_METHODS =
%i[change_column_default].freeze
POSTGRESQL_COMBINABLE_ALTER_METHODS_SINCE_6_1 =
%i[change_column_null].freeze

Constants included from DatabaseTypeResolvable

DatabaseTypeResolvable::MYSQL, DatabaseTypeResolvable::POSTGRESQL

Instance Method Summary collapse

Methods included from DatabaseTypeResolvable

#database

Instance Method Details

#on_def(node) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rubocop/cop/rails/bulk_change_table.rb', line 121

def on_def(node)
  return unless support_bulk_alter?
  return unless MIGRATION_METHODS.include?(node.method_name)
  return unless node.body

  recorder = AlterMethodsRecorder.new

  node.body.child_nodes.each do |child_node|
    if call_to_combinable_alter_method? child_node
      recorder.process(child_node)
    else
      recorder.flush
    end
  end

  recorder.offensive_nodes.each { |n| add_offense_for_alter_methods(n) }
end

#on_send(node) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/rubocop/cop/rails/bulk_change_table.rb', line 139

def on_send(node)
  return unless support_bulk_alter?
  return unless node.command?(:change_table)
  return if include_bulk_options?(node)
  return unless (body = node.block_node&.body)

  send_nodes = send_nodes_from_change_table_block(body)

  add_offense_for_change_table(node) if count_transformations(send_nodes) > 1
end