Skip to content

Commit

Permalink
Merge pull request #50 from NickLaMuro/file_split_for_db_backup_and_d…
Browse files Browse the repository at this point in the history
…b_dump

Adds file splitting support to dump/backup commands
  • Loading branch information
carbonin authored Sep 20, 2018
2 parents 8000564 + b7ab9cb commit f7741cf
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/manageiq/appliance_console/database_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def ask_questions
say(DB_DUMP_WARNING) if action == :dump
ask_file_location
ask_for_tables_to_exclude_in_dump
ask_to_split_up_output
end

def activate
Expand Down Expand Up @@ -147,6 +148,12 @@ def ask_for_tables_to_exclude_in_dump
end || true
end

def ask_to_split_up_output
if action == :dump && should_split_output?
@task_params.last[:byte_count] = ask_for_string("byte size to split by", "500M")
end || true
end

def confirm_and_execute
if allowed_to_execute?
processing_message
Expand Down Expand Up @@ -182,6 +189,12 @@ def should_exclude_tables?
end
end

def should_split_output?
ask_yn?("Would you like to split the #{action} output into multiple parts") do |q|
q.readline = true
end
end

def filename_prompt_args
default = action == :dump ? DB_DEFAULT_DUMP_FILE : DB_RESTORE_FILE
validator = LOCAL_FILE_VALIDATOR if action == :restore && backup_type == LOCAL_FILE
Expand Down
95 changes: 95 additions & 0 deletions spec/database_admin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
expect(subject).to receive(:say).with("Restore Database From Backup\n\n")
expect(subject).to receive(:ask_file_location)
expect(subject).to receive(:ask_for_tables_to_exclude_in_dump)
expect(subject).to receive(:ask_to_split_up_output)

subject.ask_questions
end
Expand Down Expand Up @@ -449,6 +450,27 @@
end
end

describe "#ask_to_split_up_output" do
let(:uri) { "/tmp/my_db.dump" }
let(:yn_prompt) { "Would you like to split the restore output into multiple parts" }
let(:byte_count_prompt) { "byte size to split by" }

before do
subject.instance_variable_set(:@task_params, ["--", { :uri => uri }])
end

it "no-ops" do
expect(subject).to receive(:ask_yn?).with(yn_prompt).never
expect(subject).to receive(:ask_for_string).with(byte_count_prompt, "500M").never
expect(subject.ask_to_split_up_output).to be_truthy
end

it "does not modify the @task_params" do
expect(subject.ask_to_split_up_output).to be_truthy
expect(subject.task_params).to eq(["--", {:uri => uri}])
end
end

describe "#confirm_and_execute" do
let(:uri) { "/tmp/my_db.backup" }
let(:agree) { "y" }
Expand Down Expand Up @@ -592,6 +614,7 @@ def confirm_and_execute
expect(subject).to receive(:say).with("Create Database Backup\n\n")
expect(subject).to receive(:ask_file_location)
expect(subject).to receive(:ask_for_tables_to_exclude_in_dump)
expect(subject).to receive(:ask_to_split_up_output)

subject.ask_questions
end
Expand Down Expand Up @@ -1006,6 +1029,27 @@ def confirm_and_execute
end
end

describe "#ask_to_split_up_output" do
let(:uri) { "/tmp/my_db.dump" }
let(:yn_prompt) { "Would you like to split the restore output into multiple parts" }
let(:byte_count_prompt) { "byte size to split by" }

before do
subject.instance_variable_set(:@task_params, ["--", { :uri => uri }])
end

it "no-ops" do
expect(subject).to receive(:ask_yn?).with(yn_prompt).never
expect(subject).to receive(:ask_for_string).with(byte_count_prompt, "500M").never
expect(subject.ask_to_split_up_output).to be_truthy
end

it "does not modify the @task_params" do
expect(subject.ask_to_split_up_output).to be_truthy
expect(subject.task_params).to eq(["--", {:uri => uri}])
end
end

describe "#confirm_and_execute" do
let(:uri) { "/tmp/my_db.backup" }
let(:agree) { "y" }
Expand Down Expand Up @@ -1130,13 +1174,15 @@ def confirm_and_execute
expect(subject).to receive(:say).with(pg_dump_warning)
expect(subject).to receive(:ask_file_location)
expect(subject).to receive(:ask_for_tables_to_exclude_in_dump)
expect(subject).to receive(:ask_to_split_up_output)

subject.ask_questions
end

it "has proper formatting for the pg_dump warning" do
allow(subject).to receive(:ask_file_location)
allow(subject).to receive(:ask_for_tables_to_exclude_in_dump)
allow(subject).to receive(:ask_to_split_up_output)
subject.ask_questions

expect_output <<-PROMPT.strip_heredoc
Expand Down Expand Up @@ -1457,6 +1503,55 @@ def confirm_and_execute
end
end

describe "#ask_to_split_up_output" do
let(:uri) { "/tmp/my_db.dump" }
let(:yn_prompt) { "Would you like to split the dump output into multiple parts" }
let(:byte_count_prompt) { "byte size to split by" }

before do
subject.instance_variable_set(:@task_params, ["--", { :uri => uri }])
end

context "when not splitting output" do
it "does not add :byte_count to @task_params" do
expect(subject).to receive(:ask_yn?).with(yn_prompt).once.and_call_original
expect(subject).to receive(:ask_for_string).with(byte_count_prompt, "500M").never

say "n"
expect(subject.ask_to_split_up_output).to be_truthy

expect(subject.task_params).to eq(["--", {:uri => uri}])
end
end

context "when splitting output" do
it "prompts the user" do
say ["y", "750M"]
expect(subject.ask_to_split_up_output).to be_truthy
expect_readline_question_asked <<-PROMPT.strip_heredoc.chomp
Would you like to split the dump output into multiple parts? (Y/N): y
Enter the byte size to split by: |500M| 750M
PROMPT
end

it "adds `:byte_count => '250M'` to @task_params" do
expect(subject).to receive(:ask_yn?).with(yn_prompt).once.and_call_original
expect(subject).to receive(:ask_for_string).with(byte_count_prompt, "500M").once.and_call_original
say ["y", "250M"]

expect(subject.ask_to_split_up_output).to be_truthy
expect(subject.task_params).to eq(["--", {:uri => uri, :byte_count => "250M"}])
end

it "defaults to '500M'" do
say ["y", ""]

expect(subject.ask_to_split_up_output).to be_truthy
expect(subject.task_params).to eq(["--", {:uri => uri, :byte_count => "500M"}])
end
end
end

describe "#confirm_and_execute" do
let(:uri) { "/tmp/my_db.dump" }
let(:agree) { "y" }
Expand Down

0 comments on commit f7741cf

Please sign in to comment.