Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New-DbaDbTable - Drop table if creation fails and change DefaultEx to DefaultExpression #8150

Merged
merged 8 commits into from
Feb 7, 2022
Next Next commit
Drop table if creation fails
  • Loading branch information
andreasjordan committed Feb 6, 2022
commit 4be1272ceeae399a78c8b17cb16766199df6b1b5
32 changes: 26 additions & 6 deletions functions/New-DbaDbTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,14 @@ function New-DbaDbTable {

foreach ($db in $InputObject) {
$server = $db.Parent
if ($Pscmdlet.ShouldProcess("Creating new object $name in $db on $server")) {
if ($Pscmdlet.ShouldProcess("Creating new table [$Schema].[$Name] in $db on $server")) {
# Test if table already exists. This ways we can drop the table if part of the creation fails.
$existingTable = $db.tables | Where-Object { $_.Schema -eq $Schema -and $_.Name -eq $Name }
if ($existingTable) {
Stop-Function -Message "Table [$Schema].[$Name] already exists in $db on $server" -Continue
}
try {
$object = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Table $db, $name, $schema
$object = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Table $db, $Name, $Schema
$properties = $PSBoundParameters | Where-Object Key -notin 'SqlInstance', 'SqlCredential', 'Name', 'Schema', 'ColumnMap', 'ColumnObject', 'InputObject', 'EnableException', 'Passthru'

foreach ($prop in $properties.Key) {
Expand Down Expand Up @@ -475,8 +480,10 @@ function New-DbaDbTable {
}

# user has specified a schema that does not exist yet
if (-not ($db | Get-DbaDbSchema -Schema $schema -IncludeSystemSchemas)) {
$schemaObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Schema $db, $schema
$schemaObject = $null
if (-not ($db | Get-DbaDbSchema -Schema $Schema -IncludeSystemSchemas)) {
Write-Message -Level Verbose -Message "Schema $Schema does not exist in $db and will be created."
$schemaObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Schema $db, $Schema
}

if ($Passthru) {
Expand All @@ -495,9 +502,22 @@ function New-DbaDbTable {
}
$null = Invoke-Create -Object $object
}
$db | Get-DbaDbTable -Table "[$schema].[$Name]"
$db | Get-DbaDbTable -Table "[$Schema].[$Name]"
} catch {
Stop-Function -Message "Failure" -ErrorRecord $_ -Continue
$exception = $_
Write-Message -Level Warning -Message "Failed to create table or failure while adding constraints. Will try to remove table."
try {
# If creation of a constraint fails, the table is already created - so it has to be droped.
# But $object.Drop() or $object.DropIfExists() don't work as expected.
# Maybe try again in a later version of the SMO...
$db.Tables | Where-Object { $_.Schema -eq $Schema -and $_.Name -eq $Name } | ForEach-Object { $_.Drop() }
if ($schemaObject) {
$db.Schemas[$Schema].Drop()
}
} catch {
Write-Message -Level Warning -Message "Failed to drop table: $_. Maybe table still exists."
}
Stop-Function -Message "Failure" -ErrorRecord $exception -Continue
}
}
}
Expand Down