diff options
Diffstat (limited to 'lib/backup/database.rb')
-rw-r--r-- | lib/backup/database.rb | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/lib/backup/database.rb b/lib/backup/database.rb index 0429d9496d6..f07fd786b4b 100644 --- a/lib/backup/database.rb +++ b/lib/backup/database.rb @@ -9,6 +9,8 @@ module Backup attr_reader :config, :db_file_name IGNORED_ERRORS = [ + # Ignore warnings + /WARNING:/, # Ignore the DROP errors; recent database dumps will use --if-exists with pg_dump /does not exist$/, # User may not have permissions to drop extensions or schemas @@ -18,7 +20,7 @@ module Backup def initialize(progress, filename: nil) @progress = progress - @config = YAML.load_file(File.join(Rails.root, 'config', 'database.yml'))[Rails.env] + @config = ActiveRecord::Base.configurations.find_db_config(Rails.env).configuration_hash @db_file_name = filename || File.join(Gitlab.config.backup.path, 'db', 'database.sql.gz') end @@ -30,9 +32,9 @@ module Backup compress_rd.close dump_pid = - case config["adapter"] + case config[:adapter] when "postgresql" then - progress.print "Dumping PostgreSQL database #{config['database']} ... " + progress.print "Dumping PostgreSQL database #{database} ... " pg_env pgsql_args = ["--clean"] # Pass '--clean' to include 'DROP TABLE' statements in the DB dump. pgsql_args << '--if-exists' @@ -47,7 +49,7 @@ module Backup end end - Process.spawn('pg_dump', *pgsql_args, config['database'], out: compress_wr) + Process.spawn('pg_dump', *pgsql_args, database, out: compress_wr) end compress_wr.close @@ -68,9 +70,9 @@ module Backup decompress_wr.close status, errors = - case config["adapter"] + case config[:adapter] when "postgresql" then - progress.print "Restoring PostgreSQL database #{config['database']} ... " + progress.print "Restoring PostgreSQL database #{database} ... " pg_env execute_and_track_errors(pg_restore_cmd, decompress_rd) end @@ -93,6 +95,10 @@ module Backup protected + def database + @config[:database] + end + def ignore_error?(line) IGNORED_ERRORS_REGEXP.match?(line) end @@ -128,17 +134,17 @@ module Backup def pg_env args = { - 'username' => 'PGUSER', - 'host' => 'PGHOST', - 'port' => 'PGPORT', - 'password' => 'PGPASSWORD', + username: 'PGUSER', + host: 'PGHOST', + port: 'PGPORT', + password: 'PGPASSWORD', # SSL - 'sslmode' => 'PGSSLMODE', - 'sslkey' => 'PGSSLKEY', - 'sslcert' => 'PGSSLCERT', - 'sslrootcert' => 'PGSSLROOTCERT', - 'sslcrl' => 'PGSSLCRL', - 'sslcompression' => 'PGSSLCOMPRESSION' + sslmode: 'PGSSLMODE', + sslkey: 'PGSSLKEY', + sslcert: 'PGSSLCERT', + sslrootcert: 'PGSSLROOTCERT', + sslcrl: 'PGSSLCRL', + sslcompression: 'PGSSLCOMPRESSION' } args.each do |opt, arg| # This enables the use of different PostgreSQL settings in @@ -161,7 +167,7 @@ module Backup private def pg_restore_cmd - ['psql', config['database']] + ['psql', database] end end end |