Class: VGH::System::MySQL

Inherits:
Object
  • Object
show all
Defined in:
lib/vgh/system/mysql.rb

Overview

This class checks if a local MySQL server is present and running. The credentials need to be specified in the app’s configuration file.

Usage

mysql = MySQL.new
mysql.flush
# run backup
mysql.unlock

Instance Method Summary collapse

Constructor Details

#initializeMySQL

Load defaults



18
19
20
21
# File 'lib/vgh/system/mysql.rb', line 18

def initialize
  @mysqladmin_cmd = '/usr/bin/mysqladmin'
  @mysql_cmd      = '/usr/bin/mysql'
end

Instance Method Details

#commands_present?Boolean

Check if server is running and we have the right credentials

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/vgh/system/mysql.rb', line 37

def commands_present?
  commands_present = false
  if File.exists?(@mysqladmin_cmd) and File.exists?(@mysql_cmd)
    commands_present = true
  end
  return commands_present
end

#flushObject

Lock & Flush the MySQL tables



61
62
63
64
65
66
# File 'lib/vgh/system/mysql.rb', line 61

def flush
  if mysql_exists?
    message.info 'Locking MySQL tables...'
    `#{@mysql} -u#{mysql_user} -p#{mysql_password} -e "FLUSH TABLES WITH READ LOCK"`
  end
end

#mysql_exists?Boolean

Check if server is running and we have the right credentials

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vgh/system/mysql.rb', line 47

def mysql_exists?
  mysql_exists = false
  server_present = system "#{@mysqladmin_cmd} -s ping" if commands_present?
  if server_present and mysql_user.nil? and mysql_password.nil?
    message.warn 'WARNING: MySQL exists but no credentials were found!'
  elsif ! server_present and ! mysql_user.nil? and ! mysql_password.nil?
    message.warn 'WARNING: MySQL credentials exist but no local server was found!'
  elsif server_present and ! mysql_user.nil? and ! mysql_password.nil?
    mysql_exists = true
  end
  return mysql_exists
end

#mysql_passwordString

Get MySQL password

Returns:

  • (String)


31
32
33
# File 'lib/vgh/system/mysql.rb', line 31

def mysql_password
  @mysql_password ||= config[:mysql_password]
end

#mysql_userString

Get MySQL user

Returns:

  • (String)


25
26
27
# File 'lib/vgh/system/mysql.rb', line 25

def mysql_user
  @mysql_user ||= config[:mysql_user]
end

#unlockObject

Unlock the MySQL tables



69
70
71
72
73
74
# File 'lib/vgh/system/mysql.rb', line 69

def unlock
  if mysql_exists?
    message.info 'Unlocking MySQL tables...'
    `#{@mysql} -u#{mysql_user} -p#{mysql_password} -e "UNLOCK TABLES"`
  end
end