Bulletproof way to DROP and CREATE a database under Continuous Integration.

Posted by H. Abraham Chavez on Stack Overflow See other posts from Stack Overflow or by H. Abraham Chavez
Published on 2010-03-17T15:45:09Z Indexed on 2010/03/17 15:51 UTC
Read the original article Hit count: 276

I am attempting to drop and recreate a database from my CI setup. But I'm finding it difficult to automate the dropping and creation of the database, which is to be expected given the complexities of the db being in use. Sometimes the process hangs, errors out with "db is currently in use" or just takes too long. I don't care if the db is in use, I want to kill it and create it again. Does some one have a straight shot method to do this? alternatively does anyone have experience dropping all objects in the db instead of dropping the db itself?

USE master

--Create a database
IF EXISTS(SELECT name FROM sys.databases
    WHERE name = 'mydb')
BEGIN
 ALTER DATABASE mydb
 SET SINGLE_USER --or RESTRICTED_USER
 --WITH ROLLBACK IMMEDIATE
    DROP DATABASE uAbraham_MapSifterAuthority
END

CREATE DATABASE mydb;

© Stack Overflow or respective owner

Related posts about continuous-integration

Related posts about sql