Skip to content

Commit 63a9dee

Browse files
Connect-DbaInstance - Keep the tab completion cache of instance names an array (#10672)
1 parent 1a96469 commit 63a9dee

6 files changed

Lines changed: 70 additions & 14 deletions

File tree

private/dynamicparams/sqlinstance.ps1

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ if (-not [Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"]
55

66
# Load user-defined instances from config (set via Add-DbaInstanceList)
77
foreach ($instance in (Get-DbatoolsConfigValue -FullName "TabExpansion.KnownInstances" -Fallback @())) {
8-
if ($instance -and [Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] -notcontains $instance) {
9-
[Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] += $instance
10-
}
8+
Add-DbaTeppInstanceName -Name $instance
119
}
1210

1311
# Load from environment variable (comma-separated list, e.g. set in PowerShell profile)
1412
if ($env:DBATOOLS_KNOWN_INSTANCES) {
1513
foreach ($instance in ($env:DBATOOLS_KNOWN_INSTANCES -split ",")) {
16-
$lower = $instance.Trim().ToLowerInvariant()
17-
if ($lower -and [Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] -notcontains $lower) {
18-
[Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] += $lower
19-
}
14+
Add-DbaTeppInstanceName -Name $instance
2015
}
2116
}
2217
#endregion Initialize Cache
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function Add-DbaTeppInstanceName {
2+
<#
3+
.SYNOPSIS
4+
Adds instance names to the tab completion cache for -SqlInstance, keeping the cache an array.
5+
6+
.DESCRIPTION
7+
The cache [Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] starts as $null.
8+
Appending to it with += turned it into a string, after which += concatenated every further name
9+
onto that string and -notcontains compared the whole string, so every later connection appended
10+
its name again and the completer offered one long blob. This function rebuilds the cache as an
11+
array every time, lower cases the names like the completer expects, and skips duplicates.
12+
13+
.PARAMETER Name
14+
The instance names to add. Empty entries are ignored.
15+
16+
.EXAMPLE
17+
PS C:\> Add-DbaTeppInstanceName -Name $instance.FullSmoName
18+
19+
Adds the instance to the completion cache of the current session.
20+
#>
21+
[CmdletBinding()]
22+
param (
23+
[AllowEmptyString()]
24+
[AllowNull()]
25+
[string[]]$Name
26+
)
27+
28+
$knownInstances = @([Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] | Where-Object { $PSItem })
29+
foreach ($item in $Name) {
30+
if (-not $item) {
31+
continue
32+
}
33+
$lower = $item.Trim().ToLowerInvariant()
34+
if ($lower -and $knownInstances -notcontains $lower) {
35+
$knownInstances += $lower
36+
}
37+
}
38+
[Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] = $knownInstances
39+
}

public/Add-DbaInstanceList.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ function Add-DbaInstanceList {
8484
}
8585

8686
# Update the TEPP cache immediately for this session
87-
if ([Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] -notcontains $lower) {
88-
[Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] += $lower
89-
}
87+
Add-DbaTeppInstanceName -Name $lower
9088
}
9189
}
9290

public/Connect-DbaInstance.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,9 +1534,7 @@ SELECT SERVERPROPERTY('ProductVersion')
15341534
[Dataplat.Dbatools.TabExpansion.TabExpansionHost]::SetInstance($instance.FullSmoName.ToLowerInvariant(), $teppConnectionContext, ($server.ConnectionContext.FixedServerRoles -match "SysAdmin"))
15351535

15361536
# Update cache for instance names
1537-
if ([Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] -notcontains $instance.FullSmoName.ToLowerInvariant()) {
1538-
[Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"] += $instance.FullSmoName.ToLowerInvariant()
1539-
}
1537+
Add-DbaTeppInstanceName -Name $instance.FullSmoName
15401538

15411539
# Update lots of registered stuff
15421540
# Default for [Dataplat.Dbatools.TabExpansion.TabExpansionHost]::TeppSyncDisabled is $true, so will not run by default

tests/Add-DbaInstanceList.Tests.ps1

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ Describe $CommandName -Tag UnitTests {
2323
Describe $CommandName -Tag IntegrationTests {
2424
BeforeAll {
2525
$instanceName = "dbatoolsci_testinstance_$(Get-Random)"
26+
$secondInstanceName = "dbatoolsci_testinstance2_$(Get-Random)"
2627
}
2728

2829
AfterAll {
29-
$null = Remove-DbaInstanceList -SqlInstance $instanceName -ErrorAction SilentlyContinue
30+
$null = Remove-DbaInstanceList -SqlInstance $instanceName, $secondInstanceName -ErrorAction SilentlyContinue
3031
}
3132

3233
Context "adds instances to the list" {
@@ -49,5 +50,17 @@ Describe $CommandName -Tag IntegrationTests {
4950
$result = Get-DbaInstanceList
5051
($result | Where-Object { $PSItem -eq $instanceName.ToLowerInvariant() }).Count | Should -Be 1
5152
}
53+
54+
It "keeps the TEPP cache an array when more than one instance is known" {
55+
# The cache starts as $null, and $null += "x" made it a string. Every further name was then
56+
# concatenated onto that string and the completer offered one long blob instead of names.
57+
Add-DbaInstanceList -SqlInstance $secondInstanceName
58+
Add-DbaInstanceList -SqlInstance $instanceName
59+
$cache = [Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"]
60+
$cache -is [array] | Should -BeTrue
61+
$cache | Should -Contain $instanceName.ToLowerInvariant()
62+
$cache | Should -Contain $secondInstanceName.ToLowerInvariant()
63+
@($cache | Where-Object { $PSItem -eq $instanceName.ToLowerInvariant() }).Count | Should -Be 1
64+
}
5265
}
5366
}

tests/Connect-DbaInstance.Tests.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,4 +1054,17 @@ SELECT SERVERPROPERTY('ProductVersion')
10541054
$tsqlVersionMajor | Should -Be $smoVersionMajor
10551055
}
10561056
}
1057+
1058+
Context "tab completion cache of instance names" {
1059+
It "keeps every connected instance as its own entry" {
1060+
# The cache starts as $null, and $null += "x" made it a string. Every further name was then
1061+
# concatenated onto that string and the completer offered one long blob instead of names.
1062+
$null = Connect-DbaInstance -SqlInstance $TestConfig.InstanceMulti1
1063+
$null = Connect-DbaInstance -SqlInstance $TestConfig.InstanceMulti2
1064+
$cache = [Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"]
1065+
$cache -is [array] | Should -BeTrue
1066+
$cache | Should -Contain $TestConfig.InstanceMulti1.ToLowerInvariant()
1067+
$cache | Should -Contain $TestConfig.InstanceMulti2.ToLowerInvariant()
1068+
}
1069+
}
10571070
}

0 commit comments

Comments
 (0)