Class: DeepTest::Database::MysqlSetupListener
- Inherits:
-
SetupListener
- Object
- NullWorkerListener
- SetupListener
- DeepTest::Database::MysqlSetupListener
- Defined in:
- lib/deep_test/database/mysql_setup_listener.rb
Overview
SetupListener implementation for MySQL.
Constant Summary
Constants inherited from SetupListener
Class Attribute Summary collapse
-
.admin_configuration ⇒ Object
ActiveRecord configuration to use when connecting to MySQL to create databases, drop database, and grant privileges.
Instance Method Summary collapse
-
#admin_connection ⇒ Object
:nodoc:.
-
#command_line_config(config) ⇒ Object
:nodoc:.
-
#create_database ⇒ Object
Creates database and grants privileges (via
grant_privileges
) on it via ActiveRecord connection based on admin_configuration. -
#drop_database ⇒ Object
Drops database via ActiveRecord connection based on admin_configuration.
-
#dump_file_name ⇒ Object
Location to store dumpfile.
-
#dump_schema ⇒ Object
Dumps schema from master database using mysqldump command.
-
#grant_privileges(connection) ⇒ Object
Grants ‘all’ privilege on worker database to username and password specified by worker database config.
-
#load_schema ⇒ Object
Loads dumpfile into worker database using mysql command.
-
#system(command) ⇒ Object
:nodoc:.
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_configuration ⇒ Object
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_connection ⇒ Object
: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_database ⇒ Object
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_database ⇒ Object
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_name ⇒ Object
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_schema ⇒ Object
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_schema ⇒ Object
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 |