Cake.SqlServer

Cake build aliases for manipulating SQL Server databases and executing inline SQL and script files.

#addin nuget:?package=Cake.SqlServer&version=2.2.0

<package id="Cake.SqlServer" version="2.2.0" />

dotnet add package Cake.SqlServer --version 2.2.0

<PackageReference Include="Cake.SqlServer" Version="2.2.0" />

Aliases

Contains functionality to backup and restore SQL Server database

General

BackupDatabase(ICakeContext, string, string, BackupDatabaseSettings) Backup an existing database to a file.
CreateBacpacFile(ICakeContext, string, string, FilePath) Creates a bacpac file for easy database backuping.
CreateDatabase(ICakeContext, string, string) Creates an empty database. If database with this name already exists, throws a SqlException. Cake.SqlServer.SqlServerAliases.CreateDatabaseIfNotExists(Cake.Core.ICakeContext,System.String,System.String) if you would like to check if database already exists.
CreateDatabase(ICakeContext, string, string, CreateDatabaseSettings) Creates an empty database. If database with this name already exists, throws a SqlException. Allows to specify primary and log files location.
CreateDatabaseIfNotExists(ICakeContext, string, string) Creates an empty database if another database with the same does not already exist.
CreateDatabaseIfNotExists(ICakeContext, string, string, CreateDatabaseSettings) Creates an empty database if another database with the same does not already exist.
DatabaseExists(ICakeContext, string, string) Test if the database exists
DropAndCreateDatabase(ICakeContext, string, string) First drops, then recreates the database
DropAndCreateDatabase(ICakeContext, string, string, CreateDatabaseSettings) First drops, then recreates the database
DropDatabase(ICakeContext, string, string) Drops database. Before dropping the DB, database is set to be offline, then online again. This is to be sure that there are no live connections, otherwise the script will fail. Also if the database does not exist - it will not do anything.
ExecuteSqlCommand(ICakeContext, SqlConnection, string) Execute any SQL command.
ExecuteSqlCommand(ICakeContext, string, string) Execute any SQL command.
ExecuteSqlFile(ICakeContext, SqlConnection, FilePath) Reads SQL commands from a file and executes them.
ExecuteSqlFile(ICakeContext, string, FilePath) Reads SQL commands from a file and executes them.
ExtractDacpacFile(ICakeContext, string, string, ExtractDacpacSettings) Extracts a dacpac file to a database package.
LocalDbCreateInstance(ICakeContext, string) Creates a server instance and starts the server. The version number defaults to the version of the SqlLocalDB utility
LocalDbCreateInstance(ICakeContext, string, LocalDbVersion) Creates a server instance and starts the server.
LocalDbDeleteInstance(ICakeContext, string) Deletes the LocalDB instance
LocalDbStartInstance(ICakeContext, string) Starts the LocalDB instance. Instance must exist before you can start it.
LocalDbStopInstance(ICakeContext, string) Stops the LocalDB instance.
OpenSqlConnection(ICakeContext, string) Opens a new System.Data.SqlClient.SqlConnection with the given connection string.
PublishDacpacFile(ICakeContext, string, string, FilePath, PublishDacpacSettings) Publish a dacpac file to a database.
RestoreBacpac(ICakeContext, string, string, FilePath) Restores a bacpac file into a database.

NB: there must be no database with the name you provide. Otherwise exception will be thrown.

RestoreMultipleSqlBackup(ICakeContext, string, RestoreSqlBackupSettings, IList<FilePath>, IList<FilePath>) Restores a database from multiple backup files.
RestoreSqlBackup(ICakeContext, string, FilePath) Restores a database from a backup file.
RestoreSqlBackup(ICakeContext, string, FilePath, RestoreSqlBackupSettings) Restores a database from a backup file.
SetSqlCommandTimeout(ICakeContext, int) Sets the CommandTimeout property for all SqlCommands used internally