Skip to content

Commit 861b0db

Browse files
authored
Import-DbaCsv: Add -NoColumnOptimize switch (#10195)
1 parent 218a98a commit 861b0db

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

public/Import-DbaCsv.ps1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ function Import-DbaCsv {
7070
For proper type inference (int, decimal, datetime2, varchar vs nvarchar), use -SampleRows or -DetectColumnTypes instead.
7171
For production use with specific constraints, create tables manually with appropriate data types, indexes, and constraints.
7272
73+
.PARAMETER NoColumnOptimize
74+
Skips the automatic column size optimization that runs after AutoCreateTable imports.
75+
By default, AutoCreateTable creates nvarchar(MAX) columns and then shrinks them to fit the imported data.
76+
Use this switch when importing multiple CSV files into the same auto-created table, so that later files
77+
with longer values are not rejected due to columns being shrunk to fit only the first file's data.
78+
7379
.PARAMETER Truncate
7480
Removes all existing data from the destination table before importing. The truncate operation is part of the transaction.
7581
Use this for full data refreshes where you want to replace all existing data with the CSV contents.
@@ -537,6 +543,7 @@ function Import-DbaCsv {
537543
[hashtable]$ColumnMap,
538544
[switch]$KeepOrdinalOrder,
539545
[switch]$AutoCreateTable,
546+
[switch]$NoColumnOptimize,
540547
[switch]$NoProgress,
541548
[switch]$NoHeaderRow,
542549
[switch]$UseFileNameForSchema,
@@ -1436,7 +1443,7 @@ WHERE c.object_id = OBJECT_ID(@tableName)
14361443
}
14371444

14381445
# Optimize column sizes after commit if we created a fat table
1439-
if ($createdFatTable) {
1446+
if ($createdFatTable -and -not $NoColumnOptimize) {
14401447
try {
14411448
Optimize-ColumnSize -SqlConn $sqlconn -Schema $schema -Table $table
14421449
} catch {
@@ -1449,7 +1456,7 @@ WHERE c.object_id = OBJECT_ID(@tableName)
14491456
} catch {
14501457
}
14511458
}
1452-
} elseif ($completed -and $createdFatTable) {
1459+
} elseif ($completed -and $createdFatTable -and -not $NoColumnOptimize) {
14531460
# NoTransaction mode - still optimize if we created a fat table
14541461
try {
14551462
Optimize-ColumnSize -SqlConn $sqlconn -Schema $schema -Table $table

tests/Import-DbaCsv.Tests.ps1

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Describe $CommandName -Tag UnitTests {
6262
"Culture",
6363
"SampleRows",
6464
"DetectColumnTypes",
65+
"NoColumnOptimize",
6566
"Parallel",
6667
"ThrottleLimit",
6768
"ParallelBatchSize"
@@ -711,6 +712,42 @@ José García,São Paulo
711712
Invoke-DbaQuery -SqlInstance $server -Query "DROP TABLE $tableName" -ErrorAction SilentlyContinue
712713
Remove-Item $filePath -ErrorAction SilentlyContinue
713714
}
715+
716+
It "skips optimization when NoColumnOptimize is specified" {
717+
$filePath = "$($TestConfig.Temp)\noopt-$(Get-Random).csv"
718+
$server = Connect-DbaInstance $TestConfig.InstanceMulti1 -Database tempdb
719+
$tableName = "NoOptTest$(Get-Random)"
720+
721+
$csvContent = @"
722+
ShortCol,MediumCol
723+
ABC,Hello
724+
XYZ,World
725+
"@
726+
$csvContent | Out-File -FilePath $filePath -Encoding UTF8
727+
728+
$splatImport = @{
729+
Path = $filePath
730+
SqlInstance = $server
731+
Database = "tempdb"
732+
Table = $tableName
733+
AutoCreateTable = $true
734+
NoColumnOptimize = $true
735+
}
736+
$result = Import-DbaCsv @splatImport
737+
738+
$result.RowsCopied | Should -Be 2
739+
740+
# Columns should remain at nvarchar(MAX) since optimization was skipped
741+
$columns = Get-DbaDbTable -SqlInstance $server -Database tempdb -Table $tableName | Select-Object -ExpandProperty Columns
742+
$shortCol = $columns | Where-Object Name -eq "ShortCol"
743+
$mediumCol = $columns | Where-Object Name -eq "MediumCol"
744+
745+
$shortCol.DataType.MaximumLength | Should -Be -1
746+
$mediumCol.DataType.MaximumLength | Should -Be -1
747+
748+
Invoke-DbaQuery -SqlInstance $server -Query "DROP TABLE $tableName" -ErrorAction SilentlyContinue
749+
Remove-Item $filePath -ErrorAction SilentlyContinue
750+
}
714751
}
715752

716753
Context "Type detection with SampleRows and DetectColumnTypes" {

0 commit comments

Comments
 (0)