Skip to content

Commit 21d8aeb

Browse files
committed
Update Restore-SqlDscDatabase tests to use checksum backups and ensure unique file names for RelocateFile objects
1 parent d27f6d6 commit 21d8aeb

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

tests/Integration/Commands/Restore-SqlDscDatabase.Integration.Tests.ps1

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,23 +279,24 @@ Describe 'Restore-SqlDscDatabase' -Tag @('Integration_SQL2017', 'Integration_SQL
279279
$script:relocateDbName = 'SqlDscRestoreRelocate_' + (Get-Random)
280280
$script:createdDatabases += $script:relocateDbName
281281

282-
# Get the file list from the backup
282+
# Get the file list from the backup to build RelocateFile objects with unique names
283283
$fileList = Get-SqlDscBackupFileList -ServerObject $script:serverObject -BackupFile $script:fullBackupFile
284284

285-
# Create RelocateFile objects
286285
$script:relocateFiles = @()
287286

288287
foreach ($file in $fileList)
289288
{
290-
$originalFileName = [System.IO.Path]::GetFileName($file.PhysicalName)
291-
$newFileName = $script:relocateDbName + '_' + $originalFileName
289+
# Generate unique filename based on the target database name to avoid conflicts
290+
$fileExtension = [System.IO.Path]::GetExtension($file.PhysicalName)
292291

293292
if ($file.Type -eq 'L')
294293
{
294+
$newFileName = $script:relocateDbName + '_log' + $fileExtension
295295
$newPath = Join-Path -Path $script:logDirectory -ChildPath $newFileName
296296
}
297297
else
298298
{
299+
$newFileName = $script:relocateDbName + $fileExtension
299300
$newPath = Join-Path -Path $script:dataDirectory -ChildPath $newFileName
300301
}
301302

@@ -375,8 +376,12 @@ Describe 'Restore-SqlDscDatabase' -Tag @('Integration_SQL2017', 'Integration_SQL
375376
$script:checksumDbName = 'SqlDscRestoreChecksum_' + (Get-Random)
376377
$script:createdDatabases += $script:checksumDbName
377378

379+
# Create a backup with checksums for this test
380+
$script:checksumBackupFile = Join-Path -Path $script:backupDirectory -ChildPath ($script:sourceDatabaseName + '_Checksum.bak')
381+
$null = Backup-SqlDscDatabase -ServerObject $script:serverObject -Name $script:sourceDatabaseName -BackupFile $script:checksumBackupFile -Checksum -Force -ErrorAction 'Stop'
382+
378383
# Get the file list from the backup to build RelocateFile objects with unique names
379-
$fileList = Get-SqlDscBackupFileList -ServerObject $script:serverObject -BackupFile $script:fullBackupFile
384+
$fileList = Get-SqlDscBackupFileList -ServerObject $script:serverObject -BackupFile $script:checksumBackupFile
380385

381386
$script:checksumRelocateFiles = @()
382387

@@ -408,10 +413,16 @@ Describe 'Restore-SqlDscDatabase' -Tag @('Integration_SQL2017', 'Integration_SQL
408413
{
409414
$null = Remove-SqlDscDatabase -DatabaseObject $existingDb -Force -ErrorAction 'SilentlyContinue'
410415
}
416+
417+
# Clean up checksum backup file
418+
if (Test-Path -Path $script:checksumBackupFile)
419+
{
420+
Remove-Item -Path $script:checksumBackupFile -Force -ErrorAction 'SilentlyContinue'
421+
}
411422
}
412423

413424
It 'Should restore database with checksum verification' {
414-
$null = Restore-SqlDscDatabase -ServerObject $script:serverObject -Name $script:checksumDbName -BackupFile $script:fullBackupFile -RelocateFile $script:checksumRelocateFiles -Checksum -Force -ErrorAction 'Stop'
425+
$null = Restore-SqlDscDatabase -ServerObject $script:serverObject -Name $script:checksumDbName -BackupFile $script:checksumBackupFile -RelocateFile $script:checksumRelocateFiles -Checksum -Force -ErrorAction 'Stop'
415426

416427
# Verify the database was restored
417428
$restoredDb = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:checksumDbName -ErrorAction 'SilentlyContinue'

0 commit comments

Comments
 (0)