Invoke-DbaDbUpgrade - Leave the database context of the caller alone - #10602
Closed
andreasjordan wants to merge 1 commit into
Closed
Invoke-DbaDbUpgrade - Leave the database context of the caller alone#10602andreasjordan wants to merge 1 commit into
andreasjordan wants to merge 1 commit into
Conversation
The four maintenance statements ran through SMO's own Database.ExecuteNonQuery, which issues a USE on the execution manager of the database - the connection context of the parent server, which belongs to the caller - and never switches back. They go through our Invoke script method now, which puts the previous database back in a finally. That alone is not enough: enumerating $db.Views for the view refresh moves the connection just as well, and no script method can cover an SMO collection. So the database of the caller is read before any work starts and put back around the view refresh. Both halves are covered by integration tests that fail without them. (do Invoke-DbaDbUpgrade) Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This was referenced Aug 25, 2026
Collaborator
Author
|
Closing this in favour of #10603. The fix for Everything measured and tested here still applies and is repeated in the body of #10603. This text was created by Claude and reviewed by Andreas Jordan. |
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.
Type of Change
This is also the first command of bucket B of #10555. It deliberately carries no closing keyword for #10555: that issue was closed by accident when #10580 merged, because "fixes #10555 in part" is read as a plain "fixes" by the linking parser. Buckets B and C are still open.
Purpose
Invoke-DbaDbUpgradehanded the connection back pointing at the database it had just upgraded. This is the command from the provisioning script in #10555 that started the whole workstream, and it is a bucket B site, so the script method fix in #10579 does not reach it.Approach
Two independent causes, and fixing either one alone leaves the leak in place under some parameter combination.
1. The four maintenance statements. They ran through SMO's own
Database.ExecuteNonQuery, which issues aUSEon the execution manager of the database - the connection context of the parent server, which belongs to the caller - and never switches back. They go through ourInvokescript method now, which since #10579 restores the previous database in afinally. The statements still run in the database they always ran in; nothing else about them changes.2. Enumerating
$db.Viewsmoves the connection just as well. This one is worth flagging beyond this command, because it is not in the inventory on #10555 and it cannot be fixed by any script method - it is not a call we make, it is a property getter. Measured on SQL Server 2019 with-NonPooledConnection, readingDB_NAME()on the caller's connection afterwards:So the command reads the database of the caller before any work starts - not just before the view refresh, or a statement that already moved the connection would be recorded as "what the caller had" - and puts it back around the view refresh, in a
finally, case sensitively, exactly asSet-DbaTempDbConfigdoes since #10580. A failing restore warns rather than throwing, so housekeeping can never become the outcome of the command.Tests
Three integration contexts in
tests/Invoke-DbaDbUpgrade.Tests.ps1, all against a non-pooled connection, because a pooled one that closes between two calls reconnects at its default database and hides the leak:Success- a command that did nothing cannot leak, so without this the leak assertion would pass for the wrong reasontempdband using-Force, which fails for a fix that restores "to master" instead of restoring what the caller had-NoRefreshView -Force, the one case the restore around the view enumeration cannot cover, so the statements have to put the database back themselvesVerified to have teeth, in the lab against
SQL03\SQL2019:Invokereverted toExecuteNonQuery-NoRefreshViewcontext)Commands to test
This text was created by Claude and reviewed by Andreas Jordan.