lib/rubocop/cop/rails/bulk_change_table.rb in rubocop-rails-2.21.2 vs lib/rubocop/cop/rails/bulk_change_table.rb in rubocop-rails-2.22.0
- old
+ new
@@ -12,11 +12,11 @@
# 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` or `postgresql`,
+ # If the adapter is not `mysql2`, `trilogy`, or `postgresql`,
# this Cop ignores offenses.
#
# @example
# # bad
# def change
@@ -62,20 +62,19 @@
# t.string :name, null: false
# t.string :nickname
# end
# end
class BulkChangeTable < Base
+ include DatabaseTypeResolvable
+
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
- MYSQL = 'mysql'
- POSTGRESQL = 'postgresql'
-
MIGRATION_METHODS = %i[change up down].freeze
COMBINABLE_TRANSFORMATIONS = %i[
primary_key
column
@@ -171,58 +170,9 @@
# arguments: [{(sym :table)(str "table")} (hash (pair (sym :bulk) _))]
options = node.arguments[1]
return false unless options
options.hash_type? && options.keys.any? { |key| key.sym_type? && key.value == :bulk }
- end
-
- def database
- cop_config['Database'] || database_from_yaml || database_from_env
- end
-
- def database_from_yaml
- return nil unless database_yaml
-
- case database_adapter
- when 'mysql2'
- MYSQL
- when 'postgresql'
- POSTGRESQL
- end
- end
-
- def database_adapter
- database_yaml['adapter'] || database_yaml.first.last['adapter']
- end
-
- def database_yaml
- return nil unless File.exist?('config/database.yml')
-
- yaml = if YAML.respond_to?(:unsafe_load_file)
- YAML.unsafe_load_file('config/database.yml')
- else
- YAML.load_file('config/database.yml')
- end
- return nil unless yaml.is_a? Hash
-
- config = yaml['development']
- return nil unless config.is_a?(Hash)
-
- config
- rescue Psych::SyntaxError
- nil
- end
-
- def database_from_env
- url = ENV['DATABASE_URL'].presence
- return nil unless url
-
- case url
- when %r{\Amysql2://}
- MYSQL
- when %r{\Apostgres(ql)?://}
- POSTGRESQL
- end
end
def support_bulk_alter?
case database
when MYSQL