Skip to content

Commit 82b8037

Browse files
committed
Fix support for shared key in database.yml
Fix #1604. Rails allows specifying defaults in the database.yml by using the `shared` key. DatabaseTypeResolvable did not support this and broke cops if the adapter is specified via the shared key, even though it's a valid database configuration.
1 parent bf79547 commit 82b8037

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1604](https://github.com/rubocop/rubocop-rails/pull/1604): Allow `DatabaseTypeResolvable` to fall back to an `adapter` configuration specified in a `shared` key. ([@codergeek121][])

lib/rubocop/cop/mixin/database_type_resolvable.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def database_from_env
4040
end
4141
end
4242

43-
def database_yaml
43+
def database_yaml(environment = 'development')
4444
return unless File.exist?('config/database.yml')
4545

4646
yaml = if YAML.respond_to?(:unsafe_load_file)
@@ -50,7 +50,7 @@ def database_yaml
5050
end
5151
return unless yaml.is_a? Hash
5252

53-
config = yaml['development']
53+
config = yaml[environment]
5454
return unless config.is_a?(Hash)
5555

5656
config
@@ -59,7 +59,7 @@ def database_yaml
5959
end
6060

6161
def database_adapter
62-
database_yaml['adapter'] || database_yaml.first.last['adapter']
62+
database_yaml['adapter'] || database_yaml('shared')&.fetch('adapter') || database_yaml.first.last['adapter']
6363
end
6464
end
6565
end

spec/rubocop/cop/rails/bulk_change_table_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,29 @@ def change
687687
end
688688
end
689689

690+
context 'shared key' do
691+
context 'with top-level adapter configuration' do
692+
let(:yaml) do
693+
{
694+
'shared' => {
695+
'adapter' => 'postgresql'
696+
},
697+
'development' => {
698+
'foo' => 'bar'
699+
}
700+
}
701+
end
702+
703+
context 'with Rails 5.2', :rails52 do
704+
it_behaves_like 'offense for postgresql'
705+
end
706+
707+
context 'with Rails 5.1', :rails51 do
708+
it_behaves_like 'no offense for postgresql'
709+
end
710+
end
711+
end
712+
690713
context 'invalid (e.g. ERB)' do
691714
before do
692715
allow(YAML).to receive(:load_file).with('config/database.yml') do

0 commit comments

Comments
 (0)