diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-12-15 09:13:20 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-12-15 09:13:20 +0300 |
commit | 5e9a1717166c6b9bf0a6cbd00137d5c0043ba442 (patch) | |
tree | 30e54c2e3c41ad4caf51b0a60b46549d3feb3a7c /gems | |
parent | 00880613328c66f85aee755fcd1e70ce4cb9fcdf (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'gems')
6 files changed, 178 insertions, 0 deletions
diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb index 01ea934863f..4c46dff868c 100644 --- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb @@ -6,6 +6,7 @@ module Gitlab module Cli autoload :VERSION, 'gitlab/backup/cli/version' autoload :Runner, 'gitlab/backup/cli/runner' + autoload :Utils, 'gitlab/backup/cli/utils' Error = Class.new(StandardError) # Your code goes here... diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/utils.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/utils.rb new file mode 100644 index 00000000000..84e6c605172 --- /dev/null +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/utils.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Gitlab + module Backup + module Cli + module Utils + autoload :PgDump, 'gitlab/backup/cli/utils/pg_dump' + end + end + end +end diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/utils/pg_dump.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/utils/pg_dump.rb new file mode 100644 index 00000000000..6c16ca5cf06 --- /dev/null +++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/utils/pg_dump.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +module Gitlab + module Backup + module Cli + module Utils + class PgDump + # Expose snapshot_id to be used when creating a database dump + # See https://www.postgresql.org/docs/14/functions-admin.html#FUNCTIONS-SNAPSHOT-SYNCHRONIZATION + attr_reader :snapshot_id + # Dump only specified database schemas instead of everything + attr_reader :schemas + # Database name + attr_reader :database_name + # Additional ENV variables to use when running PgDump + attr_reader :env + + # @param [String] database_name + # @param [String] snapshot_id the snapshot id to use when creating a database dump + # @param [Array<String>] schemas + # @param [Hash<String,String>] env + def initialize(database_name:, snapshot_id: nil, schemas: [], env: {}) + @database_name = database_name + @snapshot_id = snapshot_id + @schemas = schemas + @env = env + end + + # Spawn a pg_dump process and assign a given output IO + # + # @param [IO] output the output IO + def spawn(output:) + Process.spawn(env, 'pg_dump', *cmd_args, out: output) + end + + private + + # Returns a list of arguments used by the pg_dump command + # + # @return [Array<String (frozen)>] + def cmd_args + args = ["--clean"] # Pass '--clean' to include 'DROP TABLE' statements in the DB dump. + args << '--if-exists' + args << "--snapshot=#{snapshot_id}" if snapshot_id + + schemas.each do |schema| + args << '-n' + args << schema + end + + args << database_name + + args + end + end + end + end + end +end diff --git a/gems/gitlab-backup-cli/sig/gitlab/backup/cli.rbs b/gems/gitlab-backup-cli/sig/gitlab/backup/cli.rbs index 25540c06400..76b68239e30 100644 --- a/gems/gitlab-backup-cli/sig/gitlab/backup/cli.rbs +++ b/gems/gitlab-backup-cli/sig/gitlab/backup/cli.rbs @@ -1,6 +1,7 @@ module Gitlab module Backup module Cli + Error: StandardError VERSION: String end end diff --git a/gems/gitlab-backup-cli/sig/gitlab/backup/cli/utils/pg_dump.rbs b/gems/gitlab-backup-cli/sig/gitlab/backup/cli/utils/pg_dump.rbs new file mode 100644 index 00000000000..1718d0df6c0 --- /dev/null +++ b/gems/gitlab-backup-cli/sig/gitlab/backup/cli/utils/pg_dump.rbs @@ -0,0 +1,25 @@ +module Gitlab + module Backup + module Cli + module Utils + class PgDump + attr_reader snapshot_id: String + attr_reader schemas: Array[String] + attr_reader database_name: String + attr_reader env: Hash[String,String] + + def initialize: (database_name: String, ?snapshot_id: String?, ?schemas: Array[String], ?env: Hash[String, String]) -> void + + # Spawn a pg_dump process and assign a given output IO + # + # @param [IO] output the output IO + def spawn: (output: IO) -> Integer + + private + + def cmd_args: () -> Array[String] + end + end + end + end +end diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli/utils/pg_dump_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/utils/pg_dump_spec.rb new file mode 100644 index 00000000000..b1e8495c637 --- /dev/null +++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli/utils/pg_dump_spec.rb @@ -0,0 +1,81 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Backup::Cli::Utils::PgDump do + let(:cmd_args) { pg_dump.send(:cmd_args) } + let(:database_name) { 'gitlab_database' } + let(:env) do + { + 'PGHOST' => '192.168.99.99', + 'PGPORT' => '5434' + } + end + + subject(:pg_dump) { described_class.new(database_name: database_name) } + + context 'with accessors' do + it { respond_to :database_name } + it { respond_to :snapshot_id } + it { respond_to :schemas } + it { respond_to :env } + end + + describe '#cmd_args' do + let(:default_args) { %w[--clean --if-exists] } + + context 'when no optional parameter is provided' do + it 'returns default arguments' do + expect(cmd_args).to eq(default_args << database_name) + end + end + + context 'with custom snapshot_id' do + let(:snapshot_id) { '00000003-000001BF-1' } + + subject(:pg_dump) { described_class.new(database_name: database_name, snapshot_id: snapshot_id) } + + it 'adds a flag between default_args and the database name' do + expect(cmd_args).to eq(default_args + %W[--snapshot=#{snapshot_id} #{database_name}]) + end + end + + context 'with custom schemas' do + let(:schemas) { %w[public gitlab_partitions_dynamic gitlab_partitions_static] } + + subject(:pg_dump) { described_class.new(database_name: database_name, schemas: schemas) } + + it 'adds additional flags for each schema' do + schemas_args = %W[-n #{schemas[0]} -n #{schemas[1]} -n #{schemas[2]}] + expected_args = (default_args + schemas_args) << database_name + + expect(cmd_args).to eq(expected_args) + end + end + end + + describe '#spawn' do + it 'returns a spawned process' do + process = instance_double(Process) + expect(Process).to receive(:spawn).and_return(process) + + expect(pg_dump.spawn(output: StringIO)).to eq(process) + end + + it 'forwards cmd_args to Process spawn' do + expect(Process).to receive(:spawn).with({}, 'pg_dump', *cmd_args, any_args) + + pg_dump.spawn(output: StringIO) + end + + context 'when env variables are provided' do + subject(:pg_dump) { described_class.new(database_name: database_name, env: env) } + + it 'forwards provided env variables to Process spawn' do + expect(Process).to receive(:spawn).with(env, 'pg_dump', any_args) + + pg_dump.spawn(output: StringIO) + end + end + end +end |