File tree Expand file tree Collapse file tree 3 files changed +58
-2
lines changed Expand file tree Collapse file tree 3 files changed +58
-2
lines changed Original file line number Diff line number Diff line change
1
+ * [ #903 ] ( https://github.com/rubocop/rubocop-rails/pull/903 ) : Read database config for ` Rails/BulkChangeTable ` from environment variable. ([ @joergschiller ] [ ] )
Original file line number Diff line number Diff line change @@ -12,7 +12,8 @@ module Rails
12
12
# The `bulk` option is only supported on the MySQL and
13
13
# the PostgreSQL (5.2 later) adapter; thus it will
14
14
# 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.
16
17
# If the adapter is not `mysql2` or `postgresql`,
17
18
# this Cop ignores offenses.
18
19
#
@@ -175,7 +176,7 @@ def include_bulk_options?(node)
175
176
end
176
177
177
178
def database
178
- cop_config [ 'Database' ] || database_from_yaml
179
+ cop_config [ 'Database' ] || database_from_yaml || database_from_env
179
180
end
180
181
181
182
def database_from_yaml
@@ -211,6 +212,18 @@ def database_yaml
211
212
nil
212
213
end
213
214
215
+ def database_from_env
216
+ url = ENV [ 'DATABASE_URL' ] . presence
217
+ return nil unless url
218
+
219
+ case url
220
+ when %r{\A mysql2://}
221
+ MYSQL
222
+ when %r{\A postgres(ql)?://}
223
+ POSTGRESQL
224
+ end
225
+ end
226
+
214
227
def support_bulk_alter?
215
228
case database
216
229
when MYSQL
Original file line number Diff line number Diff line change @@ -529,4 +529,46 @@ def change
529
529
it_behaves_like 'no offense'
530
530
end
531
531
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
532
574
end
You can’t perform that action at this time.
0 commit comments