Skip to content

Commit 6202d3e

Browse files
authored
Merge pull request #1056 from mjankowski/database-adapter-detection
Fix database adapter detection for nested config
2 parents 541da7c + e80345a commit 6202d3e

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1056](https://github.com/rubocop/rubocop-rails/pull/1056): Fix database adapter detection for nested config. ([@mjankowski][])

lib/rubocop/cop/rails/bulk_change_table.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,18 @@ def database
181181
def database_from_yaml
182182
return nil unless database_yaml
183183

184-
case database_yaml['adapter']
184+
case database_adapter
185185
when 'mysql2'
186186
MYSQL
187187
when 'postgresql'
188188
POSTGRESQL
189189
end
190190
end
191191

192+
def database_adapter
193+
database_yaml['adapter'] || database_yaml.first.last['adapter']
194+
end
195+
192196
def database_yaml
193197
return nil unless File.exist?('config/database.yml')
194198

spec/rubocop/cop/rails/bulk_change_table_spec.rb

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -452,32 +452,70 @@ def change
452452
end
453453

454454
context 'mysql2' do
455-
let(:yaml) do
456-
{
457-
'development' => {
458-
'adapter' => 'mysql2'
455+
context 'with top-level adapter configuration' do
456+
let(:yaml) do
457+
{
458+
'development' => {
459+
'adapter' => 'mysql2'
460+
}
459461
}
460-
}
462+
end
463+
464+
it_behaves_like 'offense for mysql'
461465
end
462466

463-
it_behaves_like 'offense for mysql'
467+
context 'with nested adapter configuration' do
468+
let(:yaml) do
469+
{
470+
'development' => {
471+
'primary' => {
472+
'adapter' => 'mysql2'
473+
}
474+
}
475+
}
476+
end
477+
478+
it_behaves_like 'offense for mysql'
479+
end
464480
end
465481

466482
context 'postgresql' do
467-
let(:yaml) do
468-
{
469-
'development' => {
470-
'adapter' => 'postgresql'
483+
context 'with top-level adapter configuration' do
484+
let(:yaml) do
485+
{
486+
'development' => {
487+
'adapter' => 'postgresql'
488+
}
471489
}
472-
}
473-
end
490+
end
474491

475-
context 'with Rails 5.2', :rails52 do
476-
it_behaves_like 'offense for postgresql'
492+
context 'with Rails 5.2', :rails52 do
493+
it_behaves_like 'offense for postgresql'
494+
end
495+
496+
context 'with Rails 5.1', :rails51 do
497+
it_behaves_like 'no offense for postgresql'
498+
end
477499
end
478500

479-
context 'with Rails 5.1', :rails51 do
480-
it_behaves_like 'no offense for postgresql'
501+
context 'with nested adapter configuration' do
502+
let(:yaml) do
503+
{
504+
'development' => {
505+
'primary' => {
506+
'adapter' => 'postgresql'
507+
}
508+
}
509+
}
510+
end
511+
512+
context 'with Rails 5.2', :rails52 do
513+
it_behaves_like 'offense for postgresql'
514+
end
515+
516+
context 'with Rails 5.1', :rails51 do
517+
it_behaves_like 'no offense for postgresql'
518+
end
481519
end
482520
end
483521

0 commit comments

Comments
 (0)