Skip to content

Commit

Permalink
SqlAGDatabase: Fixes unit tests that failed intermittently (#1534)
Browse files Browse the repository at this point in the history
- SqlAGDatabase
  - Fixed unit tests that failed intermittently when running unit tests
    in PowerShell 7 (issue #1532).
  - Minor code style issue changes.
  • Loading branch information
johlju authored May 8, 2020
1 parent aa5cb80 commit 8f11e10
Show file tree
Hide file tree
Showing 3 changed files with 646 additions and 610 deletions.
25 changes: 14 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated to support DscResource.Common v0.7.1.
- Changed to point to CONTRIBUTING.md on master branch to avoid "404 Page not found"
([issue #1508](https://github.com/dsccommunity/SqlServerDsc/issues/1508)).
- SqlAlias
- BREAKING CHANGE: The parameter `ServerName` is now non-mandatory to
prevent ping-pong behavior ([issue #1502](https://github.com/dsccommunity/SqlServerDsc/issues/1502)).
The `ServerName` is not returned as an empty string when the protocol is
Named Pipes.
- SqlRs
- Fix typo in the schema parameter `SuppressRestart` description
and in the parameter description in the `README.md`.
- SqlSetup
- Update integration tests to correctly detect sysadmins because of changes
to the build worker.
- SqlAGDatabase
- Fixed unit tests that failed intermittently when running unit tests
in PowerShell 7 ([issue #1532](https://github.com/dsccommunity/SqlServerDsc/issues/1532)).
- Minor code style issue changes.
- SqlAgentAlert
- The parameter `ServerName` now throws when passing an empty string or
null value (part of [issue #319](https://github.com/dsccommunity/SqlServerDsc/issues/319)).
Expand All @@ -145,6 +138,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- SqlAgentOperator
- The parameter `ServerName` now throws when passing an empty string or
null value (part of [issue #319](https://github.com/dsccommunity/SqlServerDsc/issues/319)).
- SqlAlias
- BREAKING CHANGE: The parameter `ServerName` is now non-mandatory to
prevent ping-pong behavior ([issue #1502](https://github.com/dsccommunity/SqlServerDsc/issues/1502)).
The `ServerName` is not returned as an empty string when the protocol is
Named Pipes.
- SqlRs
- Fix typo in the schema parameter `SuppressRestart` description
and in the parameter description in the `README.md`.
- SqlServerDatabaseMail
- The parameter `ServerName` now throws when passing an empty string or
null value (part of [issue #319](https://github.com/dsccommunity/SqlServerDsc/issues/319)).
Expand All @@ -162,6 +163,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
message that have the actual error that occurred.
- Corrected the examples.
- SqlSetup
- Update integration tests to correctly detect sysadmins because of changes
to the build worker.
- The property `SqlTempdbLogFileGrowth` and `SqlTempdbFileGrowth` now returns
the correct values. Previously the value of the growth was wrongly
divided by 1KB even if the value was in percent. Now the value for growth
Expand Down
14 changes: 8 additions & 6 deletions source/DSCResources/DSC_SqlAGDatabase/DSC_SqlAGDatabase.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ function Set-TargetResource
ServerObject = $primaryServerObject
AvailabilityGroup = $availabilityGroup
}

$databasesToAddToAvailabilityGroup = Get-DatabasesToAddToAvailabilityGroup @getDatabasesToAddToAvailabilityGroupParameters

$getDatabasesToRemoveFromAvailabilityGroupParameters = @{
Expand All @@ -227,6 +228,7 @@ function Set-TargetResource
ServerObject = $primaryServerObject
AvailabilityGroup = $availabilityGroup
}

$databasesToRemoveFromAvailabilityGroup = Get-DatabasesToRemoveFromAvailabilityGroup @getDatabasesToRemoveFromAvailabilityGroupParameters

# Create a hash table to store the databases that failed to be added to the Availability Group
Expand Down Expand Up @@ -573,21 +575,21 @@ function Set-TargetResource
}
}

if ( $databasesToRemoveFromAvailabilityGroup.Count -gt 0 )
if ($databasesToRemoveFromAvailabilityGroup.Count -gt 0)
{
Write-Verbose -Message ($script:localizedData.RemovingDatabasesToAvailabilityGroup -f $AvailabilityGroupName, ( $databasesToRemoveFromAvailabilityGroup -join ', ' ))

foreach ( $databaseToAddToAvailabilityGroup in $databasesToRemoveFromAvailabilityGroup )
foreach ($databaseNameToRemove in $databasesToRemoveFromAvailabilityGroup)
{
$availabilityDatabase = $primaryServerObject.AvailabilityGroups[$AvailabilityGroupName].AvailabilityDatabases[$databaseToAddToAvailabilityGroup]
$availabilityDatabase = $primaryServerObject.AvailabilityGroups[$AvailabilityGroupName].AvailabilityDatabases[$databaseNameToRemove]

try
{
Remove-SqlAvailabilityDatabase -InputObject $availabilityDatabase -ErrorAction Stop
Remove-SqlAvailabilityDatabase -InputObject $availabilityDatabase -ErrorAction 'Stop'
}
catch
{
$databasesToRemoveFailures.Add($databaseToAddToAvailabilityGroup, 'Failed to remove the database from the availability group.')
$databasesToRemoveFailures.Add($databaseNameToRemove, 'Failed to remove the database from the availability group.')
}
}
}
Expand All @@ -598,7 +600,7 @@ function Set-TargetResource
$formatArgs = @()
foreach ( $failure in ( $databasesToAddFailures.GetEnumerator() + $databasesToRemoveFailures.GetEnumerator() ) )
{
$formatArgs += "The operation on the database '$( $failure.Key )' failed with the following errors: $( $failure.Value -join "`r`n" )"
$formatArgs += "The operation on the database '$($failure.Key)' failed with the following errors: $($failure.Value -join "`r`n")"
}

throw ($script:localizedData.AlterAvailabilityGroupDatabaseMembershipFailure -f $formatArgs )
Expand Down
Loading

0 comments on commit 8f11e10

Please sign in to comment.