class Fulmar::Plugin::MariaDB::Database

Provides basic methods common to all database services

Constants

DEFAULT_CONFIG

Attributes

local_shell[R]
shell[R]

Public Class Methods

new(config, shell = initialize_shell, local_shell = nil) click to toggle source
# File lib/fulmar/plugin/mariadb/database.rb, line 23
def initialize(config, shell = initialize_shell, local_shell = nil)
  @config = config
  @config.merge DEFAULT_CONFIG
  @shell = shell
  @local_shell = local_shell || Fulmar::Shell.new(@config[:mariadb][:dump_path])
  @local_shell.debug = true if config[:debug] # do not deactivate debug mode is shell set it explicitly
  config_test
end

Public Instance Methods

command(binary) click to toggle source

Add parameters like host, user, and password to the base command like mysql or mysqldump

# File lib/fulmar/plugin/mariadb/database.rb, line 50
def command(binary)
  command = binary
  command << " -h #{@config[:mariadb][:host]}" unless @config[:mariadb][:host].blank?
  command << " -u #{@config[:mariadb][:user]}" unless @config[:mariadb][:user].blank?
  command << " --password='#{@config[:mariadb][:password]}'" unless @config[:mariadb][:password].blank?
  command
end
create(name, user = nil, password = nil, host = 'localhost') click to toggle source
# File lib/fulmar/plugin/mariadb/database.rb, line 32
def create(name, user = nil, password = nil, host = 'localhost')
  query "CREATE DATABASE IF NOT EXISTS `#{name}`"
  if user
    password_sql = password ? " IDENTIFIED BY \"#{password}\"" : ''
    query "GRANT ALL ON `#{name}`.* TO #{user}@#{host}#{password_sql}"
  end
end
download_dump(filename = " click to toggle source
# File lib/fulmar/plugin/mariadb/database.rb, line 72
def download_dump(filename = "#{backup_filename}.gz")
  local_path = filename[0, 1] == '/' ? filename : "#{@config[:mariadb][:dump_path]}/#{filename}"
  remote_command = "#{dump_command} | gzip"
  @local_shell.run "ssh #{@config.ssh_user_and_host} \"#{remote_command}\" > #{local_path}"
  local_path
end
dump(filename = backup_filename) click to toggle source
# File lib/fulmar/plugin/mariadb/database.rb, line 58
def dump(filename = backup_filename)
  filename = "#{@config[:mariadb][:dump_path]}/#{filename}" unless filename[0, 1] == '/'
  @shell.run "#{dump_command} -r \"#{filename}\""
  filename
end
load_dump(dump_file, database = @config[:mariadb][:database]) click to toggle source
# File lib/fulmar/plugin/mariadb/database.rb, line 64
def load_dump(dump_file, database = @config[:mariadb][:database])
  if File.extname(dump_file) == '.gz'
    @shell.run "cat #{dump_file} | gzip -d | #{command('mysql')} -D #{database}"
  else
    @shell.run "#{command('mysql')} -D #{database} < #{dump_file}"
  end
end
query(sql_query) click to toggle source
# File lib/fulmar/plugin/mariadb/database.rb, line 40
def query(sql_query)
  if sql_query.include?('\'')
    fail 'This fulmar plugin currently does no support queries with single quotes to the simple '\
         " quoting in fulmar shell. Query was: #{sql_query}"
  end

  @shell.run "#{command('mysql')} -D '#{@config[:mariadb][:database]}' -e '#{sql_query}'"
end

Protected Instance Methods

backup_filename() click to toggle source

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 would result in more I/O

# File lib/fulmar/plugin/mariadb/database.rb, line 107
def backup_filename
  "#{@config[:mariadb][:database]}_#{Time.now.strftime('%Y-%m-%dT%H%M%S')}.sql"
end
config_test() click to toggle source

Test configuration

# File lib/fulmar/plugin/mariadb/database.rb, line 99
def config_test
  fail 'Configuration option "database" missing.' unless @config[:mariadb][:database]
end
diffable() click to toggle source

Return the mysql configuration options to make a dump diffable

# File lib/fulmar/plugin/mariadb/database.rb, line 94
def diffable
  @config[:mariadb][:diffable_dump] ? '--skip-comments --skip-extended-insert ' : ''
end
dump_command() click to toggle source
# File lib/fulmar/plugin/mariadb/database.rb, line 81
def dump_command
  "#{command('mysqldump')} #{@config[:mariadb][:database]} --single-transaction #{diffable} #{ignore_tables}"
end
ignore_tables() click to toggle source

Return mysql command line options to ignore specific tables

# File lib/fulmar/plugin/mariadb/database.rb, line 86
def ignore_tables
  @config[:mariadb][:ignore_tables] = [*@config[:mariadb][:ignore_tables]]
  @config[:mariadb][:ignore_tables].map do |table|
    "--ignore-table=#{@config[:mariadb][:database]}.#{table}"
  end.join(' ')
end
initialize_shell() click to toggle source
# File lib/fulmar/plugin/mariadb/database.rb, line 111
def initialize_shell
  @shell = Fulmar::Shell.new(@config[:mariadb][:dump_path], @config.ssh_user_and_host)
  @shell.debug = true if @config[:debug]
  @shell.strict = true
end