Skip to content
Snippets Groups Projects

Add rake task for copying 'main' database to 'ci' database

2 files
+ 74
15
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -3,21 +3,60 @@
module Gitlab
module Database
module Decomposition
MigrateError = Class.new(RuntimeError)
class Migrate
TABLE_SIZE_QUERY = <<-SQL
select sum(pg_table_size(concat(table_schema,'.',table_name))) as total
from information_schema.tables
where table_catalog = :table_catalog and table_type = 'BASE TABLE'
SQL
attr_reader :backup_location
def initialize
@main_dump_location = File.join(Gitlab.config.backup.path, 'migration')
@backup_location = File.join(Gitlab.config.backup.path, 'migration')
end
def process!
dump_main_db
dump_main_db if required_diskspace_available
import_dump_to_ci_db
FileUtils.remove_entry_secure(@main_dump_location, true)
FileUtils.remove_entry_secure(@backup_location, true)
end
private
def main_table_sizes
ApplicationRecord.connection.execute(
ApplicationRecord.sanitize_sql([
TABLE_SIZE_QUERY,
{ table_catalog: main_config.dig(:activerecord, :database) }
])
).first["total"].to_f
end
def diskspace_free
Sys::Filesystem.stat(
File.expand_path("#{@backup_location}/../")
).bytes_free
end
def required_diskspace_available
needed = main_table_sizes * 1.25
available = diskspace_free
if needed > available
raise MigrateError,
"Not enough diskspace available on #{@backup_location}: " \
"Available: #{ActiveSupport::NumberHelper.number_to_human_size(available)}, " \
"Needed: #{ActiveSupport::NumberHelper.number_to_human_size(needed)}"
end
true
end
def with_transient_pg_env(extended_env)
ENV.merge!(extended_env)
result = yield
@@ -32,13 +71,13 @@ def import_dump_to_ci_db
with_transient_pg_env(ci_config[:pg_env]) do
restore_args = ["--jobs=4", "--dbname=#{ci_database_name}"]
Open3.capture2e('pg_restore', *restore_args, @main_dump_location)
Open3.capture2e('pg_restore', *restore_args, @backup_location)
end
end
def dump_main_db
with_transient_pg_env(main_config[:pg_env]) do
args = ['--format=d', '--jobs=4', "--file=#{@main_dump_location}"]
args = ['--format=d', '--jobs=4', "--file=#{@backup_location}"]
Open3.capture2e('pg_dump', *args, main_config.dig(:activerecord, :database))
end
Loading