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:



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

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:



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

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



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

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



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

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

#dump_schemaObject

Dumps schema from master database using mysqldump command



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

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

def grant_privileges(connection)
  puts "==== #{worker_database_config[:password].inspect}"
  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



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

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:



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

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