Skip to content

Commit

Permalink
Read database config from environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
joergschiller committed Sep 6, 2023
1 parent baf39e6 commit bda5de3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/change_read_database_config_from_environment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#903](https://github.com/rubocop/rubocop-rails/pull/903): Read database config for `Rails/BulkChangeTable` from environment variable. ([@joergschiller][])
17 changes: 15 additions & 2 deletions lib/rubocop/cop/rails/bulk_change_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module Rails
# The `bulk` option is only supported on the MySQL and
# the PostgreSQL (5.2 later) adapter; thus it will
# automatically detect an adapter from `development` environment
# in `config/database.yml` when the `Database` option is not set.
# in `config/database.yml` or the environment variable `DATABASE_URL`
# when the `Database` option is not set.
# If the adapter is not `mysql2` or `postgresql`,
# this Cop ignores offenses.
#
Expand Down Expand Up @@ -175,7 +176,7 @@ def include_bulk_options?(node)
end

def database
cop_config['Database'] || database_from_yaml
cop_config['Database'] || database_from_yaml || database_from_env
end

def database_from_yaml
Expand Down Expand Up @@ -211,6 +212,18 @@ def database_yaml
nil
end

def database_from_env
url = ENV['DATABASE_URL'].presence
return nil unless url

case url
when %r{\Amysql2://}
MYSQL
when %r{\Apostgres(ql)?://}
POSTGRESQL
end
end

def support_bulk_alter?
case database
when MYSQL
Expand Down
42 changes: 42 additions & 0 deletions spec/rubocop/cop/rails/bulk_change_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,46 @@ def change
it_behaves_like 'no offense'
end
end

context 'when `DATABASE_URL` is set' do
before do
allow(ENV).to receive(:[]).with('DATABASE_URL').and_return(database_url)
end

context 'mysql2' do
let(:database_url) { 'mysql2://localhost/my_database' }

it_behaves_like 'offense for mysql'
end

context 'postgres' do
let(:database_url) { 'postgres://localhost/my_database' }

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 'postgresql' do
let(:database_url) { 'postgresql://localhost/my_database' }

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 'unsupported (e.g. sqlserver)' do
let(:database_url) { 'sqlserver://localhost/my_database' }

it_behaves_like 'no offense'
end
end
end

0 comments on commit bda5de3

Please sign in to comment.