Skip to content

Commit 52ee5ca

Browse files
Export-DbaCsv - Fix table-name validation regression (review of #10314)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4ffb606 commit 52ee5ca

3 files changed

Lines changed: 33 additions & 4 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
@@ -123,7 +123,7 @@ Find real bugs (logic errors, null refs, incorrect behavior) and fix them. Skip
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. |
125125
| 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. |
126-
| 14a47a26c | Export-DbaCsv, Export-DbaDacPackage - Normalize table/schema names via Get-ObjectNameParts (#10314) | PENDING | |
126+
| 14a47a26c | Export-DbaCsv, Export-DbaDacPackage - Normalize table/schema names via Get-ObjectNameParts (#10314) | DONE | Export-DbaCsv invalid-name handling was already fixed by follow-up 74a2d1ae1; this review added Export-DbaDacPackage table-name validation and a unit regression test.
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 | |
129129
| e3f6cc121 | Fix test for Invoke-DbaAdvancedUpdate (#10334) | PENDING | |

public/Export-DbaDacPackage.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ function Export-DbaDacPackage {
186186
foreach ($tableItem in $Table) {
187187
# Use Get-ObjectNameParts to correctly handle bracketed names like [Gross.Table.Name]
188188
$nameParts = Get-ObjectNameParts -ObjectName $tableItem
189+
if (-not $nameParts.Parsed -or -not $nameParts.Name) {
190+
Stop-Function -Message "Table value '$tableItem' is not a valid one-, two-, or three-part name. Use bracket quoting for names that contain periods."
191+
return
192+
}
189193
if ($nameParts.Schema) {
190194
$schemaName = $nameParts.Schema
191195
} else {
@@ -224,7 +228,7 @@ WHERE database_id > 4 -- Exclude system databases (master=1, tempdb=2, model=3,
224228
AND state = 0 -- Only ONLINE databases (OnlyAccessible equivalent)
225229
"@
226230

227-
$sqlParams = @{}
231+
$sqlParams = @{ }
228232

229233
# Add ExcludeDatabase filter if specified (using parameterized queries to prevent SQL injection)
230234
if ($ExcludeDatabase) {
@@ -369,4 +373,4 @@ WHERE database_id > 4 -- Exclude system databases (master=1, tempdb=2, model=3,
369373
}
370374
}
371375
}
372-
}
376+
}

tests/Export-DbaDacPackage.Tests.ps1

Lines changed: 26 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 = "Export-DbaDacPackage",
55
$PSDefaultParameterValues = $TestConfig.Defaults
66
)
@@ -30,6 +30,31 @@ Describe $CommandName -Tag UnitTests {
3030
}
3131
}
3232

33+
Describe $CommandName -Tag UnitTests {
34+
InModuleScope dbatools {
35+
Context "Table validation" {
36+
BeforeEach {
37+
Mock Test-ExportDirectory { }
38+
Mock Test-FunctionInterrupt { $false }
39+
Mock Connect-DbaInstance {
40+
throw "Connect-DbaInstance should not be called for invalid table filters"
41+
}
42+
Mock Stop-Function {
43+
throw $Message
44+
}
45+
}
46+
47+
It "rejects invalid table names before connecting" {
48+
{
49+
Export-DbaDacPackage -SqlInstance "sql1" -Database "db1" -Table "a.b.c.d" -Path "C:\temp"
50+
} | Should -Throw "*not a valid one-, two-, or three-part name*"
51+
52+
Should -Invoke Connect-DbaInstance -Times 0 -Exactly
53+
}
54+
}
55+
}
56+
}
57+
3358
Describe $CommandName -Tag IntegrationTests {
3459
BeforeAll {
3560
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.

0 commit comments

Comments
 (0)