Skip to content

Commit

Permalink
Merge pull request dataplat#5064 from sqlcollaborative/prematuretermi…
Browse files Browse the repository at this point in the history
…nation

fixes Premature termination of restores
  • Loading branch information
Stuart-Moore authored Feb 9, 2019
2 parents f63b50a + e50a8cd commit 1d9f8f9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
4 changes: 2 additions & 2 deletions functions/Invoke-DbaAdvancedRestore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ function Invoke-DbaAdvancedRestore {
}
}
} elseif (-not $WithReplace -and (-not $VerifyOnly)) {
Stop-Function -Message "$Database exists and WithReplace not specified, stopping" -EnableException $EnableException
return
Write-Message -Level verbose -Message "$Database exists and WithReplace not specified, stopping"
continue
}
}
Write-Message -Message "WithReplace = $WithReplace" -Level Debug
Expand Down
25 changes: 9 additions & 16 deletions functions/Restore-DbaDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ function Restore-DbaDatabase {
.PARAMETER Recover
If set will perform recovery on the indicated database
.PARAMETER AllowContinue
By default, Restore-DbaDatabase will stop restoring any databases if it comes across an error.
Use this switch to enable it to restore all databases without issues.
.PARAMETER GetBackupInformation
Passing a string value into this parameter will cause a global variable to be created holding the output of Get-DbaBackupInformation
Expand Down Expand Up @@ -181,6 +177,9 @@ function Restore-DbaDatabase {
.PARAMETER PageRestoreTailFolder
This parameter passes in a location for the tail log backup required for page level restore
.PARAMETER AllowContinue
This parameter has been deprecated and will be removed in v1.0
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Expand Down Expand Up @@ -295,11 +294,10 @@ function Restore-DbaDatabase {
.EXAMPLE
PS C:\> Get-DbaBackupHistory - SqlInstance server\instance1 -Database ProdFinance -Last | Restore-DbaDatabase -PageRestore
PS C:\> $SuspectPage -PageRestoreTailFolder c:\temp -TrustDbBackupHistory -AllowContinues
PS C:\> $SuspectPage -PageRestoreTailFolder c:\temp -TrustDbBackupHistory
Gets a list of Suspect Pages using Get-DbaSuspectPage. The uses Get-DbaBackupHistory and Restore-DbaDatabase to perform a restore of the suspect pages and bring them up to date
If server\instance1 is Enterprise edition this will be done online, if not it will be performed offline
AllowContinue is required to make sure we cope with existing files
.EXAMPLE
PS C:\> $BackupHistory = Get-DbaBackupInformation -SqlInstance sql2005 -Path \\backups\sql2000\ProdDb
Expand Down Expand Up @@ -362,7 +360,6 @@ function Restore-DbaDatabase {
[parameter(ParameterSetName = "Restore")][string]$DestinationFileSuffix,
[parameter(ParameterSetName = "Recovery")][switch]$Recover,
[parameter(ParameterSetName = "Restore")][switch]$KeepCDC,
[switch]$AllowContinue,
[string]$GetBackupInformation,
[switch]$StopAfterGetBackupInformation,
[string]$SelectBackupInformation,
Expand All @@ -373,7 +370,8 @@ function Restore-DbaDatabase {
[switch]$StopAfterTestBackupInformation,
[parameter(Mandatory, ParameterSetName = "RestorePage")][object]$PageRestore,
[parameter(Mandatory, ParameterSetName = "RestorePage")][string]$PageRestoreTailFolder,
[int]$StatementTimeout = 0
[int]$StatementTimeout = 0,
[switch]$AllowContinue
)
begin {
Write-Message -Level InternalComment -Message "Starting"
Expand All @@ -390,8 +388,8 @@ function Restore-DbaDatabase {
$UseDestinationDefaultDirectories = $true
$paramCount = 0

if (!(Test-Bound "AllowContinue") -and $true -ne $AllowContinue) {
$AllowContinue = $false
if (Test-Bound "AllowContinue") {
Write-Message -Level Warning -Message "AllowContinue is deprecated and will be removed in v1.0"
}
if (Test-Bound "FileMapping") {
$paramCount += 1
Expand Down Expand Up @@ -683,12 +681,7 @@ function Restore-DbaDatabase {
$DbUnVerified = ($FilteredBackupHistory | Where-Object {
$_.IsVerified -eq $False
} | Select-Object -Property Database -Unique).Database -join ','
if ($AllowContinue) {
Write-Message -Message "$DbUnverified failed testing, AllowContinue set" -Level Verbose
} else {
Stop-Function -Message "Database $DbUnverified failed testing, AllowContinue not set, exiting"
return
}
Write-Message -Level Warning -Message "Database $DbUnverified failed testing, skipping"
}
If ($PSCmdlet.ParameterSetName -eq "RestorePage") {
if (($FilteredBackupHistory.Database | select-Object -unique | Measure-Object).count -ne 1) {
Expand Down
2 changes: 1 addition & 1 deletion functions/Test-DbaBackupInformation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function Test-DbaBackupInformation {
Write-Message -Level Verbose -Message "VerifyOnly = $VerifyOnly"
If ($VerifyOnly -ne $true) {
if ($null -ne $DbCheck -and ($WithReplace -ne $true -and $Continue -ne $true)) {
Stop-Function -Message "Database $Database exists, so WithReplace must be specified" -Target $database
Write-Message -Level Warning -Message "Database $Database exists, so WithReplace must be specified" -Target $database
$VerificationErrors++
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Restore-DbaDatabase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'SqlInstance','SqlCredential','Path','DatabaseName','DestinationDataDirectory','DestinationLogDirectory','DestinationFileStreamDirectory','RestoreTime','NoRecovery','WithReplace','XpDirTree','OutputScriptOnly','VerifyOnly','MaintenanceSolutionBackup','FileMapping','IgnoreLogBackup','UseDestinationDefaultDirectories','ReuseSourceFolderStructure','DestinationFilePrefix','RestoredDatabaseNamePrefix','TrustDbBackupHistory','MaxTransferSize','BlockSize','BufferCount','DirectoryRecurse','EnableException','StandbyDirectory','Continue','AzureCredential','ReplaceDbNameInFile','DestinationFileSuffix','Recover','KeepCDC','AllowContinue','GetBackupInformation','StopAfterGetBackupInformation','SelectBackupInformation','StopAfterSelectBackupInformation','FormatBackupInformation','StopAfterFormatBackupInformation','TestBackupInformation','StopAfterTestBackupInformation','PageRestore','PageRestoreTailFolder','StatementTimeout'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Path', 'DatabaseName', 'DestinationDataDirectory', 'DestinationLogDirectory', 'DestinationFileStreamDirectory', 'RestoreTime', 'NoRecovery', 'WithReplace', 'XpDirTree', 'OutputScriptOnly', 'VerifyOnly', 'MaintenanceSolutionBackup', 'FileMapping', 'IgnoreLogBackup', 'UseDestinationDefaultDirectories', 'ReuseSourceFolderStructure', 'DestinationFilePrefix', 'RestoredDatabaseNamePrefix', 'TrustDbBackupHistory', 'MaxTransferSize', 'BlockSize', 'BufferCount', 'DirectoryRecurse', 'EnableException', 'StandbyDirectory', 'Continue', 'AzureCredential', 'ReplaceDbNameInFile', 'DestinationFileSuffix', 'Recover', 'KeepCDC', 'AllowContinue', 'GetBackupInformation', 'StopAfterGetBackupInformation', 'SelectBackupInformation', 'StopAfterSelectBackupInformation', 'FormatBackupInformation', 'StopAfterFormatBackupInformation', 'TestBackupInformation', 'StopAfterTestBackupInformation', 'PageRestore', 'PageRestoreTailFolder', 'StatementTimeout'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Expand Down Expand Up @@ -35,7 +35,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
Context "Ensuring warning is thrown if database already exists" {
$results = Restore-DbaDatabase -SqlInstance $script:instance2 -Path $script:appveyorlabrepo\singlerestore\singlerestore.bak -WarningVariable warning -WarningAction SilentlyContinue
It "Should warn" {
$warning | Where-Object { $_ -like '*Test-DbaBackupInformation*Database*' } | Should Match "exists and WithReplace not specified, stopping"
$warning | Where-Object { $_ -like '*Test-DbaBackupInformation*Database*' } | Should Match "exists, so WithReplace must be specified"
}
It "Should not return object" {
$results | Should Be $null
Expand Down

0 comments on commit 1d9f8f9

Please sign in to comment.