Skip to content

Commit 70dcc26

Browse files
Compare-DbaLogin - Fix stale destination reuse on connection failure (review of #10319)
Co-Authored-By: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 98f3c52 commit 70dcc26

3 files changed

Lines changed: 69 additions & 3 deletions

File tree

docs/trackers/features/commit-bug-review-TRACKER.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Find real bugs (logic errors, null refs, incorrect behavior) and fix them. Skip
138138
| 9899bd274 | Copy-DbaDbTableData - Add -ScriptingOptionsObject parameter (#10317) | DONE | Propagated ScriptingOptionsObject to Copy-DbaDbViewData and added unit coverage. |
139139
| 0c486b964 | Backup-DbaDbCertificate: Don't use decryption password if cert encrypted by master key (#10329) | DONE | Reviewed SMO export overload selection for master-key-encrypted certs; no bugs found. |
140140
| db77a3476 | Find-DbaObject - Add unified command to search database objects by name (#10321) | DONE | Added database DDL trigger name search via sys.triggers and covered it with an integration regression test. |
141-
| 6416b4e91 | Add Compare-DbaLogin command (#10319) | PENDING | |
141+
| 6416b4e91 | Add Compare-DbaLogin command (#10319) | DONE | Skipped failed destination connections instead of reusing the previous server; added unit regression coverage. |
142142
| bee08f8e7 | Copy-DbaSsisCatalog - Add standard MigrationObject output, integrate with Start-DbaMigration (#10311) | PENDING | |
143143
| 21e4795ee | Get-DbaService - Add PowerBI Report Server detection (#10298) | PENDING | |
144144
| 828ebc3b3 | Get-DbaBackupInformation - Fix inconsistencies with Get-DbaDbBackupHistory (#10308) | PENDING | |

public/Compare-DbaLogin.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function Compare-DbaLogin {
8686
Compares user-created logins between sql1 and sql2, excluding built-in system logins.
8787
8888
.EXAMPLE
89-
PS C:\> Compare-DbaLogin -Source sql1 -Destination sql2, sql3 -Login 'appuser', 'reportuser'
89+
PS C:\> Compare-DbaLogin -Source sql1 -Destination sql2, sql3 -Login "appuser", "reportuser"
9090
9191
Compares the specified logins between sql1 and both sql2 and sql3.
9292
#>
@@ -129,6 +129,7 @@ function Compare-DbaLogin {
129129
if (Test-FunctionInterrupt) { return }
130130

131131
foreach ($destInstance in $Destination) {
132+
$destServer = $null
132133
try {
133134
$destServer = Connect-DbaInstance -SqlInstance $destInstance -SqlCredential $DestinationSqlCredential
134135
} catch {

tests/Compare-DbaLogin.Tests.ps1

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
3-
$ModuleName = "dbatools",
3+
$ModuleName = "dbatools",
44
$CommandName = "Compare-DbaLogin",
55
$PSDefaultParameterValues = $TestConfig.Defaults
66
)
@@ -25,6 +25,71 @@ Describe $CommandName -Tag UnitTests {
2525
}
2626
}
2727

28+
Describe $CommandName -Tag UnitTests {
29+
InModuleScope dbatools {
30+
Context "Destination connection failures" {
31+
BeforeEach {
32+
function New-MockCompareDbaLoginInstance {
33+
param(
34+
[string]$Name
35+
)
36+
37+
$instance = [Dataplat.Dbatools.Parameter.DbaInstanceParameter]$Name
38+
$instance | Add-Member -NotePropertyName Name -NotePropertyValue $Name -Force
39+
$instance
40+
}
41+
42+
Mock Test-FunctionInterrupt { $false }
43+
Mock Stop-Function { }
44+
Mock Connect-DbaInstance {
45+
switch ("$SqlInstance") {
46+
"source1" {
47+
New-MockCompareDbaLoginInstance -Name "source1"
48+
}
49+
"dest1" {
50+
New-MockCompareDbaLoginInstance -Name "dest1"
51+
}
52+
"dest2" {
53+
throw "dest2 unavailable"
54+
}
55+
}
56+
}
57+
Mock Get-DbaLogin {
58+
switch ($SqlInstance.Name) {
59+
"source1" {
60+
[PSCustomObject]@{
61+
Name = "login1"
62+
LoginType = "SqlLogin"
63+
}
64+
}
65+
"dest1" {
66+
[PSCustomObject]@{
67+
Name = "login1"
68+
LoginType = "SqlLogin"
69+
}
70+
}
71+
default {
72+
throw "Unexpected Get-DbaLogin call for $($SqlInstance.Name)"
73+
}
74+
}
75+
}
76+
}
77+
78+
It "skips failed destinations without reusing the previous connection" {
79+
$result = Compare-DbaLogin -Source "source1" -Destination "dest1", "dest2"
80+
81+
$result.Count | Should -Be 1
82+
$result.SourceServer | Should -Be "source1"
83+
$result.DestinationServer | Should -Be "dest1"
84+
Should -Invoke Get-DbaLogin -Times 2 -Exactly
85+
Should -Invoke Stop-Function -Times 1 -Exactly -ParameterFilter {
86+
$Message -eq "Failure connecting to dest2" -and $Continue
87+
}
88+
}
89+
}
90+
}
91+
}
92+
2893
Describe $CommandName -Tag IntegrationTests {
2994
BeforeAll {
3095
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

0 commit comments

Comments
 (0)