Skip to content

Commit bda5de3

Browse files
committed
Read database config from environment variable
1 parent baf39e6 commit bda5de3

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#903](https://github.com/rubocop/rubocop-rails/pull/903): Read database config for `Rails/BulkChangeTable` from environment variable. ([@joergschiller][])

lib/rubocop/cop/rails/bulk_change_table.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module Rails
1212
# The `bulk` option is only supported on the MySQL and
1313
# the PostgreSQL (5.2 later) adapter; thus it will
1414
# automatically detect an adapter from `development` environment
15-
# in `config/database.yml` when the `Database` option is not set.
15+
# in `config/database.yml` or the environment variable `DATABASE_URL`
16+
# when the `Database` option is not set.
1617
# If the adapter is not `mysql2` or `postgresql`,
1718
# this Cop ignores offenses.
1819
#
@@ -175,7 +176,7 @@ def include_bulk_options?(node)
175176
end
176177

177178
def database
178-
cop_config['Database'] || database_from_yaml
179+
cop_config['Database'] || database_from_yaml || database_from_env
179180
end
180181

181182
def database_from_yaml
@@ -211,6 +212,18 @@ def database_yaml
211212
nil
212213
end
213214

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+
214227
def support_bulk_alter?
215228
case database
216229
when MYSQL

spec/rubocop/cop/rails/bulk_change_table_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,4 +529,46 @@ def change
529529
it_behaves_like 'no offense'
530530
end
531531
end
532+
533+
context 'when `DATABASE_URL` is set' do
534+
before do
535+
allow(ENV).to receive(:[]).with('DATABASE_URL').and_return(database_url)
536+
end
537+
538+
context 'mysql2' do
539+
let(:database_url) { 'mysql2://localhost/my_database' }
540+
541+
it_behaves_like 'offense for mysql'
542+
end
543+
544+
context 'postgres' do
545+
let(:database_url) { 'postgres://localhost/my_database' }
546+
547+
context 'with Rails 5.2', :rails52 do
548+
it_behaves_like 'offense for postgresql'
549+
end
550+
551+
context 'with Rails 5.1', :rails51 do
552+
it_behaves_like 'no offense for postgresql'
553+
end
554+
end
555+
556+
context 'postgresql' do
557+
let(:database_url) { 'postgresql://localhost/my_database' }
558+
559+
context 'with Rails 5.2', :rails52 do
560+
it_behaves_like 'offense for postgresql'
561+
end
562+
563+
context 'with Rails 5.1', :rails51 do
564+
it_behaves_like 'no offense for postgresql'
565+
end
566+
end
567+
568+
context 'unsupported (e.g. sqlserver)' do
569+
let(:database_url) { 'sqlserver://localhost/my_database' }
570+
571+
it_behaves_like 'no offense'
572+
end
573+
end
532574
end

0 commit comments

Comments
 (0)