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
7 changes: 6 additions & 1 deletion public/Format-DbaBackupInformation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ function Format-DbaBackupInformation {
}
$History.Database = $DatabaseNamePrefix + $History.Database

# Capture rename values before entering the ForEach-Object pipeline to ensure correct
# scoping and to use [regex]::Escape so database names with special chars are treated literally
$originalDb = $History.OriginalDatabase
$newDb = $History.Database

$History.FileList | ForEach-Object {
if ($null -ne $FileMapping ) {
if ($null -ne $FileMapping[$_.LogicalName]) {
Expand All @@ -240,7 +245,7 @@ function Format-DbaBackupInformation {
$baseName = $baseName.Split($PathSep)[-1]

if ($ReplaceDbNameInFile -eq $true) {
$baseName = $baseName -Replace $History.OriginalDatabase, $History.Database
$baseName = $baseName -Replace ([regex]::Escape($originalDb)), $newDb
}

# Determine restore directory based on file type
Expand Down
20 changes: 20 additions & 0 deletions tests/Format-DbaBackupInformation.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ Describe $CommandName -Tag IntegrationTests {
}
}

Context "Rename a Database using string ReplaceDatabaseName with ReplaceDbNameInFile renames log files" {
BeforeAll {
$history = Get-DbaBackupInformation -Import -Path $PSScriptRoot\ObjectDefinitions\BackupRestore\RawInput\RestoreTimeClean.xml
$output = Format-DbaBackupInformation -BackupHistory $history -ReplaceDatabaseName 'Pester' -ReplaceDbNameInFile
}

It "Should have a database name of Pester" {
($output | Where-Object { $_.Database -ne 'Pester' }).count | Should -Be 0
}
It "Should have renamed all data files" {
($output | Select-Object -ExpandProperty filelist | Where-Object { $_.Type -eq 'D' } | Where-Object { $_.PhysicalName -like '*RestoreTimeClean*' }).count | Should -Be 0
}
It "Should have renamed all log files" {
($output | Select-Object -ExpandProperty filelist | Where-Object { $_.Type -eq 'L' } | Where-Object { $_.PhysicalName -like '*RestoreTimeClean*' }).count | Should -Be 0
}
It "Log file physical name should contain new database name" {
($output | Select-Object -ExpandProperty filelist | Where-Object { $_.Type -eq 'L' } | Where-Object { $_.PhysicalName -like '*Pester*' }).count | Should -BeGreaterThan 0
}
}

Context "Rename 2 dbs using a hash" {
BeforeAll {
$history = Get-DbaBackupInformation -Import -Path $PSScriptRoot\ObjectDefinitions\BackupRestore\RawInput\ContinuePointTest.xml
Expand Down