Created
September 6, 2014 04:36
-
-
Save myronmarston/61380bb4500b4d85dd3f to your computer and use it in GitHub Desktop.
Example of how to disable foreign keys for a particular context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.shared_context "without foreign key constraints", :disable_foreign_keys do | |
def without_foreign_key_checks | |
DB.run('SET foreign_key_checks = 0;') | |
yield | |
ensure | |
DB.run('SET foreign_key_checks = 1;') | |
end | |
around(:example) { |ex| without_foreign_key_checks(&ex) } | |
end | |
# usage: | |
# RSpec.describe "Something", :disable_foreign_keys do | |
# # ... | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To others using this: if you get
uninitialized constant DB
, try replacingDB.run
withActiveRecord::Base.connection.execute