class Snapback::MySQL::ClientControl

Attributes

hostname[RW]
password[RW]
username[RW]

Public Instance Methods

connect() click to toggle source
# File lib/snapback/mysql/client_control.rb, line 19
def connect
  @connection = Mysql.new @hostname, @username, @password
end
database_create(database) click to toggle source
# File lib/snapback/mysql/client_control.rb, line 62
def database_create(database)
  begin
    @connection.query "CREATE DATABASE `#{Mysql.escape_string database}`"
    true
  rescue
    false
  end
end
database_exists?(database) click to toggle source

Database

# File lib/snapback/mysql/client_control.rb, line 58
def database_exists?(database)
  @connection.list_dbs(database).size == 1
end
database_select(database) click to toggle source
# File lib/snapback/mysql/client_control.rb, line 71
def database_select(database)
  @connection.select_db(database)
end
database_tables() click to toggle source
# File lib/snapback/mysql/client_control.rb, line 75
def database_tables
  @connection.query "SHOW TABLES"
end
flush_tables() click to toggle source

Maintenance

# File lib/snapback/mysql/client_control.rb, line 25
def flush_tables
  @connection.query "FLUSH TABLES WITH READ LOCK"
  true
end
get_data_directory() click to toggle source

Configuration

# File lib/snapback/mysql/client_control.rb, line 37
def get_data_directory
  rs = @connection.query "SHOW VARIABLES LIKE 'datadir'"
  rs.each_hash do |row|
    return row['Value'].gsub(/\/$/, '')
  end
end
has_innodb_file_per_table?() click to toggle source
# File lib/snapback/mysql/client_control.rb, line 44
def has_innodb_file_per_table?
  rs = @connection.query "SHOW VARIABLES LIKE 'innodb_file_per_table'"
  rs.each_hash do |row|
    return row['Value'] == "ON"
  end
end
set_innodb_file_per_table(on) click to toggle source
# File lib/snapback/mysql/client_control.rb, line 51
def set_innodb_file_per_table(on)
  rs = @connection.prepare "SET GLOBAL innodb_file_per_table = ?"
  rs.execute(on ? 'ON' : 'OFF')
end
table_drop(table) click to toggle source

Tables

# File lib/snapback/mysql/client_control.rb, line 81
def table_drop(table)
  begin
    @connection.query "DROP TABLE `#{Mysql.escape_string table}`"
    true
  rescue
    false
  end
end
table_optimize(table) click to toggle source
# File lib/snapback/mysql/client_control.rb, line 90
def table_optimize(table)
  begin
    @connection.query "ALTER TABLE `#{Mysql.escape_string table}` IMPORT TABLESPACE"
  rescue
    # If this fails, it's usually just because the table is not InnoDB, so ignore errors
  end

  begin
    @connection.query "OPTIMIZE TABLE `#{Mysql.escape_string table}`"
    true
  rescue
    false
  end
end
table_set_engine(table, engine) click to toggle source
# File lib/snapback/mysql/client_control.rb, line 105
def table_set_engine(table, engine)
  begin
    @connection.query "ALTER TABLE `#{Mysql.escape_string table}` ENGINE = #{Mysql.escape_string engine};"
    true
  rescue
    false
  end
end
tablespace_discard(table) click to toggle source

Tablespace

# File lib/snapback/mysql/client_control.rb, line 115
def tablespace_discard(table)
  begin
    @connection.query "ALTER TABLE `#{Mysql.escape_string table}` DISCARD TABLESPACE"
    true
  rescue
    false
  end
end
unlock_tables() click to toggle source
# File lib/snapback/mysql/client_control.rb, line 30
def unlock_tables
  @connection.query "UNLOCK TABLES"
  true
end