Skip to content

Commit

Permalink
Extract DatabaseTypeResolver module from Rails/BulkChangeTable cop
Browse files Browse the repository at this point in the history
It could be reused in similar cases like #952.
  • Loading branch information
koic committed Oct 7, 2023
1 parent a4df1b9 commit cbdc126
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
66 changes: 66 additions & 0 deletions lib/rubocop/cop/mixin/database_type_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

module RuboCop
module Cop
# A mixin to extend cops in order to determine the database type.
#
# This module automatically detect an adapter from `development` environment
# in `config/database.yml` or the environment variable `DATABASE_URL`
# when the `Database` option is not set.
module DatabaseTypeResolver
MYSQL = 'mysql'
POSTGRESQL = 'postgresql'

def database
cop_config['Database'] || database_from_yaml || database_from_env
end

private

def database_from_yaml
return unless database_yaml

case database_adapter
when 'mysql2'
MYSQL
when 'postgresql'
POSTGRESQL
end
end

def database_from_env
url = ENV['DATABASE_URL'].presence
return unless url

case url
when %r{\Amysql2://}
MYSQL
when %r{\Apostgres(ql)?://}
POSTGRESQL
end
end

def database_yaml
return 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 unless yaml.is_a? Hash

config = yaml['development']
return unless config.is_a?(Hash)

config
rescue Psych::SyntaxError
# noop
end

def database_adapter
database_yaml['adapter'] || database_yaml.first.last['adapter']
end
end
end
end
54 changes: 2 additions & 52 deletions lib/rubocop/cop/rails/bulk_change_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,15 @@ module Rails
# end
# end
class BulkChangeTable < Base
include DatabaseTypeResolver

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[
Expand Down Expand Up @@ -175,55 +174,6 @@ def include_bulk_options?(node)
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
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative 'mixin/active_record_helper'
require_relative 'mixin/active_record_migrations_helper'
require_relative 'mixin/class_send_node_helper'
require_relative 'mixin/database_type_resolver'
require_relative 'mixin/enforce_superclass'
require_relative 'mixin/index_method'
require_relative 'mixin/migrations_helper'
Expand Down

0 comments on commit cbdc126

Please sign in to comment.