Bulletproof DROP method and CREATE a database with continuous integration

I am trying to reset and recreate a database from my CI installation. But I find it difficult to automate the deletion and creation of the database, which should be expected, given the complexity of the db used. Sometimes the process freezes, errors with "db are currently being used" or just too long. I don't care if db is used, I want to kill it and create it again. Does anyone have a direct fire method? alternatively, does anyone have the ability to dump all objects in db rather than reset 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;
+3
source share
2 answers

Hudson QA . , , // .

, , .

USE MASTER
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_KillDatabaseProcesses]') AND type in (N'P', N'PC'))
   DROP PROCEDURE [dbo].[sp_KillDatabaseProcesses]
GO
CREATE PROCEDURE dbo.sp_KillDatabaseProcesses(@databaseName varchar(100))     
   AS
   DECLARE @databaseId int,
           @sysProcessId int,
           @cmd varchar(1000)
   EXEC ('USE MASTER')
   SELECT @databaseId = dbid FROM master..sysdatabases
      WHERE [name] = @databaseName
   DECLARE sysProcessIdCursor CURSOR FOR
      SELECT spid FROM [master]..[sysprocesses] WHERE [dbid] = @databaseId

   OPEN sysProcessIdCursor
   FETCH NEXT FROM sysProcessIdCursor INTO @sysProcessId WHILE @@fetch_status = 0
   BEGIN
      SET @cmd = 'KILL '+ convert(nvarchar(30),@sysProcessId)
      PRINT @cmd
      EXEC(@cmd)
      FETCH NEXT FROM sysProcessIdCursor INTO @sysProcessId
   END
   DEALLOCATE sysProcessIdCursor
GO
+2

- .

, CI script. CI , script/ , . script, . CI / , , , . , , .

0

Source: https://habr.com/ru/post/1737362/


All Articles