Skip to content

Commit cbdc126

Browse files
committed
Extract DatabaseTypeResolver module from Rails/BulkChangeTable cop
It could be reused in similar cases like #952.
1 parent a4df1b9 commit cbdc126

File tree

3 files changed

+69
-52
lines changed

3 files changed

+69
-52
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
# A mixin to extend cops in order to determine the database type.
6+
#
7+
# This module automatically detect an adapter from `development` environment
8+
# in `config/database.yml` or the environment variable `DATABASE_URL`
9+
# when the `Database` option is not set.
10+
module DatabaseTypeResolver
11+
MYSQL = 'mysql'
12+
POSTGRESQL = 'postgresql'
13+
14+
def database
15+
cop_config['Database'] || database_from_yaml || database_from_env
16+
end
17+
18+
private
19+
20+
def database_from_yaml
21+
return unless database_yaml
22+
23+
case database_adapter
24+
when 'mysql2'
25+
MYSQL
26+
when 'postgresql'
27+
POSTGRESQL
28+
end
29+
end
30+
31+
def database_from_env
32+
url = ENV['DATABASE_URL'].presence
33+
return unless url
34+
35+
case url
36+
when %r{\Amysql2://}
37+
MYSQL
38+
when %r{\Apostgres(ql)?://}
39+
POSTGRESQL
40+
end
41+
end
42+
43+
def database_yaml
44+
return unless File.exist?('config/database.yml')
45+
46+
yaml = if YAML.respond_to?(:unsafe_load_file)
47+
YAML.unsafe_load_file('config/database.yml')
48+
else
49+
YAML.load_file('config/database.yml')
50+
end
51+
return unless yaml.is_a? Hash
52+
53+
config = yaml['development']
54+
return unless config.is_a?(Hash)
55+
56+
config
57+
rescue Psych::SyntaxError
58+
# noop
59+
end
60+
61+
def database_adapter
62+
database_yaml['adapter'] || database_yaml.first.last['adapter']
63+
end
64+
end
65+
end
66+
end

lib/rubocop/cop/rails/bulk_change_table.rb

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,15 @@ module Rails
6464
# end
6565
# end
6666
class BulkChangeTable < Base
67+
include DatabaseTypeResolver
68+
6769
MSG_FOR_CHANGE_TABLE = <<~MSG.chomp
6870
You can combine alter queries using `bulk: true` options.
6971
MSG
7072
MSG_FOR_ALTER_METHODS = <<~MSG.chomp
7173
You can use `change_table :%<table>s, bulk: true` to combine alter queries.
7274
MSG
7375

74-
MYSQL = 'mysql'
75-
POSTGRESQL = 'postgresql'
76-
7776
MIGRATION_METHODS = %i[change up down].freeze
7877

7978
COMBINABLE_TRANSFORMATIONS = %i[
@@ -175,55 +174,6 @@ def include_bulk_options?(node)
175174
options.hash_type? && options.keys.any? { |key| key.sym_type? && key.value == :bulk }
176175
end
177176

178-
def database
179-
cop_config['Database'] || database_from_yaml || database_from_env
180-
end
181-
182-
def database_from_yaml
183-
return nil unless database_yaml
184-
185-
case database_adapter
186-
when 'mysql2'
187-
MYSQL
188-
when 'postgresql'
189-
POSTGRESQL
190-
end
191-
end
192-
193-
def database_adapter
194-
database_yaml['adapter'] || database_yaml.first.last['adapter']
195-
end
196-
197-
def database_yaml
198-
return nil unless File.exist?('config/database.yml')
199-
200-
yaml = if YAML.respond_to?(:unsafe_load_file)
201-
YAML.unsafe_load_file('config/database.yml')
202-
else
203-
YAML.load_file('config/database.yml')
204-
end
205-
return nil unless yaml.is_a? Hash
206-
207-
config = yaml['development']
208-
return nil unless config.is_a?(Hash)
209-
210-
config
211-
rescue Psych::SyntaxError
212-
nil
213-
end
214-
215-
def database_from_env
216-
url = ENV['DATABASE_URL'].presence
217-
return nil unless url
218-
219-
case url
220-
when %r{\Amysql2://}
221-
MYSQL
222-
when %r{\Apostgres(ql)?://}
223-
POSTGRESQL
224-
end
225-
end
226-
227177
def support_bulk_alter?
228178
case database
229179
when MYSQL

lib/rubocop/cop/rails_cops.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative 'mixin/active_record_helper'
44
require_relative 'mixin/active_record_migrations_helper'
55
require_relative 'mixin/class_send_node_helper'
6+
require_relative 'mixin/database_type_resolver'
67
require_relative 'mixin/enforce_superclass'
78
require_relative 'mixin/index_method'
89
require_relative 'mixin/migrations_helper'

0 commit comments

Comments
 (0)