Class: DeepTest::Database::MysqlSetupListener

Inherits:
SetupListener show all
Defined in:
lib/deep_test/database/mysql_setup_listener.rb

Overview

SetupListener implementation for MySQL.

Constant Summary

Constants inherited from SetupListener

SetupListener::DUMPED_SCHEMAS

Class Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SetupListener

#before_starting_workers, #before_sync, #connect_to_database, #dump_schema_once, #master_database_config, #starting, #worker_database, #worker_database_config

Methods inherited from NullWorkerListener

#before_remote_load_files, #before_starting_workers, #before_sync, #finished_work, #starting, #starting_work

Class Attribute Details

.admin_configurationObject

ActiveRecord configuration to use when connecting to MySQL to create databases, drop database, and grant privileges. By default, connects to information_schema on localhost as root with no password.



14
15
16
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 14

def admin_configuration
  @admin_configuration
end

Instance Method Details

#admin_connectionObject

:nodoc:



103
104
105
106
107
108
109
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 103

def admin_connection # :nodoc:
  ActiveRecord::Base.establish_connection(self.class.admin_configuration)
  conn = ActiveRecord::Base.connection
  yield conn
ensure
  conn.disconnect! if conn
end

#command_line_config(config) ⇒ Object

:nodoc:



93
94
95
96
97
98
99
100
101
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 93

def command_line_config(config) # :nodoc:
  command =  ['-u', config["username"]]
  command += ["-p#{config["password"]}"] if config["password"]
  command += ['-h', config["host"]] if config["host"]
  command += ['-P', config["port"]] if config["port"]
  command += ['-S', config["socket"]] if config["socket"]
  command += [config["database"]]
  command.join(' ')
end

#create_databaseObject

Creates database and grants privileges (via grant_privileges) on it via ActiveRecord connection based on admin_configuration.



27
28
29
30
31
32
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 27

def create_database
  admin_connection do |connection|
    connection.create_database worker_database
    grant_privileges connection
  end
end

#drop_databaseObject

Drops database via ActiveRecord connection based on admin_configuration



55
56
57
58
59
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 55

def drop_database
  admin_connection do |connection|
    connection.drop_database worker_database
  end
end

#dump_file_nameObject

Location to store dumpfile. The default assumes you are testing a Rails project. You should override this if you are not using Rails or would like the dump file to be something other than the default



84
85
86
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 84

def dump_file_name
  "#{RAILS_ROOT}/db/deep_test_schema.sql"
end

#dump_schemaObject

Dumps schema from master database using mysqldump command



64
65
66
67
68
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 64

def dump_schema
  config = command_line_config(master_database_config)
  system "mysqldump -R #{config} > #{dump_file_name}"
  raise "Error Dumping schema" unless $?.success?
end

#grant_privileges(connection) ⇒ Object

Grants ‘all’ privilege on worker database to username and password specified by worker database config. If your application has special database privilege needs beyond ‘all’, you should override this method and grant them.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 40

def grant_privileges(connection)
  identified_by = if worker_database_config["password"]
                    %{identified by %s} % connection.quote(worker_database_config["password"])
                  else
                    ""
                  end
  sql = %{grant all on #{worker_database}.* to %s@'localhost' #{identified_by} ; } % 
    connection.quote(worker_database_config["username"])

  connection.execute sql
end

#load_schemaObject

Loads dumpfile into worker database using mysql command



73
74
75
76
77
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 73

def load_schema
  config = command_line_config(worker_database_config)
  system "mysql #{config} < #{dump_file_name}"
  raise "Error Loading schema" unless $?.success?
end

#system(command) ⇒ Object

:nodoc:



88
89
90
91
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 88

def system(command) # :nodoc:
  DeepTest.logger.info command
  super command
end