Skip to content

Commit

Permalink
Remove-DbaDbCheckConstraint - new command (#8132)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeyBronowski committed Feb 4, 2022
1 parent 89b8aa0 commit 08179f2
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
124 changes: 124 additions & 0 deletions functions/Remove-DbaDbCheckConstraint.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
function Remove-DbaDbCheckConstraint {
<#
.SYNOPSIS
Removes a database check constraint(s) from each database and SQL Server instance.
.DESCRIPTION
Removes a database check constraint(s), with supported piping from Get-DbaDbCheckConstraint.
.PARAMETER SqlInstance
The target SQL Server instance or instances.
.PARAMETER SqlCredential
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.
.PARAMETER Database
The target database(s).
.PARAMETER ExcludeDatabase
The database(s) to exclude - this list is auto populated from the server.
.PARAMETER ExcludeSystemTable
This switch removes all system tables from the check collection.
.PARAMETER InputObject
Allows piping from Get-DbaDbCheckConstraint.
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
This is the default. Use -Confirm:$false to suppress these prompts.
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
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.
.NOTES
Tags: Check, Constraint, Database
Author: Mikey Bronowski (@MikeyBronowski), https://bronowski.it
Website: https://dbatools.io
Copyright: (c) 2022 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Remove-DbaDbCheckConstraint
.EXAMPLE
PS C:\> Remove-DbaDbCheckConstraint -SqlInstance localhost, sql2016 -Database db1, db2
Removes all check constraints from db1 and db2 on the local and sql2016 SQL Server instances.
.EXAMPLE
PS C:\> $chkcs = Get-DbaDbCheckConstraint -SqlInstance localhost, sql2016 -Database db1, db2
PS C:\> $chkcs | Remove-DbaDbCheckConstraint
Removes all check constraints from db1 and db2 on the local and sql2016 SQL Server instances.
.EXAMPLE
PS C:\> Remove-DbaDbCheckConstraint -SqlInstance localhost, sql2016 -Database db1, db2 -ExcludeSystemTable
Removes all check constraints except those in system tables from db1 and db2 on the local and sql2016 SQL Server instances.
#>
[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'Default', ConfirmImpact = 'High')]
param (
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[object[]]$Database,
[object[]]$ExcludeDatabase,
[switch]$ExcludeSystemTable,
[parameter(ValueFromPipeline, ParameterSetName = 'Pipeline', Mandatory = $true)]
[Microsoft.SqlServer.Management.Smo.Check[]]$InputObject,
[Parameter(ParameterSetName = 'Pipeline')]
[switch]$EnableException
)

begin {
$chkcs = @( )
}

process {
if ($SqlInstance) {
$params = $PSBoundParameters
$null = $params.Remove('WhatIf')
$null = $params.Remove('Confirm')
$chkcs = Get-DbaDbCheckConstraint @params
} else {
$chkcs += $InputObject
}
}

end {
# We have to delete in the end block to prevent "Collection was modified; enumeration operation may not execute." if directly piped from Get-DbaDbUdf.
foreach ($chkcItem in $chkcs) {
if ($PSCmdlet.ShouldProcess($chkcItem.Parent.Parent.Parent.Name, "Removing the check constraint [$($chkcItem.Name)] on the table $($chkcItem.Parent) on the database [$($chkcItem.Parent.Parent.Name)]")) {
$output = [pscustomobject]@{
ComputerName = $chkcItem.ComputerName
InstanceName = $chkcItem.Parent.Parent.Parent.ServiceName
SqlInstance = $chkcItem.Parent.Parent.Parent.DomainInstanceName
Database = $chkcItem.Parent.Name
Name = $chkcItem.Name
Status = $null
IsRemoved = $false
}
try {
$chkcItem.Drop()
$output.Status = "Dropped"
$output.IsRemoved = $true
} catch {
Stop-Function -Message "Failed removing the check constraint $($chkcItem.Schema).$($chkcItem.Name) in the database $($chkcItem.Parent.Name) on $($chkcItem.Parent.Parent.Name)" -ErrorRecord $_
$output.Status = (Get-ErrorMessage -Record $_)
$output.IsRemoved = $false
}
$output
}
}
}
}
49 changes: 49 additions & 0 deletions tests/Remove-DbaDbCheckConstraint.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[array]$params = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($CommandName, 'Function')).Parameters.Keys
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', 'ExcludeSystemTable', 'InputObject', 'EnableException'
It "Should only contain our specific parameters" {
Compare-Object -ReferenceObject $knownParameters -DifferenceObject $params | Should -BeNullOrEmpty
}
}
}

Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {

BeforeAll {

$server = Connect-DbaInstance -SqlInstance $script:instance2
$dbname1 = "dbatoolsci_$(Get-Random)"
$dbname2 = "dbatoolsci_$(Get-Random)"
$null = New-DbaDatabase -SqlInstance $server -Name $dbname1
$null = New-DbaDatabase -SqlInstance $server -Name $dbname2

$chkc1 = "dbatoolssci_chkc1_$(Get-Random)"
$chkc2 = "dbatoolssci_chkc2_$(Get-Random)"
$null = $server.Query("CREATE TABLE dbo.checkconstraint1(col int CONSTRAINT $chkc1 CHECK(col > 0));" , $dbname1)
$null = $server.Query("CREATE TABLE dbo.checkconstraint2(col int CONSTRAINT $chkc2 CHECK(col > 0));" , $dbname2)
}

AfterAll {
$null = Remove-DbaDatabase -SqlInstance $server -Database $dbname1, $dbname2 -Confirm:$false
}

Context "commands work as expected" {

It "removes an check constraint" {
Get-DbaDbCheckConstraint -SqlInstance $server -Database $dbname1 | Should -Not -BeNullOrEmpty
Remove-DbaDbCheckConstraint -SqlInstance $server -Database $dbname1 -Confirm:$false
Get-DbaDbCheckConstraint -SqlInstance $server -Database $dbname1 | Should -BeNullOrEmpty
}

It "supports piping check constraint" {
Get-DbaDbCheckConstraint -SqlInstance $server -Database $dbname2 | Should -Not -BeNullOrEmpty
Get-DbaDbCheckConstraint -SqlInstance $server -Database $dbname2 | Remove-DbaDbCheckConstraint -Confirm:$false
Get-DbaDbCheckConstraint -SqlInstance $server -Database $dbname2 | Should -BeNullOrEmpty
}
}
}

0 comments on commit 08179f2

Please sign in to comment.