Invoke-DbaAdvancedRestore - Leave the connection of the caller alone - #10576
Open
andreasjordan wants to merge 1 commit into
Open
Invoke-DbaAdvancedRestore - Leave the connection of the caller alone#10576andreasjordan wants to merge 1 commit into
andreasjordan wants to merge 1 commit into
Conversation
The command closed the connection after every single backup file, no matter who opened it. On a session-scoped connection that takes the session of the caller with it, and Restore-DbaDatabase connects once and hands that server object down to here, so this is the connection of its caller that is closed. Connect-DbaInstance now reports through IsNewConnectionReference whether it opened the connection, and only a connection opened here is closed. The per-backup-file disconnect is gone. Reconnecting between the files of one restore chain serves nothing, SMO just opens the connection again for the next file. The disconnect at the end was guarded by ConnectionContext.exists, which is not a property of a ServerConnection, so it never ran at all; it now runs once after all databases are done, for a connection this command owns. While in there: the verbose message in the catch said the server connection was being closed, which the catch has never done. (do Invoke-DbaAdvancedRestore, Restore-DbaDatabase) Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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
Purpose
Last command of the inventory in #10554, and the one with the widest reach:
Restore-DbaDatabaseconnects once and hands that server object down to here, so the connection that gets closed is the one its caller passed in.That sits in the
finallyof the loop over the backup files, so it fires after every single backup file. Verified on SQL Server 2019 withConnect-DbaInstance -NonPooledConnectionand a temp table as the session marker:Approach
Connect-DbaInstancereports throughIsNewConnectionReferencewhether it opened the connection, and only a connection opened here is closed.The per-backup-file disconnect is gone rather than guarded. Reconnecting between the files of one restore chain serves nothing - SMO opens the connection again for the next file anyway.
The disconnect at the end of the loop over the databases was guarded by
$server.ConnectionContext.exists:existsis not a property of aServerConnection, so the condition was always$nulland the block never ran. It now runs once, after all databases are done, and only for a connection this command opened itself.Two things checked while in there and deliberately left alone:
$server.ConnectionContext.Connect()after killing processes or dropping the database is idempotent on an open connection and does not lose the session, so it stays as it is.The only other change is the verbose message in the
catch, which said the server connection was being closed - thecatchhas never done that.Commands to test
Tests
tests\Invoke-DbaAdvancedRestore.Tests.ps1had unit tests only. Three integration contexts were added onInstanceSingle, all against a full plus a log backup so that the restore really runs more than one backup file - a single file would not show the disconnect that sat inside that loop:Restore-DbaDatabasewith a server object of the caller does the same, which is the path this reaches in practice14 tests, all passing. Against
development3 of them fail, covering both the direct call and theRestore-DbaDatabasepath.馃 Generated with Claude Code