Skip to content

Commit

Permalink
Make Backup-DbaDatabase use Database and ExcludeDatabase when piping (#…
Browse files Browse the repository at this point in the history
…5069)

* fixes #5044

* fix one test

* in instead of contains
  • Loading branch information
potatoqualitee authored Feb 25, 2019
1 parent b096f15 commit e36b397
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
19 changes: 14 additions & 5 deletions functions/Backup-DbaDatabase.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,19 @@ function Backup-DbaDatabase {
Write-Message -Message 'Setting Default timestampformat' -Level Verbose
$TimeStampFormat = "yyyyMMddHHmm"
}
if ($SqlInstance.length -ne 0) {

if ($SqlInstance) {
try {
$Server = Connect-SqlInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential -AzureUnsupported
} catch {
Stop-Function -Message "Cannot connect to $SqlInstance" -ErrorRecord $_
return
}

$InputObject = $server.Databases | Where-Object Name -ne 'tempdb'

if ($Database) {
$InputObject = $server.Databases | Where-Object Name -in $Database
} else {
$InputObject = $server.Databases | Where-Object Name -ne 'tempdb'
$InputObject = $InputObject | Where-Object Name -in $Database
}

if ($ExcludeDatabase) {
Expand Down Expand Up @@ -280,13 +281,21 @@ function Backup-DbaDatabase {
}

process {
if (!$SqlInstance -and !$InputObject) {
if (-not $SqlInstance -and -not $InputObject) {
Stop-Function -Message "You must specify a server and database or pipe some databases"
return
}

Write-Message -Level Verbose -Message "$($InputObject.Count) database to backup"

if ($Database) {
$InputObject = $InputObject | Where-Object Name -in $Database
}

if ($ExcludeDatabase) {
$InputObject = $InputObject | Where-Object Name -notin $ExcludeDatabase
}

foreach ($db in $InputObject) {
$ProgressId = Get-Random
$failures = @()
Expand Down
22 changes: 21 additions & 1 deletion tests/Backup-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','ExcludeDatabase','BackupDirectory','BackupFileName','ReplaceInName','CopyOnly','Type','InputObject','CreateFolder','FileCount','CompressBackup','Checksum','Verify','MaxTransferSize','BlockSize','BufferCount','AzureBaseUrl','AzureCredential','NoRecovery','BuildPath','WithFormat','Initialize','SkipTapeHeader','TimeStampFormat','IgnoreFileChecks','OutputScriptOnly','EnableException'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', 'BackupDirectory', 'BackupFileName', 'ReplaceInName', 'CopyOnly', 'Type', 'InputObject', 'CreateFolder', 'FileCount', 'CompressBackup', 'Checksum', 'Verify', 'MaxTransferSize', 'BlockSize', 'BufferCount', 'AzureBaseUrl', 'AzureCredential', 'NoRecovery', 'BuildPath', 'WithFormat', 'Initialize', 'SkipTapeHeader', 'TimeStampFormat', 'IgnoreFileChecks', 'OutputScriptOnly', '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 Down Expand Up @@ -75,6 +75,26 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
}
}

Context "Database parameter works when using pipes (fixes #5044)" {
$results = Get-DbaDatabase -SqlInstance $script:instance1 | Backup-DbaDatabase -Database master -BackupFileName PesterTest.bak -BackupDirectory $DestBackupDir
It "Should report it has backed up to the path with the correct name" {
$results.Fullname | Should -BeLike "$DestBackupDir*PesterTest.bak"
}
It "Should have backed up to the path with the correct name" {
Test-Path "$DestBackupDir\PesterTest.bak" | Should -Be $true
}
}

Context "ExcludeDatabase parameter works when using pipes (fixes #5044)" {
$results = Get-DbaDatabase -SqlInstance $script:instance1 | Backup-DbaDatabase -ExcludeDatabase master, tempdb, msdb, model
It "Should report it has backed up to the path with the correct name" {
$results.DatabaseName | Should -Not -Contain master
$results.DatabaseName | Should -Not -Contain tempdb
$results.DatabaseName | Should -Not -Contain msdb
$results.DatabaseName | Should -Not -Contain model
}
}

Context "Handling backup paths that don't exist" {
$MissingPathTrailing = "$DestBackupDir\Missing1\Awol2\"
$MissingPath = "$DestBackupDir\Missing1\Awol2"
Expand Down

0 comments on commit e36b397

Please sign in to comment.