Skip to content

CustomCreateDatabase

Harry McIntyre edited this page Oct 2, 2015 · 9 revisions

You can instruct RH to use a custom script to create a database instead of the default scripts that it uses. You want to use this when you have a special way of creating databases that may vary from the norm, like adding multiple disk locations, etc.

Return 1 IF Script Creates Database

When using CustomCreateDatabase, if your script creates a database, it should return a value of 1. Otherwise any scripts you have in the runaftercreate folder will not be run.

rh.exe [options] -cds CreateDatabase.sql

In CreateDatabase.sql


//this example restores from a common backup, if the target database name doesn't exist
//The tokenised DatabaseName parameter is passed from Roundhouse

USE master
DECLARE @Created bit
SET @Created = 0
        
IF NOT EXISTS(SELECT * FROM sys.databases WHERE [name] = '{{DatabaseName}}')
BEGIN
        Set @Created = 1
                         
                        
        CREATE DATABASE {{DatabaseName}}
        ALTER DATABASE {{DatabaseName}} SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
        
        RESTORE DATABASE {{DatabaseName}}
        FROM DISK = N'C:\Backups\SomeDatabase.bak'
        WITH MOVE 'SomeDatabase' TO 'D:\SQLDATA\{{DatabaseName}}.mdf'
        , MOVE 'SomeDatabase_log' TO 'D:\SQLLOGS\{{DatabaseName}}_log.ldf'
        , REPLACE, NOUNLOAD,  STATS = 10;

        ALTER DATABASE {{DatabaseName}} SET MULTI_USER;

END 

SELECT @Created

Or see see https://github.com/chucknorris/roundhouse/blob/master/product/roundhouse.databases.sqlserver/SqlServerDatabase.cs#L108-L115

You can’t perform that action at this time.