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

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

Overview

This Cop 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` when the `Database` option is not set. If the adapter is not `mysql2` or `postgresql`, 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

See Also:

Defined Under Namespace

Classes: AlterMethodsRecorder

Constant Summary collapse

MSG_FOR_CHANGE_TABLE =
<<-MSG.strip_indent.chomp
  You can combine alter queries using `bulk: true` options.
MSG
MSG_FOR_ALTER_METHODS =
<<-MSG.strip_indent.chomp
  You can use `change_table :%<table>s, bulk: true` to combine alter queries.
MSG
MYSQL =
'mysql'.freeze
POSTGRESQL =
'postgresql'.freeze
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_ALTER_METHODS =
%i[
  change_column_default
  change_column_null
].freeze

Constants included from Util

Util::LITERAL_REGEX

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offenses, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offense, all, autocorrect_incompatible_with, badge, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, #correct, department, #duplicate_location?, #excluded_file?, #find_location, #highlights, inherited, #initialize, #join_force?, lint?, match?, #message, #messages, non_rails, #parse, qualified_cop_name, #relevant_file?, #target_rails_version, #target_ruby_version

Methods included from AST::Sexp

#s

Methods included from NodePattern::Macros

#def_node_matcher, #def_node_search, #node_search, #node_search_all, #node_search_body, #node_search_first

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #support_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

begins_its_line?, comment_line?, double_quotes_required?, escape_string, first_part_of_call_chain, interpret_string_escapes, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, tokens

Methods included from PathUtil

absolute?, hidden_dir?, hidden_file_in_not_hidden_dir?, match_path?, pwd, relative_path, reset_pwd, smart_path

Constructor Details

This class inherits a constructor from RuboCop::Cop::Cop

Instance Method Details

#on_def(node) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rubocop/cop/rails/bulk_change_table.rb', line 135

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.each_child_node(:send) do |send_node|
    if combinable_alter_methods.include?(send_node.method_name)
      recorder.process(send_node)
    else
      recorder.flush
    end
  end

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

#on_send(node) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rubocop/cop/rails/bulk_change_table.rb', line 153

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

  send_nodes = node.block_node.body.each_child_node(:send).to_a

  transformations = send_nodes.select do |send_node|
    combinable_transformations.include?(send_node.method_name)
  end

  add_offense_for_change_table(node) if transformations.size > 1
end