Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save frankyston/680b92f805cc2e9b076dea2dbeca9c79 to your computer and use it in GitHub Desktop.
Save frankyston/680b92f805cc2e9b076dea2dbeca9c79 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
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
@frankyston
Copy link
Author

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

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