Skip to content

Commit

Permalink
Merge pull request #1056 from mjankowski/database-adapter-detection
Browse files Browse the repository at this point in the history
Fix database adapter detection for nested config
  • Loading branch information
koic authored Aug 7, 2023
2 parents 541da7c + e80345a commit 6202d3e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog/change_fix_database_adapter_detection_for.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1056](https://github.com/rubocop/rubocop-rails/pull/1056): Fix database adapter detection for nested config. ([@mjankowski][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rails/bulk_change_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,18 @@ def database
def database_from_yaml
return nil unless database_yaml

case database_yaml['adapter']
case database_adapter
when 'mysql2'
MYSQL
when 'postgresql'
POSTGRESQL
end
end

def database_adapter
database_yaml['adapter'] || database_yaml.first.last['adapter']
end

def database_yaml
return nil unless File.exist?('config/database.yml')

Expand Down
70 changes: 54 additions & 16 deletions spec/rubocop/cop/rails/bulk_change_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,32 +452,70 @@ def change
end

context 'mysql2' do
let(:yaml) do
{
'development' => {
'adapter' => 'mysql2'
context 'with top-level adapter configuration' do
let(:yaml) do
{
'development' => {
'adapter' => 'mysql2'
}
}
}
end

it_behaves_like 'offense for mysql'
end

it_behaves_like 'offense for mysql'
context 'with nested adapter configuration' do
let(:yaml) do
{
'development' => {
'primary' => {
'adapter' => 'mysql2'
}
}
}
end

it_behaves_like 'offense for mysql'
end
end

context 'postgresql' do
let(:yaml) do
{
'development' => {
'adapter' => 'postgresql'
context 'with top-level adapter configuration' do
let(:yaml) do
{
'development' => {
'adapter' => 'postgresql'
}
}
}
end
end

context 'with Rails 5.2', :rails52 do
it_behaves_like 'offense for postgresql'
context 'with Rails 5.2', :rails52 do
it_behaves_like 'offense for postgresql'
end

context 'with Rails 5.1', :rails51 do
it_behaves_like 'no offense for postgresql'
end
end

context 'with Rails 5.1', :rails51 do
it_behaves_like 'no offense for postgresql'
context 'with nested adapter configuration' do
let(:yaml) do
{
'development' => {
'primary' => {
'adapter' => 'postgresql'
}
}
}
end

context 'with Rails 5.2', :rails52 do
it_behaves_like 'offense for postgresql'
end

context 'with Rails 5.1', :rails51 do
it_behaves_like 'no offense for postgresql'
end
end
end

Expand Down

0 comments on commit 6202d3e

Please sign in to comment.