Skip to content

Commit bfb48b2

Browse files
Restore-DbaDatabase, Invoke-DbaAdvancedRestore - Add ErrorBrokerConversations parameter
Adds support for the ERROR_BROKER_CONVERSATIONS option when restoring databases. This option ends all Service Broker conversations with an error during restore. Closes #6581 (do *Restore*) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
1 parent 63c906f commit bfb48b2

4 files changed

Lines changed: 43 additions & 25 deletions

File tree

public/Invoke-DbaAdvancedRestore.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ function Invoke-DbaAdvancedRestore {
9595
Essential when restoring databases where CDC is actively capturing data changes for auditing or ETL processes.
9696
Cannot be combined with NoRecovery or StandbyDirectory parameters as CDC requires the database to be fully recovered.
9797
98+
.PARAMETER ErrorBrokerConversations
99+
Ends all conversations in the database with an error message when restoring, using the ERROR_BROKER_CONVERSATIONS option.
100+
Use this when you want to clean up Service Broker conversations that may be left in an inconsistent state after a restore.
101+
Only applied during the final restore step (WITH RECOVERY), not during intermediate log restores.
102+
98103
.PARAMETER PageRestore
99104
Array of page objects from Get-DbaSuspectPage specifying corrupted pages to restore using page-level restore.
100105
Use this for targeted repair of specific corrupted pages without restoring the entire database.
@@ -234,6 +239,7 @@ function Invoke-DbaAdvancedRestore {
234239
[switch]$WithReplace,
235240
[switch]$KeepReplication,
236241
[switch]$KeepCDC,
242+
[switch]$ErrorBrokerConversations,
237243
[object[]]$PageRestore,
238244
[string]$ExecuteAs,
239245
[switch]$StopBefore,
@@ -406,12 +412,15 @@ function Invoke-DbaAdvancedRestore {
406412
if ($Pscmdlet.ShouldProcess($SqlInstance, "Restoring $database to $SqlInstance based on these files: $($backup.FullName -join ', ')")) {
407413
try {
408414
$restoreComplete = $true
409-
if ($KeepCDC -and $restore.NoRecovery -eq $false) {
415+
if (($KeepCDC -or $ErrorBrokerConversations) -and $restore.NoRecovery -eq $false) {
410416
$script = $restore.Script($server)
417+
$withOptions = @()
418+
if ($KeepCDC) { $withOptions += 'KEEP_CDC' }
419+
if ($ErrorBrokerConversations) { $withOptions += 'ERROR_BROKER_CONVERSATIONS' }
411420
if ($script -like '*WITH*') {
412-
$script = $script.TrimEnd() + ' , KEEP_CDC'
421+
$script = $script.TrimEnd() + ' , ' + ($withOptions -join ' , ')
413422
} else {
414-
$script = $script.TrimEnd() + ' WITH KEEP_CDC'
423+
$script = $script.TrimEnd() + ' WITH ' + ($withOptions -join ' , ')
415424
}
416425
if ($true -ne $OutputScriptOnly) {
417426
Write-Progress -id 1 -activity "Restoring $database to $SqlInstance - Backup $BackupCnt of $($Backups.count)" -percentcomplete 0 -status ([System.String]::Format("Progress: {0} %", 0))

public/Restore-DbaDatabase.ps1

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ function Restore-DbaDatabase {
216216
Use this when restoring databases with CDC enabled and you need to maintain change tracking functionality.
217217
Cannot be combined with NoRecovery or Standby modes as CDC requires the database to be fully recovered.
218218
219+
.PARAMETER ErrorBrokerConversations
220+
Ends all conversations in the database with an error message when restoring, using the ERROR_BROKER_CONVERSATIONS option.
221+
Use this when you want to clean up Service Broker conversations that may be left in an inconsistent state after a restore.
222+
Only applied during the final restore step (WITH RECOVERY), not during intermediate log restores.
223+
219224
.PARAMETER KeepReplication
220225
Maintains replication settings and objects when restoring databases involved in replication topologies.
221226
Use this when restoring publisher or subscriber databases where you need to preserve replication configuration.
@@ -485,6 +490,7 @@ function Restore-DbaDatabase {
485490
[parameter(ParameterSetName = "Restore")][string]$DestinationFileSuffix,
486491
[parameter(ParameterSetName = "Recovery")][switch]$Recover,
487492
[parameter(ParameterSetName = "Restore")][switch]$KeepCDC,
493+
[parameter(ParameterSetName = "Restore")][switch]$ErrorBrokerConversations,
488494
[string]$GetBackupInformation,
489495
[switch]$StopAfterGetBackupInformation,
490496
[string]$SelectBackupInformation,
@@ -873,28 +879,29 @@ function Restore-DbaDatabase {
873879
}
874880
try {
875881
$parms = @{
876-
SqlInstance = $RestoreInstance
877-
WithReplace = $WithReplace
878-
RestoreTime = $RestoreTime
879-
StandbyDirectory = $StandbyDirectory
880-
NoRecovery = $NoRecovery
881-
Continue = $Continue
882-
OutputScriptOnly = $OutputScriptOnly
883-
BlockSize = $BlockSize
884-
MaxTransferSize = $MaxTransferSize
885-
BufferCount = $Buffercount
886-
KeepCDC = $KeepCDC
887-
VerifyOnly = $VerifyOnly
888-
PageRestore = $PageRestore
889-
StorageCredential = $StorageCredential
890-
KeepReplication = $KeepReplication
891-
StopMark = $StopMark
892-
StopAfterDate = $StopAfterDate
893-
StopBefore = $StopBefore
894-
ExecuteAs = $ExecuteAs
895-
Checksum = $Checksum
896-
Restart = $Restart
897-
EnableException = $true
882+
SqlInstance = $RestoreInstance
883+
WithReplace = $WithReplace
884+
RestoreTime = $RestoreTime
885+
StandbyDirectory = $StandbyDirectory
886+
NoRecovery = $NoRecovery
887+
Continue = $Continue
888+
OutputScriptOnly = $OutputScriptOnly
889+
BlockSize = $BlockSize
890+
MaxTransferSize = $MaxTransferSize
891+
BufferCount = $Buffercount
892+
KeepCDC = $KeepCDC
893+
ErrorBrokerConversations = $ErrorBrokerConversations
894+
VerifyOnly = $VerifyOnly
895+
PageRestore = $PageRestore
896+
StorageCredential = $StorageCredential
897+
KeepReplication = $KeepReplication
898+
StopMark = $StopMark
899+
StopAfterDate = $StopAfterDate
900+
StopBefore = $StopBefore
901+
ExecuteAs = $ExecuteAs
902+
Checksum = $Checksum
903+
Restart = $Restart
904+
EnableException = $true
898905
}
899906
$FilteredBackupHistory | Where-Object { $_.IsVerified -eq $true } | Invoke-DbaAdvancedRestore @parms
900907
} catch {

tests/Invoke-DbaAdvancedRestore.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Describe $CommandName -Tag UnitTests {
2727
"WithReplace",
2828
"KeepReplication",
2929
"KeepCDC",
30+
"ErrorBrokerConversations",
3031
"PageRestore",
3132
"ExecuteAs",
3233
"StopBefore",

tests/Restore-DbaDatabase.Tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Describe $CommandName -Tag UnitTests {
4545
"DestinationFileSuffix",
4646
"Recover",
4747
"KeepCDC",
48+
"ErrorBrokerConversations",
4849
"GetBackupInformation",
4950
"StopAfterGetBackupInformation",
5051
"SelectBackupInformation",

0 commit comments

Comments
 (0)