Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions public/Remove-DbaAgDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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) {
Expand Down
25 changes: 24 additions & 1 deletion tests/Remove-DbaAgDatabase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -55,14 +67,25 @@ 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

$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
Expand Down