Skip to content

Fix Database.Owner not reflecting SetOwner() until Refresh() - #231

Open
Andreas Jordan (andreasjordan) wants to merge 1 commit into
microsoft:mainfrom
andreasjordan:fix-database-setowner-stale-owner
Open

Fix Database.Owner not reflecting SetOwner() until Refresh()#231
Andreas Jordan (andreasjordan) wants to merge 1 commit into
microsoft:mainfrom
andreasjordan:fix-database-setowner-stale-owner

Conversation

@andreasjordan

Copy link
Copy Markdown

Fixes #40.

SetOwnerImpl only executed the ALTER AUTHORIZATION batch without touching the property bag, so Database.Owner kept returning the previous owner until Refresh() 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 Owner property and marks the property bag 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 invalidation is skipped in capture mode since nothing was executed.

The existing Database_CanChangeOwner tests worked around the bug by calling Refresh() before asserting; they now also assert the value before Refresh().

Verified against SQL Server 2019: after SetOwner("sa"), Owner returns sa without Refresh(); after SetOwner with a differently-cased Windows login, Owner returns the server's canonical casing.

This fix was created by Claude and reviewed by Andreas Jordan.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Microsoft.SqlServer.Management.Smo.Database - .SetOwner() does not change .Owner without .Refresh()

1 participant