Skip to content

Instantly share code, notes, and snippets.

@bmpercy
Created February 22, 2012 04:51
Show Gist options
  • Save bmpercy/1881527 to your computer and use it in GitHub Desktop.
Save bmpercy/1881527 to your computer and use it in GitHub Desktop.
Hack to disable transactional fixtures on a per-test basis in rails 3.2
module TransactionalFixtureDisabler
def self.included(klass)
klass.teardown :restore_transactional_fixture_status
end
# call this at the top of your test if you need to disable
# transactional fixtures. this might be the case if:
# - you're testing after_commit or after_rollback callbacks
# on models
# - you're using more than one db connection some case
# (e.g. pushing some slow SELECTS to replicated db)
#
# the module takes care of all the cleanup and restores
# transactional fixtures to the default. note that if you
# want to speed things up a bit, you can specify the
# particular tables or activerecord models that should have
# their data deleted after your test has executed.
#
# NOTE: this will rollback any changes you make in a setup method
# (unless you call this method within your setup method)
# (it does an early rollback to remove the wrapper transaction)
#
# things_to_delete is an optional hash:
# :models_to_delete => array of classes to delete data for
# :tables_to_delete => array of table names to delete data for
# optional. default => hand-crafted list of a lot of tables
#
# usage examples:
#
# class MyTest < ActiveSupport::TestCase
# test "my class does something important to every table" do
# disable_transactional_fixtures
# # run tests that require fixtures off...
# end
#
# test "my test that touches only two tables, one activerecord, one not" do
# disable_transactional_fixtures :models_to_delete => [MyModel],
# :tables_to_delete => ['another_table_name']
# # run tests that require fixtures off
# end
# end
#-----------------------------------------------------------------------------
def disable_transactional_fixtures(things_to_delete = {})
if @_transactional_fixture_disabler_activated
raise "transactional fixtures already disabled for this test!"
end
@_transactional_fixture_disabler_activated = true
teardown_fixtures # activerecord gem: lib/active_record/fixtures.rb
@_original_transactional_fixture_status = self.class.use_transactional_fixtures
self.class.use_transactional_fixtures = false
@_things_to_delete = things_to_delete
end
def restore_transactional_fixture_status
if @_transactional_fixture_disabler_activated
if @_original_transactional_fixture_status
delete_everything @_things_to_delete
# when we've finally gotten rid of all fixtures, can probably get
# rid of this
setup_fixtures
end
self.class.use_transactional_fixtures = @_original_transactional_fixture_status
@_transactional_fixture_disabler_activated = false
end
end
end
@bmpercy
Copy link
Author

bmpercy commented Mar 1, 2012

NOTE: you'll have to provide your own delete_everything method...ours goes through the list of models and table names and truncates the corresponding tables, by default doing all of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment