Class: Testjour::MysqlDatabaseSetup
- Inherits:
-
Object
- Object
- Testjour::MysqlDatabaseSetup
- Defined in:
- lib/testjour/mysql.rb
Overview
Stolen from deep-test
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
- #connect ⇒ Object
- #create_database ⇒ Object
- #drop_database ⇒ Object
- #grant_privileges(connection) ⇒ Object
-
#initialize(runner_database_name = nil) ⇒ MysqlDatabaseSetup
constructor
A new instance of MysqlDatabaseSetup.
- #load_schema ⇒ Object
- #runner_database_name ⇒ Object
Constructor Details
#initialize(runner_database_name = nil) ⇒ MysqlDatabaseSetup
Returns a new instance of MysqlDatabaseSetup.
25 26 27 |
# File 'lib/testjour/mysql.rb', line 25 def initialize(runner_database_name = nil) @runner_database_name = runner_database_name end |
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.
15 16 17 |
# File 'lib/testjour/mysql.rb', line 15 def admin_configuration @admin_configuration end |
Instance Method Details
#admin_connection ⇒ Object
61 62 63 64 65 66 |
# File 'lib/testjour/mysql.rb', line 61 def admin_connection conn = ActiveRecord::Base.mysql_connection(self.class.admin_configuration) yield conn ensure conn.disconnect! if conn end |
#connect ⇒ Object
51 52 53 |
# File 'lib/testjour/mysql.rb', line 51 def connect ActiveRecord::Base.establish_connection(self.class.admin_configuration.merge(:database => runner_database_name)) end |
#create_database ⇒ Object
38 39 40 41 42 43 |
# File 'lib/testjour/mysql.rb', line 38 def create_database admin_connection do |connection| connection.recreate_database runner_database_name grant_privileges(connection) end end |
#drop_database ⇒ Object
45 46 47 48 49 |
# File 'lib/testjour/mysql.rb', line 45 def drop_database admin_connection do |connection| connection.drop_database runner_database_name end end |
#grant_privileges(connection) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/testjour/mysql.rb', line 29 def grant_privileges(connection) sql = %{grant all on #{runner_database_name}.* to %s@'localhost' identified by %s;} % [ connection.quote(self.class.admin_configuration[:username]), connection.quote(self.class.admin_configuration[:password] || "") ] connection.execute sql end |
#load_schema ⇒ Object
55 56 57 58 59 |
# File 'lib/testjour/mysql.rb', line 55 def load_schema # silence_stream(STDOUT) do load File.join(RAILS_ROOT, "db", "schema.rb") # end end |
#runner_database_name ⇒ Object
68 69 70 |
# File 'lib/testjour/mysql.rb', line 68 def runner_database_name @runner_database_name ||= "testjour_runner_#{rand(1_000)}" end |