// if database name is not provided, dbname from the backup is used. // if newStoragePath is not provided, system defaults are used internal static void RestoreSqlBackup( ICakeContext context, string connectionString, RestoreSqlBackupSettings settings, IList <FilePath> backupFiles, IList <FilePath>?differentialBackupFiles = null) { Initializer.InitializeNativeSearchPath(); using (var connection = SqlServerAliasesImpl.OpenSqlConnection(context, connectionString)) { var firstBackupFile = backupFiles.First(); var oldDbName = GetDatabaseName(firstBackupFile, connection); var databaseName = settings.NewDatabaseName ?? oldDbName; if (settings.SwitchToUserMode != DbUserMode.MultiUser) { using (var singleModeCommand = GetSetDatabaseUserModeCommand( context, connection, databaseName, settings.SwitchToUserMode)) { singleModeCommand.ExecuteNonQuery(); } } var hasDifferentialBackup = differentialBackupFiles?.Count > 0; using (var fullRestoreCommand = GetRestoreSqlBackupCommand( context, connection, settings.BackupSetFile, settings.WithReplace, hasDifferentialBackup, databaseName, settings.NewStorageFolder ?? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), backupFiles.ToArray())) { fullRestoreCommand.ExecuteNonQuery(); } if (hasDifferentialBackup) { using (var differentialRestoreCommand = GetRestoreSqlBackupCommand( context, connection, settings.DifferentialBackupSetFile, false, false, databaseName, settings.NewStorageFolder ?? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), differentialBackupFiles?.ToArray() ?? Array.Empty <FilePath>())) { differentialRestoreCommand.ExecuteNonQuery(); } } if (settings.SwitchToUserMode != DbUserMode.MultiUser) { using (var singleModeCommand = GetSetDatabaseUserModeCommand(context, connection, databaseName, DbUserMode.MultiUser)) { singleModeCommand.ExecuteNonQuery(); } } } }
internal static void DropAndCreateDatabase(ICakeContext context, string connectionString, string databaseName, CreateDatabaseSettings settings) { Initializer.InitializeNativeSearchPath(); DropDatabase(context, connectionString, databaseName); CreateDatabase(context, connectionString, databaseName, settings); }