Class: Snapback::Database

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/snapback/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



10
11
12
# File 'lib/snapback/database.rb', line 10

def hostname
  @hostname
end

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/snapback/database.rb', line 10

def password
  @password
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/snapback/database.rb', line 10

def username
  @username
end

Instance Method Details

#connectObject



18
19
20
# File 'lib/snapback/database.rb', line 18

def connect
  @connection = Mysql.new @hostname, @username, @password
end

#db_create(database_name) ⇒ Object



73
74
75
76
# File 'lib/snapback/database.rb', line 73

def db_create database_name
  @connection.query "CREATE DATABASE `#{Mysql.escape_string database_name}`"
  true
end

#db_drop(database_name) ⇒ Object



78
79
80
81
# File 'lib/snapback/database.rb', line 78

def db_drop database_name
  @connection.query "DROP DATABASE `#{Mysql.escape_string database_name}`"
  true
end

#db_exists?(database_name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/snapback/database.rb', line 68

def db_exists? database_name
  sql = @connection.list_dbs database_name
  sql.size == 1
end

#db_use(database_name) ⇒ Object



83
84
85
86
# File 'lib/snapback/database.rb', line 83

def db_use database_name
  @connection.select_db database_name
  true
end

#flush_lockObject



22
23
24
25
26
27
28
# File 'lib/snapback/database.rb', line 22

def flush_lock
  begin
    @connection.query "FLUSH TABLES WITH READ LOCK"
  end

  true
end

#get_data_dirObject



38
39
40
41
42
43
44
45
# File 'lib/snapback/database.rb', line 38

def get_data_dir
  begin
    rs = @connection.query "SHOW VARIABLES LIKE 'datadir'"
    rs.each_hash do |row|
      return row['Value'].gsub(/\/$/, '')
    end
  end
end

#innodb_file_per_tableObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/snapback/database.rb', line 47

def innodb_file_per_table
  begin
    rs = @connection.query "SHOW VARIABLES LIKE 'innodb_file_per_table'"
    rs.each_hash do |row|
      return row['Value'] == "ON"
    end
  rescue
    raise "MySQL Query failed"
  end
end

#server_startObject



94
95
96
97
98
99
100
# File 'lib/snapback/database.rb', line 94

def server_start
  run "Start MySQL server and reconnect",
    "service mysql start"

  self.connect
  true
end

#server_stopObject



88
89
90
91
92
# File 'lib/snapback/database.rb', line 88

def server_stop
  run "Stop MySQL server",
    "service mysql stop"
  true
end

#set_innodb_file_per_table(on) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/snapback/database.rb', line 58

def set_innodb_file_per_table on
  begin
    rs = @connection.prepare "SET GLOBAL innodb_file_per_table = ?"
    rs.execute(on ? 'ON' : 'OFF')
    return true
  rescue
    raise "MySQL Query failed"
  end
end

#unlockObject



30
31
32
33
34
35
36
# File 'lib/snapback/database.rb', line 30

def unlock
  begin
    @connection.query "UNLOCK TABLES"
  end

  true
end