diff --git a/public/Remove-DbaAgDatabase.ps1 b/public/Remove-DbaAgDatabase.ps1 index 6e336a902ea..079ac8c0039 100644 --- a/public/Remove-DbaAgDatabase.ps1 +++ b/public/Remove-DbaAgDatabase.ps1 @@ -98,7 +98,7 @@ function Remove-DbaAgDatabase { if ((Test-Bound -ParameterName SqlInstance)) { if ((Test-Bound -Not -ParameterName Database)) { - Stop-Function -Message "You must specify one or more databases and one or more Availability Groups when using the SqlInstance parameter." + Stop-Function -Message "You must specify one or more databases when using the SqlInstance parameter." return } } @@ -110,7 +110,13 @@ function Remove-DbaAgDatabase { } if ($SqlInstance) { - $InputObject += Get-DbaAgDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $Database + $splatGetAgDatabase = @{ + SqlInstance = $SqlInstance + SqlCredential = $SqlCredential + Database = $Database + AvailabilityGroup = $AvailabilityGroup + } + $InputObject += Get-DbaAgDatabase @splatGetAgDatabase } foreach ($db in $InputObject) { diff --git a/tests/Remove-DbaAgDatabase.Tests.ps1 b/tests/Remove-DbaAgDatabase.Tests.ps1 index 3589f131311..74aeb8b2eb6 100644 --- a/tests/Remove-DbaAgDatabase.Tests.ps1 +++ b/tests/Remove-DbaAgDatabase.Tests.ps1 @@ -47,6 +47,18 @@ Describe $CommandName -Tag IntegrationTests { } $ag = New-DbaAvailabilityGroup @splatAvailabilityGroup + # A second availability group without any databases, to prove that a removal scoped + # to this group does not touch databases in other availability groups. + $agname2 = "dbatoolsci_removeagdb_agroup2" + $splatAvailabilityGroup2 = @{ + Primary = $TestConfig.InstanceHadr + Name = $agname2 + ClusterType = "None" + FailoverMode = "Manual" + Certificate = "dbatoolsci_AGCert" + } + $null = New-DbaAvailabilityGroup @splatAvailabilityGroup2 + # We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings. $PSDefaultParameterValues.Remove("*-Dba*:EnableException") } @@ -55,7 +67,7 @@ Describe $CommandName -Tag IntegrationTests { # We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails. $PSDefaultParameterValues["*-Dba*:EnableException"] = $true - $null = Remove-DbaAvailabilityGroup -SqlInstance $server -AvailabilityGroup $agname + $null = Remove-DbaAvailabilityGroup -SqlInstance $server -AvailabilityGroup $agname, $agname2 $null = Get-DbaEndpoint -SqlInstance $TestConfig.InstanceHadr -Type DatabaseMirroring | Remove-DbaEndpoint $null = Remove-DbaDatabase -SqlInstance $server -Database $dbname Remove-Item -Path "$($TestConfig.Temp)\$dbname.bak", "$($TestConfig.Temp)\$dbname.trn" -ErrorAction SilentlyContinue @@ -63,6 +75,17 @@ Describe $CommandName -Tag IntegrationTests { $PSDefaultParameterValues.Remove("*-Dba*:EnableException") } + Context "honors the AvailabilityGroup filter" { + It "does not remove the database when the removal is scoped to another availability group" { + $results = Remove-DbaAgDatabase -SqlInstance $TestConfig.InstanceHadr -Database $dbname -AvailabilityGroup $agname2 + $results | Should -BeNullOrEmpty + $WarnVar | Should -BeNullOrEmpty + + $agDatabase = Get-DbaAgDatabase -SqlInstance $TestConfig.InstanceHadr -Database $dbname + $agDatabase.AvailabilityGroup | Should -Be $agname + } + } + Context "removes ag db" { It "returns removed results" { $results = Remove-DbaAgDatabase -SqlInstance $TestConfig.InstanceHadr -Database $dbname