Skip to content

Commit

Permalink
Connect-DbaInstance: Fix for issue when changing database context (#9505
Browse files Browse the repository at this point in the history
)
  • Loading branch information
andreasjordan authored Oct 14, 2024
1 parent 7eec680 commit 0b9be65
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion public/Connect-DbaInstance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -639,10 +639,13 @@ function Connect-DbaInstance {
if ($Database) {
# Save StatementTimeout because it might be reset on GetDatabaseConnection
$savedStatementTimeout = $connContext.StatementTimeout
$connContext = $connContext.GetDatabaseConnection($Database)
$connContext = $connContext.GetDatabaseConnection($Database, $false)
$connContext.StatementTimeout = $savedStatementTimeout
}
$server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $connContext
if ($Database -and $server.ConnectionContext.CurrentDatabase -ne $Database) {
Write-Message -Level Warning -Message "Changing connection context to database $Database was not successful. Current database is $($server.ConnectionContext.CurrentDatabase). Please open an issue on https://github.com/dataplat/dbatools/issues."
}
} else {
$server = $inputObject
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Connect-DbaInstance.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
$serverClone.ConnectionContext.ServerInstance | Should -Match '^ADMIN:'
$serverClone | Disconnect-DbaInstance
}

It "clones when using Backup-DabInstace" {
$server = Connect-DbaInstance -SqlInstance $script:instance1 -Database tempdb
$null = Backup-DbaDatabase -SqlInstance $server -Database msdb
$null = Backup-DbaDatabase -SqlInstance $server -Database msdb -WarningVariable warn
$warn | Should -BeNullOrEmpty
}
}

Context "multiple connections are properly made using strings" {
Expand Down

0 comments on commit 0b9be65

Please sign in to comment.