-
-
Notifications
You must be signed in to change notification settings - Fork 809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix stale cache issue in Add-DbaAgDatabase #9094
Fix stale cache issue in Add-DbaAgDatabase #9094
Conversation
…Instance objects Signed-off-by: Klaus Frank <[email protected]>
Fantastic PR! Thank you so much. @andreasjordan do you see any potential issues with the changes? |
I would like to find a way to fix the issue without adding a new parameter. Can you give me some days to investigate before merging? |
98e25b2
to
0590d5a
Compare
I have setup my lab and can reproduce the issue - thanks to the detailed description. Please give me some more days to have a deeper look. |
Thank you both 🙇🏼 I'll also take a closer look if I've got time in the upcoming days. |
I have a theory: I think this is "normal" SMO behavior. With the first database, the SMO property $server.AvailabilityGroups['AG'].AvailabilityDatabases is read the first time, so the SMO fetches the information from the server. But it also marks this property as "feched" and will not update its value without a refresh. And as we use the same SMO object with the second database, we still use the old information - where the new database is missing. I will test a bit and see if I can find the best place for the refresh. |
Ok, I have added one line to fix the problem. Please add |
LGTM! I like this, thank you! |
btw @andreasjordan, i removed the param |
@potatoqualitee sorry i'm only on mobile and can't figure out what you have merged, but i think the change is to big. Only one lines needed to be added. The pr should have been canceled and only the one line should have been added. Please don't release and wait a day so I can check that. |
This needs to be undone!
|
I just added #9101 to fix the issue. |
oh dear, sorry! I didnt realize the refresh and depended on the tests passing 😱 |
When multiple databases are joined to an AG using Add-DbaAgDatabase using the same SqlInstance objects Get-DbaAgDatabase returns $null for all except the first database.
It looks like this issue does NOT happen when the dbatools internal backup function is used beforehand. But if a stored procedure is invoked to create the backup it does.
In order to fix this I added a switch to Get-DbaAgDatabase to update the Availability group object before use.
Note: I was unable to run
.\tests\manual.pester.ps1
within the GitHub codespace, as even after manually installing PSScriptAnalyzer it still failed to find and use it.EDIT: Only partially fixes the problem, it still sometimes returns null. Interestingly even if the Get-DbaAgReplica call still returns $null, changing it to
Get-DbaAvailabilityGroup -SqlInstance $replicaServerSMO[$replicaName] -SqlCredential $SqlCredential -AvailabilityGroup $AvailabilityGroup | Get-DbaAgDatabase -AvailabilityGroup $Avail abilityGroup -Database $db.Name -EnableException
appears to almost always work even in these cases?!Edit2: Fixed now too, the issue is that
AvailabilityReplicas.Refresh()
needs to be called for$InputObject.AvailabilityDatabases
to be updated. (No, that's not a typo, it's really refreshing Replicas for Databases to be updated 🙄)Please read -- recent changes to our repo
On November 10, 2022, we removed some bloat from our repository (for the second and final time). This change requires that all contributors reclone or refork their repo.
PRs from repos that have not been recently reforked or recloned will be closed and @potatoqualitee will cherry-pick your commits and open a new PR with your changes.
Type of Change
.\tests\manual.pester.ps1
)Purpose
Fix caching issue within Add-DbaAgdatabase
Approach
Triggers a refresh of the AvailabilityGroup object. But only does so if it is required.
Commands to test
The foreach around Add-DbaAgDatabase is what causes the bug (but not for the very first database)
Screenshots