Skip to content

Commit

Permalink
fix: creating ignore-table option
Browse files Browse the repository at this point in the history
  • Loading branch information
sh1nj1 committed Aug 17, 2024
1 parent b9daf73 commit c8ebcce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/mysql_db_tool/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def perform

puts "backupFile=#{backupFile}"

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

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

Expand All @@ -57,7 +57,7 @@ def perform
end
}

commands.push(gzipCommand("mysqldump --no-create-info #{options} #{defaultOptions} #{backupTables.join(' ')}", isGzip, "#{backupFile}-all-other-tables.sql", gzipSuffix))
commands.push(gzipCommand("mysqldump --no-create-info #{ignoreTablesOption} #{options} #{defaultOptions} #{backupTables.join(' ')}", isGzip, "#{backupFile}-all-other-tables.sql", gzipSuffix))
commands
end
end
Expand Down
37 changes: 28 additions & 9 deletions spec/mysql_db_tool/backup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
data_tables: [],
ignore_tables: []
})

fixed_time = DateTime.new(2024, 7, 17, 12, 0, 0)
allow(DateTime).to receive(:now).and_return(fixed_time)
end

describe '#perform' do
Expand All @@ -20,14 +23,12 @@
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)

commands = instance.perform
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 > backup-42/0_test_db-abc/2024-07-17_42-schema.sql",
"mysqldump --no-create-info --single-transaction --skip-lock-tables --column-statistics=0 --ssl-mode=disabled -h my-host -u test-user test_db-abc > backup-42/0_test_db-abc/2024-07-17_42-all-other-tables.sql"
"mysqldump --no-create-info --single-transaction --skip-lock-tables --column-statistics=0 --ssl-mode=disabled -h my-host -u test-user test_db-abc > backup-42/0_test_db-abc/2024-07-17_42-all-other-tables.sql"
])
end
end
Expand All @@ -38,14 +39,12 @@
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
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"
"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
Expand All @@ -56,14 +55,34 @@
let(:instance) { described_class.new(options) }

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

commands = instance.perform
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.Z",
"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.Z"
"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.Z"
])
end
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 with ignore table' do

allow(MySQLDBTool::Config::ConfigLoader).to receive(:load).and_return ({
db_info: { user: 'test-user', password: '', host: 'my-host', database: ['test_db-abc'] },
data_tables: [],
ignore_tables: ['ignore_table1', 'ignore_table2']
})

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

0 comments on commit c8ebcce

Please sign in to comment.