Howto run a rake task in sandbox mode
If you have a Rails rake task that somehow changes your DB data, but you want to be sure that the DB will be rolled back to its previous state after the rake task has completed, you can simply include this snippet right after your task definition:
1
2
3
4
5
6
ActiveRecord::Base.connection.increment_open_transactions
ActiveRecord::Base.connection.begin_db_transaction
at_exit do
ActiveRecord::Base.connection.rollback_db_transaction
ActiveRecord::Base.connection.decrement_open_transactions
end
If you wonder where is this code coming from, it’s directly from the rails console code.
This post is licensed under CC BY 4.0 by the author.