lib/rubocop/cop/rails/bulk_change_table.rb in rubocop-rails-2.20.2 vs lib/rubocop/cop/rails/bulk_change_table.rb in rubocop-rails-2.21.0
- old
+ new
@@ -10,11 +10,12 @@
# 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.
+ # 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`,
# this Cop ignores offenses.
#
# @example
# # bad
@@ -173,24 +174,28 @@
options.hash_type? && options.keys.any? { |key| key.sym_type? && key.value == :bulk }
end
def database
- cop_config['Database'] || database_from_yaml
+ cop_config['Database'] || database_from_yaml || database_from_env
end
def database_from_yaml
return nil unless database_yaml
- case database_yaml['adapter']
+ 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')
@@ -203,9 +208,21 @@
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