From be1ced9279cd71a80c7dae350924472a4a0c3ce5 Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Tue, 26 May 2020 12:01:21 -0400 Subject: [PATCH] Use DATABASE_URL friendly AR::Base.configurations 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: https://github.com/ManageIQ/manageiq/pull/15269 --- app/models/database_backup.rb | 2 +- app/models/miq_region_remote.rb | 4 ++-- lib/evm_database_ops.rb | 2 +- lib/tasks/test_vmdb.rake | 2 ++ spec/models/miq_region_remote_spec.rb | 2 +- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/models/database_backup.rb b/app/models/database_backup.rb index e2cbe1b2926..445e8193713 100644 --- a/app/models/database_backup.rb +++ b/app/models/database_backup.rb @@ -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"], diff --git a/app/models/miq_region_remote.rb b/app/models/miq_region_remote.rb index 9008e4115af..ec1a2bc6d10 100644 --- a/app/models/miq_region_remote.rb +++ b/app/models/miq_region_remote.rb @@ -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 @@ -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 diff --git a/lib/evm_database_ops.rb b/lib/evm_database_ops.rb index b170071e2dc..4475826f4fc 100644 --- a/lib/evm_database_ops.rb +++ b/lib/evm_database_ops.rb @@ -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 diff --git a/lib/tasks/test_vmdb.rake b/lib/tasks/test_vmdb.rake index 32e9aaa90b0..2ed6823a479 100644 --- a/lib/tasks/test_vmdb.rake +++ b/lib/tasks/test_vmdb.rake @@ -42,6 +42,8 @@ namespace :test do desc "Run RSpec code examples in parallel" task :vmdb_parallel => :spec_deps do # Check that '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) diff --git a/spec/models/miq_region_remote_spec.rb b/spec/models/miq_region_remote_spec.rb index ba312cd8065..fbdd94bafe3 100644 --- a/spec/models/miq_region_remote_spec.rb +++ b/spec/models/miq_region_remote_spec.rb @@ -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"