From ba56e695723607bbe2d6b3cf4161e40f332f330d Mon Sep 17 00:00:00 2001 From: jianjunpei <52251330+jianjunpei@users.noreply.github.com> Date: Thu, 12 Sep 2024 19:53:52 -0500 Subject: [PATCH 1/4] Update Get-DbaHelpIndex.ps1 --- public/Get-DbaHelpIndex.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/public/Get-DbaHelpIndex.ps1 b/public/Get-DbaHelpIndex.ps1 index 86d44d9cb9..fd26d438f1 100644 --- a/public/Get-DbaHelpIndex.ps1 +++ b/public/Get-DbaHelpIndex.ps1 @@ -1,5 +1,6 @@ function Get-DbaHelpIndex { <# + test .SYNOPSIS Returns size, row and configuration information for indexes in databases. @@ -1103,4 +1104,4 @@ function Get-DbaHelpIndex { } } } -} \ No newline at end of file +} From 49941280d7cb2ca607998fd47e77325dce63da08 Mon Sep 17 00:00:00 2001 From: jianjunpei <52251330+jianjunpei@users.noreply.github.com> Date: Thu, 12 Sep 2024 19:54:28 -0500 Subject: [PATCH 2/4] Update Get-DbaHelpIndex.ps1 --- public/Get-DbaHelpIndex.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/public/Get-DbaHelpIndex.ps1 b/public/Get-DbaHelpIndex.ps1 index fd26d438f1..bad29a7768 100644 --- a/public/Get-DbaHelpIndex.ps1 +++ b/public/Get-DbaHelpIndex.ps1 @@ -1,6 +1,5 @@ function Get-DbaHelpIndex { <# - test .SYNOPSIS Returns size, row and configuration information for indexes in databases. From f9fa42f208811c785793415876c2802a37cbf104 Mon Sep 17 00:00:00 2001 From: jianjun pei Date: Sat, 14 Sep 2024 22:56:52 -0500 Subject: [PATCH 3/4] Update Get-DbaHelpIndex.ps1 fixed issue #9447 --- public/Get-DbaHelpIndex.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/public/Get-DbaHelpIndex.ps1 b/public/Get-DbaHelpIndex.ps1 index bad29a7768..5b94d98271 100644 --- a/public/Get-DbaHelpIndex.ps1 +++ b/public/Get-DbaHelpIndex.ps1 @@ -404,7 +404,7 @@ function Get-DbaHelpIndex { name AS IndexName , STUFF((SELECT N', ' + ColumnName FROM cteIndex ci2 - WHERE ci2.name = ci.name + WHERE ci2.name = ci.name AND ci2.object_id=ci.object_id AND ci2.is_included_column = 0 GROUP BY ci2.index_column_id , ci2.ColumnName @@ -414,7 +414,7 @@ function Get-DbaHelpIndex { 2, N'') AS KeyColumns , ISNULL(STUFF((SELECT N', ' + ColumnName FROM cteIndex ci3 - WHERE ci3.name = ci.name + WHERE ci3.name = ci.name AND ci3.object_id=ci.object_id AND ci3.is_included_column = 1 GROUP BY ci3.index_column_id , ci3.ColumnName @@ -801,7 +801,7 @@ function Get-DbaHelpIndex { name AS IndexName , STUFF((SELECT N', ' + ColumnName FROM cteIndex ci2 - WHERE ci2.name = ci.name + WHERE ci2.name = ci.name and ci2.object_id=ci.object_id AND ci2.is_included_column = 0 GROUP BY ci2.index_column_id , ci2.ColumnName @@ -811,7 +811,7 @@ function Get-DbaHelpIndex { 2, N'') AS KeyColumns , ISNULL(STUFF((SELECT N', ' + ColumnName FROM cteIndex ci3 - WHERE ci3.name = ci.name + WHERE ci3.name = ci.name and ci3.object_id=ci.object_id AND ci3.is_included_column = 1 GROUP BY ci3.index_column_id , ci3.ColumnName From 6c2938372eaa97a8d72cfda2d0f1eae777587a9e Mon Sep 17 00:00:00 2001 From: jianjunpei Date: Sat, 21 Sep 2024 19:34:46 -0500 Subject: [PATCH 4/4] update get-dbahelpindex #9447 --- public/Get-DbaHelpIndex.ps1 | 4 ++-- tests/Get-DbaHelpIndex.Tests.ps1 | 31 ++++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/public/Get-DbaHelpIndex.ps1 b/public/Get-DbaHelpIndex.ps1 index 5b94d98271..865082a690 100644 --- a/public/Get-DbaHelpIndex.ps1 +++ b/public/Get-DbaHelpIndex.ps1 @@ -412,7 +412,7 @@ function Get-DbaHelpIndex { FOR XML PATH(N'') , TYPE).value(N'.[1]', N'nvarchar(1000)'), 1, 2, N'') AS KeyColumns , - ISNULL(STUFF((SELECT N', ' + ColumnName + ISNULL(STUFF((SELECT N', ' + ColumnName FROM cteIndex ci3 WHERE ci3.name = ci.name AND ci3.object_id=ci.object_id AND ci3.is_included_column = 1 @@ -809,7 +809,7 @@ function Get-DbaHelpIndex { FOR XML PATH(N'') , TYPE).value(N'.[1]', N'nvarchar(1000)'), 1, 2, N'') AS KeyColumns , - ISNULL(STUFF((SELECT N', ' + ColumnName + ISNULL(STUFF((SELECT N', ' + ColumnName FROM cteIndex ci3 WHERE ci3.name = ci.name and ci3.object_id=ci.object_id AND ci3.is_included_column = 1 diff --git a/tests/Get-DbaHelpIndex.Tests.ps1 b/tests/Get-DbaHelpIndex.Tests.ps1 index 7c5df2491a..f642d4954f 100644 --- a/tests/Get-DbaHelpIndex.Tests.ps1 +++ b/tests/Get-DbaHelpIndex.Tests.ps1 @@ -2,13 +2,14 @@ $CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan . "$PSScriptRoot\constants.ps1" +Write-host -Object "${script:instance2}" -ForegroundColor Cyan Describe "$CommandName Unit Tests" -Tag 'UnitTests' { Context "Validate parameters" { - [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')} + [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') } [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', 'InputObject', 'ObjectName', 'IncludeStats', 'IncludeDataTypes', 'Raw', 'IncludeFragmentation', 'EnableException' $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters It "Should only contain our specific parameters" { - (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0 + (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0 } } } @@ -22,12 +23,16 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" { $server.Query("Insert into test values ('value1',1),('value2',2)", $dbname) $server.Query("create statistics dbatools_stats on test (col2)", $dbname) $server.Query("select * from test", $dbname) + $server.Query("create table t1(c1 int,c2 int,c3 int,c4 int)", $dbname) + $server.Query("create nonclustered index idx_1 on t1(c1) include(c3)", $dbname) + $server.Query("create table t2(c1 int,c2 int,c3 int,c4 int)", $dbname) + $server.Query("create nonclustered index idx_1 on t2(c1,c2) include(c3,c4)", $dbname) } AfterAll { $null = Get-DbaDatabase -SqlInstance $script:instance2 -Database $dbname | Remove-DbaDatabase -Confirm:$false } Context "Command works for indexes" { - $results = Get-DbaHelpIndex -SqlInstance $script:instance2 -Database $dbname + $results = Get-DbaHelpIndex -SqlInstance $script:instance2 -Database $dbname -ObjectName Test It 'Results should be returned' { $results | Should Not BeNullOrEmpty } @@ -42,7 +47,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" { } } Context "Command works when including statistics" { - $results = Get-DbaHelpIndex -SqlInstance $script:instance2 -Database $dbname -IncludeStats | Where-Object {$_.Statistics} + $results = Get-DbaHelpIndex -SqlInstance $script:instance2 -Database $dbname -ObjectName Test -IncludeStats | Where-Object { $_.Statistics } It 'Results should be returned' { $results | Should Not BeNullOrEmpty } @@ -51,7 +56,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" { } } Context "Command output includes data types" { - $results = Get-DbaHelpIndex -SqlInstance $script:instance2 -Database $dbname -IncludeDataTypes + $results = Get-DbaHelpIndex -SqlInstance $script:instance2 -Database $dbname -ObjectName Test -IncludeDataTypes It 'Results should be returned' { $results | Should Not BeNullOrEmpty } @@ -60,7 +65,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" { } } Context "Formatting is correct" { - $results = Get-DbaHelpIndex -SqlInstance $script:instance2 -Database $dbname -IncludeFragmentation + $results = Get-DbaHelpIndex -SqlInstance $script:instance2 -Database $dbname -ObjectName Test -IncludeFragmentation It 'Formatted as strings' { $results.IndexReads | Should BeOfType 'String' $results.IndexUpdates | Should BeOfType 'String' @@ -72,7 +77,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" { } } Context "Formatting is correct for raw" { - $results = Get-DbaHelpIndex -SqlInstance $script:instance2 -Database $dbname -raw -IncludeFragmentation + $results = Get-DbaHelpIndex -SqlInstance $script:instance2 -Database $dbname -ObjectName Test -raw -IncludeFragmentation It 'Formatted as Long' { $results.IndexReads | Should BeOfType 'Long' $results.IndexUpdates | Should BeOfType 'Long' @@ -84,4 +89,16 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" { $results.IndexFragInPercent | Should BeOfType 'Double' } } + Context "Result is correct for tables having the indexes with the same names" { + $results = Get-DbaHelpIndex -SqlInstance $script:instance2 -Database $dbname + It 'Table t1 has correct index key columns and included columns' { + $results.where({ $_.object -eq '[dbo].[t1]' }).KeyColumns | Should -be 'c1' + $results.where({ $_.object -eq '[dbo].[t1]' }).IncludeColumns | Should -be 'c3' + } + It 'Table t2 has correct index key columns and included columns' { + $results.where({ $_.object -eq '[dbo].[t2]' }).KeyColumns | Should -be 'c1, c2' + $results.where({ $_.object -eq '[dbo].[t2]' }).IncludeColumns | Should -be 'c3, c4' + } + + } } \ No newline at end of file