lib/fulmar/infrastructure/service/database/database_service.rb in fulmar-0.6.5 vs lib/fulmar/infrastructure/service/database/database_service.rb in fulmar-1.0.0
- old
+ new
@@ -10,16 +10,15 @@
attr_accessor :client
attr_reader :shell
DEFAULT_CONFIG = {
maria: {
- hostname: '127.0.0.1',
+ host: '127.0.0.1',
port: 3306,
user: 'root',
password: '',
encoding: 'utf8',
- backup_path: '/tmp'
}
}
def initialize(config)
@config = config
@@ -61,15 +60,15 @@
def connected?
@connected
end
def local?
- @config[:maria][:hostname] == 'localhost' || @config[:maria][:hostname] == '127.0.0.1'
+ @config[:hostname] == 'localhost'
end
def tunnel
- @tunnel ||= Fulmar::Infrastructure::Service::TunnelService.new(@config[:maria][:hostname], @config[:maria][:port])
+ @tunnel ||= Fulmar::Infrastructure::Service::TunnelService.new(@config[:hostname], @config[:maria][:port], @config[:maria][:hostname])
end
# shortcut for DatabaseService.client.query
def query(*arguments)
@client.query(arguments)
@@ -80,37 +79,29 @@
connect unless connected?
@client.query "CREATE DATABASE IF NOT EXISTS `#{name}`"
disconnect unless state_before
end
- def dump(filename = nil)
-
- if filename
- # Ensure path is absolute
- path = filename[0, 1] == '/' ? filename : @config[:maria][:backup_path] + '/' + filename
- else
- path = @config[:maria][:backup_path] + '/' + backup_filename
- end
-
+ def dump(filename = backup_filename)
diffable = @config[:maria][:diffable_dump] ? '--skip-comments --skip-extended-insert ' : ''
- @shell.run "mysqldump -u #{@config[:maria][:user]} --password='#{@config[:maria][:password]}' " \
- "#{@config[:maria][:database]} --single-transaction #{diffable}-r \"#{path}\""
+ @shell.run "mysqldump -h #{@config[:maria][:host]} -u #{@config[:maria][:user]} --password='#{@config[:maria][:password]}' " \
+ "#{@config[:maria][:database]} --single-transaction #{diffable}-r \"#{filename}\""
- path
+ @config[:remote_path] + '/' + filename
end
def load_dump(dump_file, database = @config[:maria][:database])
- @shell.run "mysql -u #{@config[:maria][:user]} --password='#{@config[:maria][:password]}' " \
+ @shell.run "mysql -h #{@config[:maria][:host]} -u #{@config[:maria][:user]} --password='#{@config[:maria][:password]}' " \
"-D #{database} < #{dump_file}"
end
def download_dump(filename = backup_filename)
local_path = filename[0, 1] == '/' ? filename : @config[:local_path] + '/' + filename
remote_path = dump
- copy = system("scp -q #{@config[:maria][:hostname]}:#{remote_path} #{local_path}")
- system("ssh #{@config[:maria][:hostname]} 'rm -f #{remote_path}'") # delete temporary file
+ copy = system("scp -q #{@config[:hostname]}:#{remote_path} #{local_path}")
+ @shell.run "rm -f \"#{remote_path}\"" # delete temporary file
if copy
local_path
else
''
end
@@ -119,11 +110,10 @@
protected
# Test configuration
def config_test
fail 'Configuration option "database" missing.' unless @config[:maria][:database]
- @shell.run "test -d '#{@config[:maria][:backup_path]}'"
end
# Builds the filename for a new database backup file
# NOTE: The file might already exist, for example if this is run at the same
# time from to different clients. I won't handle this as it is unlikely and
@@ -134,9 +124,10 @@
def initialize_shell
path = local? ? @config[:local_path] : @config[:remote_path]
@shell = Fulmar::Infrastructure::Service::ShellService.new(path, @config[:hostname])
@shell.debug = true if @config[:debug]
+ @shell.strict = true
end
# Compiles a mysql config hash from valid options of the fulmar config
def compile_options
possible_options = [:host, :username, :password, :port, :encoding, :socket, :read_timeout, :write_timeout,