Cake.SqlServer

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

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

dotnet add package Cake.SqlServer --version 3.0.0

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

Aliases

Contains functionality to deal with SQL Server: DropDatabase, CreateDatabase, execute SQL, execute SQL from files, etc. Provides functionality to manage LocalDb instances: Create, Start, Stop, Delete instances;

In order to use the commands for this addin, include the following in your build.cake file to download and reference from NuGet.org:

#addin "nuget:?package=Cake.SqlServer"

General

BackupDatabase(string, string, BackupDatabaseSettings) Backup an existing database to a file.
CreateBacpacFile(string, string, FilePath) Creates a bacpac file for easy database backuping.
CreateDatabase(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.
CreateDatabase(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.
CreateDatabaseIfNotExists(string, string, CreateDatabaseSettings) Creates an empty database if another database with the same does not already exist.
CreateDatabaseIfNotExists(string, string) Creates an empty database if another database with the same does not already exist.
DatabaseExists(string, string) Test if the database exists
DropAndCreateDatabase(string, string, CreateDatabaseSettings) First drops, then recreates the database
DropAndCreateDatabase(string, string) First drops, then recreates the database
DropDatabase(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(SqlConnection, string) Execute any SQL command.
ExecuteSqlCommand(string, string) Execute any SQL command.
ExecuteSqlFile(SqlConnection, FilePath) Reads SQL commands from a file and executes them.
ExecuteSqlFile(string, FilePath) Reads SQL commands from a file and executes them.
ExtractDacpacFile(string, string, ExtractDacpacSettings) Extracts a dacpac file to a database package.
LocalDbCreateInstance(string, LocalDbVersion) Creates a server instance and starts the server.
LocalDbCreateInstance(string) Creates a server instance and starts the server. The version number defaults to the version of the SqlLocalDB utility
LocalDbDeleteInstance(string) Deletes the LocalDB instance
LocalDbStartInstance(string) Starts the LocalDB instance. Instance must exist before you can start it.
LocalDbStopInstance(string) Stops the LocalDB instance.
OpenSqlConnection(string) Opens a new Microsoft.Data.SqlClient.SqlConnection with the given connection string.
PublishDacpacFile(string, string, FilePath, PublishDacpacSettings) Publish a dacpac file to a database.
RestoreBacpac(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(string, RestoreSqlBackupSettings, IList<FilePath>, IList<FilePath>) Restores a database from multiple backup files.
RestoreSqlBackup(string, FilePath, RestoreSqlBackupSettings) Restores a database from a backup file.
RestoreSqlBackup(string, FilePath) Restores a database from a backup file.
SetSqlCommandTimeout(int) Sets the CommandTimeout property for all SqlCommands used internally