Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

use configuration_hash and one place for user || username

Ivan Kuchin4e31c8bba576
diff --git a/lib/tasks/backup.rake b/lib/tasks/backup.rake
index 318be6fe449..0f4c2addacf 100644
--- a/lib/tasks/backup.rake
+++ b/lib/tasks/backup.rake
@@ -44,13 +44,12 @@ namespace :backup do
"--file=#{args[:path_to_backup]}",
'--format=custom',
'--no-owner']
- pg_dump_call << "--host=#{config['host']}" if config['host']
- pg_dump_call << "--port=#{config['port']}" if config['port']
- user = config.values_at('user', 'username').compact.first
- pg_dump_call << "--username=#{user}" if user
- pg_dump_call << config['database'].to_s
+ pg_dump_call << "--host=#{config[:host]}" if config[:host]
+ pg_dump_call << "--port=#{config[:port]}" if config[:port]
+ pg_dump_call << "--username=#{config[:user]}" if config[:user]
+ pg_dump_call << config[:database].to_s
- if config['password']
+ if config[:password]
Kernel.system({ 'PGPASSFILE' => config_file }, *pg_dump_call)
else
Kernel.system(*pg_dump_call)
@@ -70,14 +69,13 @@ namespace :backup do
'--clean',
'--no-owner',
'--single-transaction',
- "--dbname=#{config['database']}"]
- pg_restore_call << "--host=#{config['host']}" if config['host']
- pg_restore_call << "--port=#{config['port']}" if config['port']
- user = config.values_at('user', 'username').compact.first
- pg_restore_call << "--username=#{user}" if user
+ "--dbname=#{config[:database]}"]
+ pg_restore_call << "--host=#{config[:host]}" if config[:host]
+ pg_restore_call << "--port=#{config[:port]}" if config[:port]
+ pg_restore_call << "--username=#{config[:user]}" if config[:user]
pg_restore_call << args[:path_to_backup].to_s
- if config['password']
+ if config[:password]
Kernel.system({ 'PGPASSFILE' => config_file }, *pg_restore_call)
else
Kernel.system(*pg_restore_call)
@@ -88,12 +86,17 @@ namespace :backup do
private
def database_configuration
- Rails.application.config.database_configuration[Rails.env]
+ hash = ActiveRecord::Base.connection_db_config.configuration_hash
+
+ {
+ **hash.slice(:host, :port, :database, :password, :sslkey, :sslcert, :sslca),
+ user: hash[:user] || hash[:username],
+ }
end
def with_config_file(config, &blk)
file = Tempfile.new('op_pg_config')
- file.write "*:*:*:*:#{config['password']}"
+ file.write "*:*:*:*:#{config[:password]}"
file.close
blk.yield file.path
file.unlink
@@ -101,13 +104,13 @@ namespace :backup do
def sql_dump_tempfile(config)
t = "[client]\n"
- t << "password=\"#{config['password']}\"\n"
- t << "user=\"#{config.values_at('user', 'username').compact.first}\"\n"
- t << "host=\"#{config['host'] || '127.0.0.1'}\"\n"
- t << "port=\"#{config['port']}\"\n" if config['port']
- t << "ssl-key=\"#{config['sslkey']}\"\n" if config['sslkey']
- t << "ssl-cert=\"#{config['sslcert']}\"\n" if config['sslcert']
- t << "ssl-ca=\"#{config['sslca']}\"\n" if config['sslca']
+ t << "password=\"#{config[:password]}\"\n"
+ t << "user=\"#{config[:user]}\"\n"
+ t << "host=\"#{config[:host] || '127.0.0.1'}\"\n"
+ t << "port=\"#{config[:port]}\"\n" if config[:port]
+ t << "ssl-key=\"#{config[:sslkey]}\"\n" if config[:sslkey]
+ t << "ssl-cert=\"#{config[:sslcert]}\"\n" if config[:sslcert]
+ t << "ssl-ca=\"#{config[:sslca]}\"\n" if config[:sslca]
t
end
diff --git a/spec/tasks/backup_spec.rb b/spec/tasks/backup_spec.rb
index 847c52b40e6..feece39c4bf 100644
--- a/spec/tasks/backup_spec.rb
+++ b/spec/tasks/backup_spec.rb
@@ -36,8 +36,12 @@ RSpec.describe Rake::Task, 'backup:database' do
'password' => 'test_password' }
end
+ let(:hash_config) do
+ ActiveRecord::DatabaseConfigurations::HashConfig.new('test', 'primary', database_config)
+ end
+
before do
- allow(ActiveRecord::Base).to receive(:configurations).and_return('test' => database_config)
+ allow(ActiveRecord::Base).to receive(:connection_db_config).and_return(hash_config)
allow(FileUtils).to receive(:mkdir_p).and_return(nil)
allow(Kernel).to receive(:system)
end