Skip to content

Commit

Permalink
feat: add --database option for override configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
sh1nj1 committed Aug 17, 2024
1 parent c8ebcce commit d81d0a0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions bin/mysql_backup
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ OptionParser.new do |opts|
MySQLDBTool.env_opt(options, opts)
MySQLDBTool.id_opt(options, opts)
MySQLDBTool.run_opt(options, opts)
MySQLDBTool.database_opt(options, opts)

opts.on("-g", "--[no-]gzip", "Enable or disable gzip (default: enabled)") do |gzip|
options[:gzip] = gzip
Expand Down
1 change: 1 addition & 0 deletions bin/mysql_restore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ OptionParser.new do |opts|
MySQLDBTool.env_opt(options, opts)
MySQLDBTool.id_opt(options, opts)
MySQLDBTool.run_opt(options, opts)
MySQLDBTool.database_opt(options, opts)

opts.on("-D", "--[no-]drop-all-tables", "Drop all tables before restoring (default: disabled)") do |drop_all_tables|
options[:drop_all_tables] = drop_all_tables
Expand Down
6 changes: 6 additions & 0 deletions lib/mysql_db_tool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def self.run_opt(options, opts)
end
end

def self.database_opt(options, opts)
opts.on("-d", "--database DATABASE", "Override option for dbInfo.database configuration") do |database|
options[:database] = database.split(',')
end
end

# You might want to add methods to easily access your main functionalities
def self.backup(options = {})
commands = Backup.new(options).perform
Expand Down
2 changes: 2 additions & 0 deletions lib/mysql_db_tool/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def initialize(options = {})
@data_tables = tableConfig[:data_tables]
@ignore_tables = tableConfig[:ignore_tables]
@db_info = tableConfig[:db_info]

@db_info[:database] = @options[:database] if @options[:database]
end

def perform
Expand Down
2 changes: 2 additions & 0 deletions lib/mysql_db_tool/restore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def initialize(options = {})
@data_tables = tableConfig[:data_tables]
@ignore_tables = tableConfig[:ignore_tables]
@db_info = tableConfig[:db_info]

@db_info[:database] = @options[:database] if @options[:database]
end

def perform
Expand Down
16 changes: 16 additions & 0 deletions spec/mysql_db_tool/backup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,20 @@
end
end

describe '#perform' do

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

it 'backs up with database option' do

commands = instance.perform
expect(commands).to eq ([
"mkdir -p backup-42/0_test_db2",
"mysqldump --no-data --column-statistics=0 --ssl-mode=disabled -h my-host -u test-user test_db2 > backup-42/0_test_db2/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_db2 > backup-42/0_test_db2/2024-07-17_42-all-other-tables.sql"
])
end
end

end
31 changes: 28 additions & 3 deletions spec/mysql_db_tool/restore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
require 'mysql_db_tool/restore'

RSpec.describe MySQLDBTool::Restore do
let(:options) { { environment: 'test', backup_id: '1', run: false, drop_all_tables: false } }
let(:instance) { described_class.new(options) }

before do

end

describe '#perform' do
let(:options) { { environment: 'test', backup_id: '1', run: false, drop_all_tables: false } }
let(:instance) { described_class.new(options) }

it 'restores one db' do
allow(MySQLDBTool::Config::ConfigLoader).to receive(:load).and_return ({
db_info: { user: 'test-user', password: '', host: 'my-host', database: ['test_db-abc'] },
Expand Down Expand Up @@ -46,4 +47,28 @@
end
end

describe '#perform with database option' do
let(:options) { { environment: 'test', backup_id: '1', run: false, drop_all_tables: false, database: ['rdb-1', 'rdb-2'] } }
let(:instance) { described_class.new(options) }

it 'restores' do

allow(MySQLDBTool::Config::ConfigLoader).to receive(:load).and_return ({
db_info: { user: 'test-user', password: '', host: 'my-host', database: ['db-1', 'db-2'] },
data_tables: [],
ignore_tables: []
})
allow(Dir).to receive(:entries).with('backup-0').and_return(['.', '..', '0_db-1', '1_db-2'])
allow(Dir).to receive(:entries).with('backup-0/0_db-1').and_return(['.', '..', 'a.sql'])
allow(Dir).to receive(:entries).with('backup-0/1_db-2').and_return(['.', '..', 'b.sql'])

commands = instance.perform
puts "commands=#{commands}"
expect(commands).to eq ([
"cat backup-0/0_db-1/a.sql | ruby -pe '$_=$_.gsub(/`db-1`\\./, \"`rdb-1`.\").gsub(/`db-2`\\./, \"`rdb-2`.\")' | mysql --ssl-mode=disabled -h my-host -u test-user rdb-1 ",
"cat backup-0/1_db-2/b.sql | ruby -pe '$_=$_.gsub(/`db-1`\\./, \"`rdb-1`.\").gsub(/`db-2`\\./, \"`rdb-2`.\")' | mysql --ssl-mode=disabled -h my-host -u test-user rdb-2 "
])
end
end

end

0 comments on commit d81d0a0

Please sign in to comment.