Skip to content

Commit

Permalink
Copy-DbaDbTableData - Enable failback to default group (#9122)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-thul authored Oct 10, 2023
1 parent 0bfa72b commit 166663b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 21 additions & 3 deletions public/Copy-DbaDbTableData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ function Copy-DbaDbTableData {
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.PARAMETER UseDefaultFileGroup
By default, this command will use a filegroup of the same name between
source and target. Use this flag if you'd instead like to use the
default filegroup in the target database.
.NOTES
Tags: Table, Data
Author: Simone Bizzotto (@niphlod)
Expand Down Expand Up @@ -212,6 +217,7 @@ function Copy-DbaDbTableData {
[switch]$Truncate,
[int]$BulkCopyTimeout = 5000,
[int]$CommandTimeout = 0,
[switch]$UseDefaultFileGroup,
[Parameter(ValueFromPipeline)]
[Microsoft.SqlServer.Management.Smo.TableViewBase[]]$InputObject,
[switch]$EnableException
Expand All @@ -231,6 +237,14 @@ function Copy-DbaDbTableData {
$bulkCopyOptions += $([Microsoft.Data.SqlClient.SqlBulkCopyOptions]::$option).value__
}
}

$defaultFGScriptingOption = @{
ScriptingOptionsObject = $(
$so = New-DbaScriptingOption
$so.NoFileGroup = $UseDefaultFileGroup
$so
)
}
}

process {
Expand Down Expand Up @@ -333,11 +347,15 @@ function Copy-DbaDbTableData {
# need these for generating the script of the table and then replacing the schema and name
$schemaNameToReplace = $tempTable.Schema
$tableNameToReplace = $tempTable.Name
$tablescript = $tempTable | Export-DbaScript -Passthru | Out-String
$tablescript = $tempTable |
Export-DbaScript @defaultFGScriptingOption -Passthru |
Out-String
# cleanup
Invoke-DbaQuery -SqlInstance $server -Database $Database -Query "DROP TABLE tempdb..$tempTableName" -EnableException
} else {
$tablescript = $sqlObject | Export-DbaScript -Passthru | Out-String
$tablescript = $sqlObject |
Export-DbaScript @defaultFGScriptingOption -Passthru |
Out-String
$schemaNameToReplace = $sqlObject.Schema
$tableNameToReplace = $sqlObject.Name
}
Expand Down Expand Up @@ -479,4 +497,4 @@ function Copy-DbaDbTableData {
}
}
}
}
}
2 changes: 1 addition & 1 deletion tests/Copy-DbaDbTableData.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
It "Should only contain our specific parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
[object[]]$knownParameters = 'AutoCreateTable', 'BatchSize', 'BulkCopyTimeout', 'CheckConstraints', 'CommandTimeout', 'Database', 'Destination', 'DestinationDatabase', 'DestinationSqlCredential', 'DestinationTable', 'EnableException', 'FireTriggers', 'InputObject', 'KeepIdentity', 'KeepNulls', 'NoTableLock', 'NotifyAfter', 'Query', 'SqlCredential', 'SqlInstance', 'Table', 'Truncate', 'View'
[object[]]$knownParameters = 'AutoCreateTable', 'BatchSize', 'BulkCopyTimeout', 'CheckConstraints', 'CommandTimeout', 'Database', 'Destination', 'DestinationDatabase', 'DestinationSqlCredential', 'DestinationTable', 'EnableException', 'FireTriggers', 'InputObject', 'KeepIdentity', 'KeepNulls', 'NoTableLock', 'NotifyAfter', 'Query', 'SqlCredential', 'SqlInstance', 'Table', 'Truncate', 'View', 'UseDefaultFileGroup'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters

(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should -Be 0
Expand Down

0 comments on commit 166663b

Please sign in to comment.