Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created September 6, 2014 04:36
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save myronmarston/61380bb4500b4d85dd3f to your computer and use it in GitHub Desktop.
Save myronmarston/61380bb4500b4d85dd3f to your computer and use it in GitHub Desktop.
Example of how to disable foreign keys for a particular context
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
@joshuapaling
Copy link

To others using this: if you get uninitialized constant DB, try replacing DB.run with ActiveRecord::Base.connection.execute

@john-huang-121
Copy link

This helped! Thanks!

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