Class: IsolatedServer::Mysql

Inherits:
Base
  • Object
show all
Includes:
DBConnection
Defined in:
lib/isolated_server/mysql.rb,
lib/isolated_server/mysql/jdbc_connection.rb,
lib/isolated_server/mysql/mysql2_connection.rb

Defined Under Namespace

Modules: DBConnection Classes: WrappedJDBCConnection

Instance Attribute Summary collapse

Attributes inherited from Base

#base, #params, #pid, #port

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DBConnection

#connection

Methods inherited from Base

#cleanup!, #down!, #exec_server, exec_wait, get_free_port, #grab_free_port, #kill!, #locate_executable

Constructor Details

#initialize(options = {}) ⇒ Mysql

Returns a new instance of Mysql.



16
17
18
19
20
21
22
23
# File 'lib/isolated_server/mysql.rb', line 16

def initialize(options = {})
  super options
  @mysql_data_dir = "#{@base}/mysqld"
  @mysql_socket   = "#{@mysql_data_dir}/mysqld.sock"
  @load_data_path = options[:data_path]
  @log_bin        = options[:log_bin] || "--log-bin"
  @server_id      = rand(2**31)
end

Instance Attribute Details

#initial_binlog_fileObject (readonly)

Returns the value of attribute initial_binlog_file.



14
15
16
# File 'lib/isolated_server/mysql.rb', line 14

def initial_binlog_file
  @initial_binlog_file
end

#initial_binlog_posObject (readonly)

Returns the value of attribute initial_binlog_pos.



14
15
16
# File 'lib/isolated_server/mysql.rb', line 14

def initial_binlog_pos
  @initial_binlog_pos
end

#mysql_data_dirObject (readonly)

Returns the value of attribute mysql_data_dir.



14
15
16
# File 'lib/isolated_server/mysql.rb', line 14

def mysql_data_dir
  @mysql_data_dir
end

#server_idObject (readonly)

Returns the value of attribute server_id.



14
15
16
# File 'lib/isolated_server/mysql.rb', line 14

def server_id
  @server_id
end

Class Method Details

.thread_boot(*params) ⇒ Object

TODO:

Extract and genericize more of this into ‘Base`

For JRuby



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/isolated_server/mysql.rb', line 27

def self.thread_boot(*params)
  bin = [File.dirname(__FILE__) + "/../bin/boot_isolated_mysql_server"]
  mysql_dir, mysql_port = nil, nil
  restore_env = {}

  if `which ruby` =~ (/rvm/)
    bin = ["rvm", "1.8.7", "do", "ruby"] + bin
  end

  params = ["--pid", $$.to_s] + params

  Thread.abort_on_exception = true
  Thread.new do
    ENV.keys.grep(/GEM|BUNDLE|RUBYOPT/).each do |k|
      restore_env[k] = ENV.delete(k)
    end
    IO.popen(bin + params, "r") do |pipe|
      mysql_dir = pipe.readline.split(' ').last
      mysql_port = pipe.readline.split(' ').last.to_i
      sleep
    end
  end

  while mysql_port.nil?
    sleep 1
  end
  new(:port => mysql_port, :base => mysql_dir)
end

Instance Method Details

#boot!Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/isolated_server/mysql.rb', line 56

def boot!
  @port ||= grab_free_port

  setup_data_dir
  setup_binlog
  setup_tmp_dir

  up!

  record_initial_master_position

  setup_time_zone
  setup_server_id
end

#consoleObject



92
93
94
# File 'lib/isolated_server/mysql.rb', line 92

def console
  system("mysql -uroot --port #{@port.to_s.shellescape} mysql --host 127.0.0.1")
end

#make_slave_of(master) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/isolated_server/mysql.rb', line 101

def make_slave_of(master)
  binlog_file = master.initial_binlog_file || (@log_bin.split('/').last + ".000001")
  binlog_pos = master.initial_binlog_pos || 4

  connection.query(<<-EOL
    CHANGE MASTER TO MASTER_HOST='127.0.0.1',
                     MASTER_PORT=#{master.port},
                     MASTER_USER='root', MASTER_PASSWORD='',
                     MASTER_LOG_FILE='#{binlog_file}',
                     MASTER_LOG_POS=#{binlog_pos}
    EOL
  )
  connection.query("SLAVE START")
  connection.query("SET GLOBAL READ_ONLY=1")
end

#reconnect!Object



96
97
98
99
# File 'lib/isolated_server/mysql.rb', line 96

def reconnect!
  @cx = nil
  connection
end

#set_rw(rw) ⇒ Object



117
118
119
120
# File 'lib/isolated_server/mysql.rb', line 117

def set_rw(rw)
  ro = rw ? 0 : 1
  connection.query("SET GLOBAL READ_ONLY=#{ro}")
end

#up!Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/isolated_server/mysql.rb', line 71

def up!
  exec_server([
    locate_executable("mysqld"),
    '--no-defaults',
    '--default-storage-engine=innodb',
    "--datadir=#{@mysql_data_dir}",
    "--pid-file=#{@base}/mysqld.pid",
    "--port=#{@port}",
    "--socket=#{@mysql_data_dir}/mysql.sock",
    @log_bin,
    '--log-slave-updates',
    *@params
  ].join(' '))

  sleep(0.1) until up?
end

#up?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/isolated_server/mysql.rb', line 88

def up?
  system("mysql -h127.0.0.1 --port=#{@port.to_s.shellescape} --database=mysql -u root -e 'select 1' >/dev/null 2>&1")
end