Class: Fulmar::Plugin::MariaDB::Database
- Inherits:
-
Object
- Object
- Fulmar::Plugin::MariaDB::Database
- Defined in:
- lib/fulmar/plugin/mariadb/database.rb
Overview
Provides basic methods common to all database services
Constant Summary collapse
- DEFAULT_CONFIG =
{ mariadb: { host: '127.0.0.1', port: 3306, user: 'root', password: '', encoding: 'utf8', ignore_tables: [], diffable_dump: false, dump_path: '/tmp' } }
Instance Attribute Summary collapse
-
#local_shell ⇒ Object
readonly
Returns the value of attribute local_shell.
-
#shell ⇒ Object
readonly
Returns the value of attribute shell.
Instance Method Summary collapse
-
#command(binary) ⇒ Object
Add parameters like host, user, and password to the base command like mysql or mysqldump.
- #create(name, user = nil, password = nil, host = 'localhost') ⇒ Object
- #download_dump(filename = "#{backup_filename}.gz") ⇒ Object
- #dump(filename = backup_filename) ⇒ Object
-
#initialize(config, shell = initialize_shell, local_shell = nil) ⇒ Database
constructor
A new instance of Database.
- #load_dump(dump_file, database = @config[:mariadb]) ⇒ Object
- #query(sql_query) ⇒ Object
Constructor Details
#initialize(config, shell = initialize_shell, local_shell = nil) ⇒ Database
Returns a new instance of Database.
23 24 25 26 27 28 29 30 |
# 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 |
Instance Attribute Details
#local_shell ⇒ Object (readonly)
Returns the value of attribute local_shell.
8 9 10 |
# File 'lib/fulmar/plugin/mariadb/database.rb', line 8 def local_shell @local_shell end |
#shell ⇒ Object (readonly)
Returns the value of attribute shell.
8 9 10 |
# File 'lib/fulmar/plugin/mariadb/database.rb', line 8 def shell @shell end |
Instance Method Details
#command(binary) ⇒ Object
Add parameters like host, user, and password to the base command like mysql or mysqldump
50 51 52 53 54 55 56 |
# 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') ⇒ Object
32 33 34 35 36 37 38 |
# 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 = "#{backup_filename}.gz") ⇒ Object
72 73 74 75 76 77 |
# 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) ⇒ Object
58 59 60 61 62 |
# 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]) ⇒ Object
64 65 66 67 68 69 70 |
# 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) ⇒ Object
40 41 42 43 44 45 46 47 |
# 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 |