Skip to content

Commit

Permalink
Add-DbaAgDatabase - Fix stale cache issue (#9101)
Browse files Browse the repository at this point in the history
Co-authored-by: Chrissy LeMaire <[email protected]>
  • Loading branch information
andreasjordan and potatoqualitee authored Oct 3, 2023
1 parent 5f774b9 commit 5d27f60
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration-tests-repl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,4 @@ jobs:
- name: Run replication tests part 3
run: |
Import-Module ./dbatools.psd1 -Force
Invoke-Pester ./tests/gh-actions-repl-3.ps1 -Output Detailed -PassThru -Verbose
# Invoke-Pester ./tests/gh-actions-repl-3.ps1 -Output Detailed -PassThru -Verbose
8 changes: 0 additions & 8 deletions public/Add-DbaAgDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,6 @@ function Add-DbaAgDatabase {

try {
$replicaAgDb = Get-DbaAgDatabase -SqlInstance $replicaServerSMO[$replicaName] -AvailabilityGroup $AvailabilityGroup -Database $db.Name -EnableException
if ($null -eq $replicaAgDb) {
# We know the database has to exist by now, so we refresh the AvailabilityGroup object to update the cache that claims it doesn't.
$replicaAgDb = Get-DbaAgDatabase -Refresh -SqlInstance $replicaServerSMO[$replicaName] -AvailabilityGroup $AvailabilityGroup -Database $db.Name -EnableException
if ($null -eq $replicaAgDb) {
# throws to a catch
throw [System.Management.Automation.ItemNotFoundException]::New()
}
}
} catch {
$failure = $true
Stop-Function -Message "Failed to get database $($db.Name) on replica $replicaName." -ErrorRecord $_ -Continue
Expand Down
8 changes: 0 additions & 8 deletions public/Get-DbaAgDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ function Get-DbaAgDatabase {
$InputObject += Get-DbaAvailabilityGroup -SqlInstance $SqlInstance -SqlCredential $SqlCredential -AvailabilityGroup $AvailabilityGroup
}

foreach ($ag in $InputObject) {
$null = $ag.Refresh()
# Calling AvilabilityReplica.Refresh() has the side effct of updating AvailabilityDatabases.
# calling AvailabilityDatabases.Refresh() alone is not enough
$null = $ag.AvailabilityReplicas.Refresh()
$null = $ag.AvailabilityDatabases.Refresh()
}

foreach ($db in $InputObject.AvailabilityDatabases) {
if ($Database) {
if ($db.Name -notin $Database) { continue }
Expand Down
3 changes: 3 additions & 0 deletions public/Get-DbaAvailabilityGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ function Get-DbaAvailabilityGroup {
}

foreach ($ag in $ags) {
# Refresh list of databases to fix #9094
$ag.AvailabilityDatabases.Refresh()

Add-Member -Force -InputObject $ag -MemberType NoteProperty -Name ComputerName -value $server.ComputerName
Add-Member -Force -InputObject $ag -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
Add-Member -Force -InputObject $ag -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
Expand Down

0 comments on commit 5d27f60

Please sign in to comment.