Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions public/New-DbaDbTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,20 @@ function New-DbaDbTable {
}
}

# Parse the Name parameter to handle bracket-quoted names and two-part names like [schema].[table]
if (Test-Bound -ParameterName Name) {
$parsedName = Get-ObjectNameParts -ObjectName $Name
if ($parsedName.Parsed) {
if ($parsedName.Schema -and -not (Test-Bound -ParameterName Schema)) {
$Schema = $parsedName.Schema
}
$Name = $parsedName.Name
} else {
Stop-Function -Message "Could not parse -Name '$Name' as a valid object name."
return
}
}

foreach ($instance in $SqlInstance) {
$InputObject += Get-DbaDatabase -SqlInstance $instance -SqlCredential $SqlCredential -Database $Database
}
Expand Down
31 changes: 31 additions & 0 deletions tests/New-DbaDbTable.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,37 @@ Describe $CommandName -Tag IntegrationTests {
$tableWithSchema[2] | Should -Match "$tableName"
}
}
Context "Should handle bracket-quoted names and two-part names" {
BeforeAll {
$map = @{
Name = "testId"
Type = "int"
}
}
It "Strips brackets from a bracket-quoted table name" {
$random = Get-Random
$tableName = "table_bracket_$random"
$result = New-DbaDbTable -SqlInstance $TestConfig.InstanceMulti1 -Database $dbname -Name "[$tableName]" -ColumnMap $map
$result.Name | Should -Be $tableName
$result.Schema | Should -Be "dbo"
}
It "Parses schema and table from a two-part bracket-quoted name" {
$random = Get-Random
$tableName = "table_twopart_$random"
$schemaName = "schema_twopart_$random"
$result = New-DbaDbTable -SqlInstance $TestConfig.InstanceMulti1 -Database $dbname -Name "[$schemaName].[$tableName]" -ColumnMap $map
$result.Name | Should -Be $tableName
$result.Schema | Should -Be $schemaName
}
It "Parses schema and table from a two-part unquoted name" {
$random = Get-Random
$tableName = "table_unquoted_$random"
$schemaName = "schema_unquoted_$random"
$result = New-DbaDbTable -SqlInstance $TestConfig.InstanceMulti1 -Database $dbname -Name "$schemaName.$tableName" -ColumnMap $map
$result.Name | Should -Be $tableName
$result.Schema | Should -Be $schemaName
}
}
Context "Should create graph tables with IsNode and IsEdge switches" {
BeforeAll {
$server = Connect-DbaInstance -SqlInstance $TestConfig.InstanceMulti2
Expand Down
Loading