Skip to content

Commit

Permalink
Use DATABASE_URL friendly AR::Base.configurations
Browse files Browse the repository at this point in the history
Rails.configuration.database_configuration has no knowledge of db configuration
found in the DATABASE_URL environment variable.  We need to use
ActiveRecord::Base.configurations.

Related to: ManageIQ#15269
  • Loading branch information
jrafanie committed May 26, 2020
1 parent cf75130 commit be1ced9
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/models/database_backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def backup_file_name
private

def current_db_opts
current = Rails.configuration.database_configuration[Rails.env]
current = ActiveRecord::Base.configurations[Rails.env]
{
:hostname => current["host"],
:dbname => current["database"],
Expand Down
4 changes: 2 additions & 2 deletions app/models/miq_region_remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def self.region_valid?(guid, region, host, port, username, password, database =

def self.prepare_default_fields(database, adapter)
if database.nil? || adapter.nil?
db_conf = Rails.configuration.database_configuration[Rails.env]
db_conf = ActiveRecord::Base.configurations[Rails.env]
database ||= db_conf["database"]
adapter ||= db_conf["adapter"]
end
Expand All @@ -84,7 +84,7 @@ def self.with_remote_connection(host, port, username, password, database, adapte
host = host.to_s.strip
raise ArgumentError, _("host cannot be blank") if host.blank?
if [nil, "", "localhost", "localhost.localdomain", "127.0.0.1", "0.0.0.0"].include?(host)
local_database = Rails.configuration.database_configuration.fetch_path(Rails.env, "database").to_s.strip
local_database = ActiveRecord::Base.configurations.fetch_path(Rails.env, "database").to_s.strip
if database == local_database
raise ArgumentError, _("host cannot be set to localhost if database matches the local database")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/evm_database_ops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def self.gc(options = {})
end

def self.database_connections(database = nil, type = :all)
database ||= Rails.configuration.database_configuration[Rails.env]["database"]
database ||= ActiveRecord::Base.configurations[Rails.env]["database"]
conn = ActiveRecord::Base.connection
conn.client_connections.count { |c| c["database"] == database }
end
Expand Down
2 changes: 2 additions & 0 deletions lib/tasks/test_vmdb.rake
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace :test do
desc "Run RSpec code examples in parallel"
task :vmdb_parallel => :spec_deps do
# Check that '<name_of_test_database>2' exists, else you need additional setup
# FIXME, parallel_tests via this rake task does not currently support DATABASE_URL configuration.
# We should be using ActiveRecord::Base.configurations at some point.
test_config = Rails.configuration.database_configuration['test'].tap { |config| config['database'].concat('2') }
begin
ActiveRecord::Base.establish_connection(test_config)
Expand Down
2 changes: 1 addition & 1 deletion spec/models/miq_region_remote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
it "removes the temporary connection pool" do
original = described_class.connection.raw_connection.conninfo_hash[:dbname]

config = Rails.configuration.database_configuration[Rails.env]
config = ActiveRecord::Base.configurations[Rails.env]
params = config.values_at("host", "port", "username", "password", "database", "adapter")
params[0] ||= "localhost"
params[4] = "template1"
Expand Down

0 comments on commit be1ced9

Please sign in to comment.