Fix Database.Owner not reflecting SetOwner() until Refresh() - #231
Open
Andreas Jordan (andreasjordan) wants to merge 1 commit into
Open
Fix Database.Owner not reflecting SetOwner() until Refresh()#231Andreas Jordan (andreasjordan) wants to merge 1 commit into
Andreas Jordan (andreasjordan) wants to merge 1 commit into
Conversation
SetOwnerImpl only executed the ALTER AUTHORIZATION statement without touching the property bag, so Database.Owner kept returning the previous owner until Refresh() was called (issue microsoft#40). Instead of storing the caller-provided login name (which may differ from the canonical name the server stores, e.g. by casing on a case-insensitive server), mark the Owner property as not retrieved and the property bag as lazy - the same pattern CleanObject uses for the default language properties - so the next read of Owner fetches the canonical value from the server. The existing Database_CanChangeOwner tests worked around the bug by calling Refresh() before asserting; they now also assert the value before Refresh(). Fixes microsoft#40 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Aug 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #40.
SetOwnerImplonly executed the ALTER AUTHORIZATION batch without touching the property bag, soDatabase.Ownerkept returning the previous owner untilRefresh()was called.Rather than writing the caller-provided login name into the property (it may differ from what the server stores, e.g. by casing on a case-insensitive server), the fix invalidates the cached
Ownerproperty and marks the property bag lazy - the same patternCleanObjectuses for the default language properties - so the next read ofOwnerfetches the canonical value from the server. The invalidation is skipped in capture mode since nothing was executed.The existing
Database_CanChangeOwnertests worked around the bug by callingRefresh()before asserting; they now also assert the value beforeRefresh().Verified against SQL Server 2019: after
SetOwner("sa"),OwnerreturnssawithoutRefresh(); afterSetOwnerwith a differently-cased Windows login,Ownerreturns the server's canonical casing.This fix was created by Claude and reviewed by Andreas Jordan.