Skip to content

Commit

Permalink
cannot alter db in restoring state
Browse files Browse the repository at this point in the history
  • Loading branch information
rducom committed Apr 29, 2021
1 parent a0b1275 commit f667c24
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/SqlBackupTools/Restore/Native/NativeRestoreMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ private async Task<Exception> InternalFullDiffLogAsync(RestoreItem item, bool st
forceRestoreFull = true;
try
{
await sqlConnection.ExecuteAsync($"ALTER DATABASE [{item.Name}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", commandTimeout: _state.RestoreCommand.Timeout);
if (_state.ActualDbs.TryGetValue(item.Name, out var databaseInfo) && databaseInfo.State != DatabaseState.RESTORING)
{
await sqlConnection.ExecuteAsync($"ALTER DATABASE [{item.Name}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", commandTimeout: _state.RestoreCommand.Timeout);
}
await sqlConnection.ExecuteAsync($"DROP DATABASE [{item.Name}]", commandTimeout: _state.RestoreCommand.Timeout);
await sqlConnection.ExecuteAsync($"EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'{item.Name}'", commandTimeout: _state.RestoreCommand.Timeout);
}
Expand Down

0 comments on commit f667c24

Please sign in to comment.