-
-
Save frankyston/680b92f805cc2e9b076dea2dbeca9c79 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 | |
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0;') | |
yield | |
ensure | |
ActiveRecord::Base.connection.execute('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 replacing DB.run with ActiveRecord::Base.connection.execute