Skip to content

Instantly share code, notes, and snippets.

@jnettome
Created August 23, 2013 19:40
Show Gist options
  • Select an option

  • Save jnettome/6323172 to your computer and use it in GitHub Desktop.

Select an option

Save jnettome/6323172 to your computer and use it in GitHub Desktop.
DatabaseCleaner SQL Server
ActiveRecord::Base.connection.execute "EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasForeignRef'') = 1
DELETE FROM ?
else
TRUNCATE TABLE ?
'
EXEC sp_MSForEachTable 'DROP TABLE ?'
-- Now enable referential integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment