Class: DeepTest::Database::SetupListener
- Inherits:
-
NullWorkerListener
- Object
- NullWorkerListener
- DeepTest::Database::SetupListener
- Defined in:
- lib/deep_test/database/setup_listener.rb
Overview
Skeleton Listener to help with setting up a separate database for each worker. Calls dump_schema
, load_schema
, create_database
, and drop_database
hooks provided by subclasses that implement database setup strategies for particular database flavors.
Direct Known Subclasses
Constant Summary collapse
- DUMPED_SCHEMAS =
[]
Instance Method Summary collapse
-
#before_starting_workers ⇒ Object
:nodoc:.
-
#before_sync ⇒ Object
:nodoc:.
-
#connect_to_database ⇒ Object
Called on each worker after creating database and before loading schema to initialize connections.
-
#create_database ⇒ Object
Called in each worker to create the database named by
worker_database
. -
#drop_database ⇒ Object
Called in each worker to drop the database created by
create_database
. -
#dump_schema ⇒ Object
Called before any workers are spawned to dump the schema that will be used for testing.
-
#dump_schema_once ⇒ Object
:nodoc:.
-
#load_schema ⇒ Object
Called once in each worker as it is starting to load the schema dumped from dump_schema.
-
#master_database_config ⇒ Object
ActiveRecord configuration for the master database, based on RAILS_ENV.
-
#starting(worker) ⇒ Object
:nodoc:.
-
#worker_database ⇒ Object
Unique name for database on machine that worker is running on.
-
#worker_database_config ⇒ Object
ActiveRecord configuration for the worker database.
Methods inherited from NullWorkerListener
#finished_work, #starting_work
Instance Method Details
#before_starting_workers ⇒ Object
:nodoc:
16 17 18 |
# File 'lib/deep_test/database/setup_listener.rb', line 16 def before_starting_workers # :nodoc: dump_schema_once end |
#before_sync ⇒ Object
:nodoc:
12 13 14 |
# File 'lib/deep_test/database/setup_listener.rb', line 12 def before_sync # :nodoc: dump_schema_once end |
#connect_to_database ⇒ Object
Called on each worker after creating database and before loading schema to initialize connections
44 45 46 |
# File 'lib/deep_test/database/setup_listener.rb', line 44 def connect_to_database ActiveRecord::Base.establish_connection(worker_database_config) end |
#create_database ⇒ Object
Called in each worker to create the database named by worker_database
.
52 53 54 |
# File 'lib/deep_test/database/setup_listener.rb', line 52 def create_database raise "Subclass must implement" end |
#drop_database ⇒ Object
Called in each worker to drop the database created by create_database
. This method is called twice, once before create_database
to ensure that no database exists and once at exit to clean as the worker process exits. This method must not fail if the database does not exist when it is called.
63 64 65 |
# File 'lib/deep_test/database/setup_listener.rb', line 63 def drop_database raise "Subclass must implement" end |
#dump_schema ⇒ Object
Called before any workers are spawned to dump the schema that will be used for testing. When running distributed, this method is called on the local machine providing the tests to run.
For distributed testing to work, the schema must be dumped in location accessible by all worker machines. The easiest way to accomplish this is to dump it to a location within the working copy.
76 77 78 |
# File 'lib/deep_test/database/setup_listener.rb', line 76 def dump_schema raise "Subclass must implement" end |
#dump_schema_once ⇒ Object
:nodoc:
20 21 22 23 24 |
# File 'lib/deep_test/database/setup_listener.rb', line 20 def dump_schema_once # :nodoc: schema_name = master_database_config[:database] dump_schema unless DUMPED_SCHEMAS.include?(schema_name) DUMPED_SCHEMAS << schema_name end |
#load_schema ⇒ Object
Called once in each worker as it is starting to load the schema dumped from dump_schema. Subclasses should load the schema definition into the worker_database
86 87 88 |
# File 'lib/deep_test/database/setup_listener.rb', line 86 def load_schema raise "Subclass must implement" end |
#master_database_config ⇒ Object
ActiveRecord configuration for the master database, based on RAILS_ENV. If not running Rails, you’ll need to override this to provide the correct configuration.
104 105 106 |
# File 'lib/deep_test/database/setup_listener.rb', line 104 def master_database_config ActiveRecord::Base.configurations[RAILS_ENV].with_indifferent_access end |
#starting(worker) ⇒ Object
:nodoc:
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/deep_test/database/setup_listener.rb', line 26 def starting(worker) # :nodoc: @worker = worker at_exit do DeepTest.logger.debug("dropping database #{worker_database}") drop_database end drop_database create_database connect_to_database load_schema end |
#worker_database ⇒ Object
Unique name for database on machine that worker is running on.
111 112 113 |
# File 'lib/deep_test/database/setup_listener.rb', line 111 def worker_database "deep_test_worker_#{@worker.number}_pid_#{Process.pid}" end |
#worker_database_config ⇒ Object
ActiveRecord configuration for the worker database. By default, the same as master_database_config
, except that points to worker_database
instead of the database named in the master config.
95 96 97 |
# File 'lib/deep_test/database/setup_listener.rb', line 95 def worker_database_config master_database_config.merge(:database => worker_database) end |