Skip to content

Commit

Permalink
fix: add .gz extension if gzip option is on
Browse files Browse the repository at this point in the history
  • Loading branch information
sh1nj1 committed Aug 14, 2024
1 parent ee64b10 commit ac0cced
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/mysql_db_tool/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def perform

ignoreTablesOption = @ignore_tables.map { |e| "--ignore-table=#{@db_info[:database]}.#{e}" }.join(' ')

commands.push gzipCommand("mysqldump --no-data #{ignoreTablesOption} #{defaultOptions}", isGzip, "#{backupFile}-schema.sql")
commands.push gzipCommand("mysqldump --no-data #{ignoreTablesOption} #{defaultOptions}", isGzip, "#{backupFile}-schema.sql#{isGzip ? '.gz' : ''}")

backupTables = []

Expand All @@ -52,11 +52,11 @@ def perform
backupTables.push(table[:name])
next
else
commands.push(gzipCommand("mysqldump --no-create-info #{options} #{where} #{defaultOptions} #{table[:name]}", isGzip, "#{backupFile}-#{table[:name]}.sql"))
commands.push(gzipCommand("mysqldump --no-create-info #{options} #{where} #{defaultOptions} #{table[:name]}", isGzip, "#{backupFile}-#{table[:name]}.sql#{isGzip ? '.gz' : ''}"))
end
}

commands.push(gzipCommand("mysqldump --no-create-info #{options} #{defaultOptions} #{backupTables.join(' ')}", isGzip, "#{backupFile}-all-other-tables.sql"))
commands.push(gzipCommand("mysqldump --no-create-info #{options} #{defaultOptions} #{backupTables.join(' ')}", isGzip, "#{backupFile}-all-other-tables.sql#{isGzip ? '.gz' : ''}"))
commands
end
end
Expand Down
25 changes: 23 additions & 2 deletions spec/mysql_db_tool/backup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
require 'mysql_db_tool/backup'

RSpec.describe MySQLDBTool::Backup do
let(:options) { { env: 'backup-test-env', id: '42', run: false, gzip: false } }
let(:instance) { described_class.new(options) }

before do
allow(MySQLDBTool::Config::ConfigLoader).to receive(:load).and_return ({
Expand All @@ -17,6 +15,10 @@
end

describe '#perform' do

let(:options) { { env: 'backup-test-env', id: '42', run: false, gzip: false } }
let(:instance) { described_class.new(options) }

it 'backs up each database' do
fixed_time = DateTime.new(2024, 7, 17, 12, 0, 0)
allow(DateTime).to receive(:now).and_return(fixed_time)
Expand All @@ -30,4 +32,23 @@
end
end

describe '#perform' do

let(:options) { { env: 'backup-test-env', id: '42', run: false, gzip: true } }
let(:instance) { described_class.new(options) }

it 'backs up with gzip' do
fixed_time = DateTime.new(2024, 7, 17, 12, 0, 0)
allow(DateTime).to receive(:now).and_return(fixed_time)

commands = instance.perform
puts "commands=#{commands}"
expect(commands).to eq ([
"mkdir -p backup-42/0_test_db-abc",
"mysqldump --no-data --column-statistics=0 --ssl-mode=disabled -h my-host -u test-user test_db-abc | gzip > backup-42/0_test_db-abc/2024-07-17_42-schema.sql.gz",
"mysqldump --no-create-info --single-transaction --skip-lock-tables --column-statistics=0 --ssl-mode=disabled -h my-host -u test-user test_db-abc | gzip > backup-42/0_test_db-abc/2024-07-17_42-all-other-tables.sql.gz"
])
end
end

end

0 comments on commit ac0cced

Please sign in to comment.