Skip to content

Commit 4ffb606

Browse files
Test-DbaDbCompression - Fix schema-qualified table filtering (review of #10313)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8db64a1 commit 4ffb606

5 files changed

Lines changed: 132 additions & 11 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
@@ -122,7 +122,7 @@ Find real bugs (logic errors, null refs, incorrect behavior) and fix them. Skip
122122
| 392fc9dea | Set-DbaDbCompression, Invoke-DbaBalanceDataFiles, Invoke-DbaDbPiiScan - Normalize table names via Get-ObjectNameParts (#10312) | DONE | Preserved schema-qualified table matching across all three commands and added unit regression coverage. |
123123
| b2f217f47 | Invoke-TlsWebRequest - Auto-detect system proxy (#10310) | DONE | Preserved configured proxies without Address members, skipped explicit -Proxy overrides, and added regression coverage. |
124124
| 1662d73a6 | New-DbaDatabase - Support Azure Blob Storage paths for data and log files (#10315) | DONE | Preserved rooted Data/Log paths for validation while still trimming file names; added unit regression tests. |
125-
| 241a118ce | Test-DbaDbCompression, Get-DbaDbPageInfo - Normalize table names via Get-ObjectNameParts (#10313) | PENDING | |
125+
| 241a118ce | Test-DbaDbCompression, Get-DbaDbPageInfo - Normalize table names via Get-ObjectNameParts (#10313) | DONE | Preserved schema-/database-qualified table matching and escaped SQL literals; added unit regression tests. |
126126
| 14a47a26c | Export-DbaCsv, Export-DbaDacPackage - Normalize table/schema names via Get-ObjectNameParts (#10314) | PENDING | |
127127
| fe639f6e6 | Remove-DbaDbTableData - Normalize table name via Get-ObjectNameParts (#10316) | PENDING | |
128128
| 070d2ee7f | Fix AppVeyor dbatools.library cache miss by installing to AllUsers scope (#10335) | PENDING | |

public/Get-DbaDbPageInfo.ps1

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,34 @@ function Get-DbaDbPageInfo {
116116
INNER JOIN sys.schemas AS ss ON ss.schema_id = st.schema_id"
117117

118118
if ($Schema) {
119-
$sql = "$sql WHERE ss.name IN ('$($Schema -join "','")')"
119+
$schemaNames = $Schema | ForEach-Object { $_.Replace("'", "''") }
120+
$sql = "$sql WHERE ss.name IN (N'$($schemaNames -join "','")')"
120121
}
121122

122123
if ($Table) {
123-
$tableNames = $Table | ForEach-Object { (Get-ObjectNameParts -ObjectName $_).Name }
124-
if ($schema) {
125-
$sql = "$sql AND st.name IN ('$($tableNames -join "','")')"
124+
$tableParts = $Table | ForEach-Object { Get-ObjectNameParts -ObjectName $_ }
125+
$tableWhereClauses = foreach ($tablePart in $tableParts) {
126+
$tableName = ([string]$tablePart.Name).Replace("'", "''")
127+
$clauseParts = @("st.name = N'$tableName'")
128+
129+
if ($tablePart.Schema) {
130+
$schemaName = ([string]$tablePart.Schema).Replace("'", "''")
131+
$clauseParts += "ss.name = N'$schemaName'"
132+
}
133+
134+
if ($tablePart.Database) {
135+
$databaseName = ([string]$tablePart.Database).Replace("'", "''")
136+
$clauseParts += "DB_NAME() = N'$databaseName'"
137+
}
138+
139+
"($($clauseParts -join " AND "))"
140+
}
141+
142+
$tableWhereClause = $tableWhereClauses -join " OR "
143+
if ($Schema) {
144+
$sql = "$sql AND ($tableWhereClause)"
126145
} else {
127-
$sql = "$sql WHERE st.name IN ('$($tableNames -join "','")')"
146+
$sql = "$sql WHERE $tableWhereClause"
128147
}
129148
}
130149
}

public/Test-DbaDbCompression.ps1

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,30 @@ function Test-DbaDbCompression {
193193
Write-Message -Level System -Message "Bound parameters: $($PSBoundParameters.Keys -join ", ")"
194194

195195
if ($Schema) {
196-
$sqlSchemaWhere = "AND s.name IN ('$($Schema -join "','")')"
196+
$schemaNames = $Schema | ForEach-Object { $_.Replace("'", "''") }
197+
$sqlSchemaWhere = "AND s.name IN (N'$($schemaNames -join "','")')"
197198
}
198199

199200
if ($Table) {
200-
$tableNames = $Table | ForEach-Object { (Get-ObjectNameParts -ObjectName $_).Name }
201-
$sqlTableWhere = "AND t.name IN ('$($tableNames -join "','")')"
201+
$tableParts = $Table | ForEach-Object { Get-ObjectNameParts -ObjectName $_ }
202+
$tableWhereClauses = foreach ($tablePart in $tableParts) {
203+
$tableName = ([string]$tablePart.Name).Replace("'", "''")
204+
$clauseParts = @("t.name = N'$tableName'")
205+
206+
if ($tablePart.Schema) {
207+
$schemaName = ([string]$tablePart.Schema).Replace("'", "''")
208+
$clauseParts += "s.name = N'$schemaName'"
209+
}
210+
211+
if ($tablePart.Database) {
212+
$databaseName = ([string]$tablePart.Database).Replace("'", "''")
213+
$clauseParts += "DB_NAME() = N'$databaseName'"
214+
}
215+
216+
"($($clauseParts -join " AND "))"
217+
}
218+
219+
$sqlTableWhere = "AND ($($tableWhereClauses -join " OR "))"
202220
}
203221

204222
if ($ResultSize) {

tests/Get-DbaDbPageInfo.Tests.ps1

Lines changed: 36 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 = "Get-DbaDbPageInfo",
55
$PSDefaultParameterValues = $TestConfig.Defaults
66
)
@@ -22,6 +22,41 @@ Describe $CommandName -Tag UnitTests {
2222
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
2323
}
2424
}
25+
26+
InModuleScope dbatools {
27+
Context "Table name normalization" {
28+
BeforeAll {
29+
$script:lastQuery = $null
30+
$script:mockDatabase = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database
31+
$script:mockDatabase.Name = "db1"
32+
$script:mockDatabase | Add-Member -Force -MemberType NoteProperty -Name Parent -Value ([PSCustomObject]@{
33+
VersionMajor = 16
34+
})
35+
$script:mockDatabase | Add-Member -Force -MemberType ScriptMethod -Name ExecuteWithResults -Value {
36+
param($Sql)
37+
$script:lastQuery = $Sql
38+
[PSCustomObject]@{
39+
Tables = @(@())
40+
}
41+
}
42+
43+
$script:mockServer = [DbaInstanceParameter]"sql1"
44+
$script:mockServer | Add-Member -Force -MemberType NoteProperty -Name Databases -Value @($script:mockDatabase)
45+
46+
Mock Connect-DbaInstance { $script:mockServer }
47+
}
48+
49+
It "honors schema-qualified -Table input" {
50+
$script:lastQuery = $null
51+
52+
$null = Get-DbaDbPageInfo -SqlInstance "sql1" -Database "db1" -Table "db1.sales.Customer"
53+
$normalizedQuery = $script:lastQuery -replace "\s+", " "
54+
55+
$normalizedQuery | Should -Match "st\.name = N'Customer'\s+AND\s+ss\.name = N'sales'\s+AND\s+DB_NAME\(\) = N'db1'"
56+
$normalizedQuery | Should -Not -Match "st\.name IN"
57+
}
58+
}
59+
}
2560
}
2661

2762
Describe $CommandName -Tag IntegrationTests {

tests/Test-DbaDbCompression.Tests.ps1

Lines changed: 50 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 = "Test-DbaDbCompression",
55
$PSDefaultParameterValues = $TestConfig.Defaults
66
)
@@ -25,6 +25,55 @@ Describe $CommandName -Tag UnitTests {
2525
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
2626
}
2727
}
28+
29+
InModuleScope dbatools {
30+
Context "Table name normalization" {
31+
BeforeAll {
32+
$script:lastQuery = $null
33+
$script:mockDatabase = [PSCustomObject]@{
34+
Name = "db1"
35+
IsAccessible = $true
36+
IsSystemObject = 0
37+
CompatibilityLevel = "Version160"
38+
Status = "Normal"
39+
}
40+
$script:mockServer = [DbaInstanceParameter]"sql1"
41+
$script:mockServer | Add-Member -Force -MemberType NoteProperty -Name ComputerName -Value "sql1"
42+
$script:mockServer | Add-Member -Force -MemberType NoteProperty -Name ServiceName -Value "MSSQLSERVER"
43+
$script:mockServer | Add-Member -Force -MemberType NoteProperty -Name DomainInstanceName -Value "sql1"
44+
$script:mockServer | Add-Member -Force -MemberType NoteProperty -Name ConnectionContext -Value ([PSCustomObject]@{
45+
StatementTimeout = 30
46+
})
47+
$script:mockServer | Add-Member -Force -MemberType NoteProperty -Name EngineEdition -Value "EnterpriseOrDeveloper"
48+
$script:mockServer | Add-Member -Force -MemberType NoteProperty -Name VersionString -Value "16.0.1000.0"
49+
$script:mockServer | Add-Member -Force -MemberType NoteProperty -Name Databases -Value @($script:mockDatabase)
50+
$script:mockServer | Add-Member -Force -MemberType ScriptMethod -Name Query -Value {
51+
param($Sql, $DatabaseName)
52+
$script:lastQuery = $Sql
53+
@()
54+
}
55+
56+
Mock Connect-DbaInstance { $script:mockServer }
57+
Mock Get-DbaBuild {
58+
[PSCustomObject]@{
59+
Build = [PSCustomObject]@{
60+
Major = 16
61+
}
62+
}
63+
}
64+
}
65+
66+
It "honors schema-qualified -Table input" {
67+
$script:lastQuery = $null
68+
69+
$null = Test-DbaDbCompression -SqlInstance "sql1" -Database "db1" -Table "db1.sales.Customer"
70+
$normalizedQuery = $script:lastQuery -replace "\s+", " "
71+
72+
$normalizedQuery | Should -Match "t\.name = N'Customer'\s+AND\s+s\.name = N'sales'\s+AND\s+DB_NAME\(\) = N'db1'"
73+
$normalizedQuery | Should -Not -Match "t\.name IN"
74+
}
75+
}
76+
}
2877
}
2978

3079
Describe $CommandName -Tag IntegrationTests {

0 commit comments

Comments
 (0)