lib/koine/db_bkp/mysql/dump.rb in koine-db_bkp-0.1.0 vs lib/koine/db_bkp/mysql/dump.rb in koine-db_bkp-0.1.1
- old
+ new
@@ -1,27 +1,62 @@
+require 'addressable/uri'
+
module Koine
module DbBkp
module Mysql
class Dump
def initialize(config = {})
- config = config.reject { |_k, v| ['', nil].include?(v) }
+ config = normalize_config(config)
- @hostname = config[:hostname]
+ @hostname = config.fetch(:hostname)
@database = config.fetch(:database)
+ @username = config[:username]
@password = config[:password]
@cli = Cli.new
end
def to_sql_file(file)
parts = ['mysqldump']
parts.push("-h #{@hostname}") if @hostname
+ parts.push("-u #{@username}") if @username
parts.push("-p#{@password}") if @password
parts.push(@database)
+
+ file = FileName.new(file)
parts.push("> #{file}")
@cli.execute(parts.join(' '))
+ end
+
+ private
+
+ def normalize_config(config)
+ config = config.reject { |_k, v| ['', nil].include?(v) }
+ merge_url(symbolize_keys(config))
+ end
+
+ def merge_url(config)
+ url = config.delete(:url)
+
+ return config unless url
+
+ url = Addressable::URI.parse(url)
+
+ config.merge(
+ adapter: url.scheme,
+ hostname: url.host,
+ database: url.path.split('/').join(''),
+ username: url.user,
+ password: url.password
+ )
+ end
+
+ def symbolize_keys(hash)
+ {}.tap do |new_hash|
+ hash.each { |key, value| new_hash[key.to_sym] = value }
+ end
end
end
end
end
end