From 7bf3449898442c47e0147b30da304256ce12f415 Mon Sep 17 00:00:00 2001 From: Andreas Jordan Date: Sat, 12 Sep 2026 12:28:59 +0200 Subject: [PATCH] Restore-DbaDatabase - Say that nothing was restored when the selection is empty Without -EnableException, Select-DbaBackupInformation only warns when no restorable backup is left - no full backup anchors the chain, or nothing is newer than the continue point - and the command handed the empty selection on to the restore, which returned nothing and looked like success. A script that restores and then uses the database went on against a database that was never restored. The command now stops with a warning that names the database and says that nothing was restored, right after the selection. The warning before it still names what was missing. With -EnableException the selection already throws since #10631, so that path is unchanged, and the contract stays a warning: callers who point the command at possibly empty paths on purpose are not turned into failures. Fixes #10657 (do Restore-DbaDatabase) Co-Authored-By: Claude Fable 5.1 --- public/Restore-DbaDatabase.ps1 | 8 ++++++++ tests/Restore-DbaDatabase.Tests.ps1 | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/public/Restore-DbaDatabase.ps1 b/public/Restore-DbaDatabase.ps1 index 118873c22db..e7da7cdb38d 100644 --- a/public/Restore-DbaDatabase.ps1 +++ b/public/Restore-DbaDatabase.ps1 @@ -877,6 +877,14 @@ function Restore-DbaDatabase { if ($StopAfterSelectBackupInformation) { return } + if (-not $FilteredBackupHistory) { + # Without -EnableException, Select-DbaBackupInformation only warns when nothing restorable is left - no + # full backup anchors the chain, or nothing is newer than the continue point - and used to hand an empty + # selection on to the restore, which then returned nothing and looked like success (#10657). Say plainly + # that nothing was restored; the warning before this one names the reason. + Stop-Function -Message "Nothing to restore: the backup information selected no restorable backups for $($BackupHistory.Database | Sort-Object -Unique) on $RestoreInstance. A full backup has to anchor the chain (or -Continue has to point at a database restored with -NoRecovery), see the warning above for what was missing." + return + } try { Write-Message -Level Verbose -Message "VerifyOnly = $VerifyOnly" $parms = @{ diff --git a/tests/Restore-DbaDatabase.Tests.ps1 b/tests/Restore-DbaDatabase.Tests.ps1 index 2781d4fcb58..8cf71010285 100644 --- a/tests/Restore-DbaDatabase.Tests.ps1 +++ b/tests/Restore-DbaDatabase.Tests.ps1 @@ -225,8 +225,14 @@ Describe $CommandName -Tag IntegrationTests { WarningAction = "SilentlyContinue" } $results = Restore-DbaDatabase @splatRestore - $WarnVar | Should -BeLike "*Fullname property not found*" $results | Should -BeNullOrEmpty + # Two warnings: the selection names what is missing, and the command says that nothing was + # restored because of it (#10657). Before, the second one did not exist and the empty result + # looked like a successful restore. + $warnings = @($WarnVar) -join " " + $warnings | Should -BeLike "*Fullname property not found*" + $warnings | Should -BeLike "*Nothing to restore*" + $warnings | Should -BeLike "*$chainDbName*" } It "Throws when the path holds no backups at all with EnableException" { @@ -239,6 +245,19 @@ Describe $CommandName -Tag IntegrationTests { } { Restore-DbaDatabase @splatRestore } | Should -Throw "*No backups passed through*" } + + It "Warns that no backups passed through and returns nothing without EnableException" { + $splatRestore = @{ + SqlInstance = $TestConfig.InstanceSingle + Path = $emptyBackupDir + DatabaseName = $chainDbName + WithReplace = $true + WarningAction = "SilentlyContinue" + } + $results = Restore-DbaDatabase @splatRestore + $results | Should -BeNullOrEmpty + @($WarnVar) -join " " | Should -BeLike "*No backups passed through*" + } }