Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Update this document for externally visible changes. Put most recent changes first.
Once we push a new version to nuget.org add a double hash header for that version.

- Fix `Database.Owner` returning the previous owner after `Database.SetOwner()` until `Refresh()` was called. The cached property is now invalidated and re-fetched from the server on next access.

## 181.37.1

- Add `ServerConnection.Edition` and `ServerConnection.EditionID` to expose the values of `SERVERPROPERTY('Edition')` and `SERVERPROPERTY('EditionID')`.
Expand Down
10 changes: 10 additions & 0 deletions src/FunctionalTest/Smo/ScriptingTests/Database_SmoTestSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,8 +1190,17 @@ public void Database_CanChangeOwner()
{

database.SetOwner(login.Name);
Assert.That(database.Owner, Is.EqualTo(login.Name), "Owner of database should reflect the new owner immediately after calling SetOwner, without needing Refresh");
Comment thread
andreasjordan marked this conversation as resolved.
database.Refresh();
Assert.That(database.Owner, Is.EqualTo(login.Name), "Owner of database was not changed to '{0}' after calling SetOwner");

if (!database.Parent.Information.IsCaseSensitive)
{
//The server resolves the login case-insensitively and keeps its own spelling,
//so Owner has to return the stored name and not the casing passed to SetOwner
database.SetOwner(login.Name.ToUpperInvariant());
Assert.That(database.Owner, Is.EqualTo(login.Name), "Owner of database should return the login name as stored on the server, not the casing passed to SetOwner");
}
}
finally
{
Expand Down Expand Up @@ -1227,6 +1236,7 @@ public void Database_CanChangeOwner_WithExistingUser()
try
{
database.SetOwner(login.Name, dropExistingUser: true);
Assert.That(database.Owner, Is.EqualTo(login.Name), "Owner of database should reflect the new owner immediately after calling SetOwner, without needing Refresh");
database.Refresh();
Assert.That(database.Owner, Is.EqualTo(login.Name), "Owner of database was not changed to '{0}' after calling SetOwner");
}
Expand Down
12 changes: 12 additions & 0 deletions src/Microsoft/SqlServer/Management/Smo/DatabaseBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,18 @@ private void SetOwnerImpl(string loginName, bool dropExistingUser)
this.ScriptChangeOwner(query, loginName);

this.ExecutionManager.ExecuteNonQuery(query);

if (!this.ExecutionManager.Recording)
{
//The query only propagates the new owner to the server, so the cached
//Owner value is stale now. Invalidate it so it is retrieved from the
//server on next access. We don't store the caller-provided name because
//the server may canonicalize it differently (e.g. different casing on
//a case-insensitive server).
Property owner = this.Properties.Get("Owner");
owner.SetRetrieved(false);
this.propertyBagState = PropertyBagState.Lazy;
}
}
}
catch (Exception e)
Expand Down