Skip to content

Commit

Permalink
Remove-DbaDatabase - Remove IncludeSystemDb parameter (dataplat#5561)
Browse files Browse the repository at this point in the history
  • Loading branch information
sqllensman authored and wsmelton committed May 20, 2019
1 parent 5df3463 commit 28d580f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
18 changes: 6 additions & 12 deletions functions/Remove-DbaDatabase.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function Remove-DbaDatabase {
<#
.SYNOPSIS
Drops a database, hopefully even the really stuck ones.
Drops a user database, hopefully even the really stuck ones.
.DESCRIPTION
Tries a bunch of different ways to remove a database or two or more.
Tries a bunch of different ways to remove a user created database or two or more.
.PARAMETER SqlInstance
The target SQL Server instance or instances.You must have sysadmin access and server version must be SQL Server version 2000 or higher.
Expand All @@ -18,9 +18,6 @@ function Remove-DbaDatabase {
.PARAMETER InputObject
A collection of databases (such as returned by Get-DbaDatabase), to be removed.
.PARAMETER IncludeSystemDb
Use this switch to disable any kind of verbose messages
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
Expand Down Expand Up @@ -59,12 +56,12 @@ function Remove-DbaDatabase {
Does not prompt and swiftly removes containeddb on SQL Server sql2016
.EXAMPLE
PS C:\> Get-DbaDatabase -SqlInstance server\instance -ExcludeSystem | Remove-DbaDatabase
PS C:\> Get-DbaDatabase -SqlInstance server\instance | Remove-DbaDatabase
Removes all the user databases from server\instance
.EXAMPLE
PS C:\> Get-DbaDatabase -SqlInstance server\instance -ExcludeSystem | Remove-DbaDatabase -Confirm:$false
PS C:\> Get-DbaDatabase -SqlInstance server\instance | Remove-DbaDatabase -Confirm:$false
Removes all the user databases from server\instance without any confirmation
#>
Expand All @@ -81,7 +78,6 @@ function Remove-DbaDatabase {
[object[]]$Database,
[Parameter(ValueFromPipeline, Mandatory, ParameterSetName = "databases")]
[Microsoft.SqlServer.Management.Smo.Database[]]$InputObject,
[switch]$IncludeSystemDb,
[Alias('Silent')]
[switch]$EnableException
)
Expand All @@ -97,11 +93,9 @@ function Remove-DbaDatabase {
$InputObject += $server.Databases | Where-Object { $_.Name -in $Database }
}

# Excludes system databases as these cannot be deleted
$system_dbs = @( "master", "model", "tempdb", "resource", "msdb" )

if (-not($IncludeSystemDb)) {
$InputObject = $InputObject | Where-Object { $_.Name -notin $system_dbs}
}
$InputObject = $InputObject | Where-Object { $_.Name -notin $system_dbs}

foreach ($db in $InputObject) {
try {
Expand Down
4 changes: 2 additions & 2 deletions tests/Remove-DbaDatabase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'SqlInstance','SqlCredential','Database','InputObject','IncludeSystemDb','EnableException'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'InputObject', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Expand All @@ -14,7 +14,7 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
}

Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
Context "Should not munge system databases unless explicitly told to." {
Context "Should not munge system databases." {

$dbs = @( "master", "model", "tempdb", "msdb" )

Expand Down

0 comments on commit 28d580f

Please sign in to comment.