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_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:



100
101
102
103
104
105
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 100

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

#command_line_config(config) ⇒ Object

:nodoc:



90
91
92
93
94
95
96
97
98
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 90

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



52
53
54
55
56
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 52

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



81
82
83
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 81

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

#dump_schemaObject

Dumps schema from master database using mysqldump command



61
62
63
64
65
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 61

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
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 40

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

#load_schemaObject

Loads dumpfile into worker database using mysql command



70
71
72
73
74
# File 'lib/deep_test/database/mysql_setup_listener.rb', line 70

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:



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

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