Skip to content

Commit

Permalink
Test-DBALsnChain - Fix missing log backup (#7515) (#9467)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamJKoehler authored Oct 5, 2024
1 parent 90ec329 commit bf8423c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
45 changes: 24 additions & 21 deletions private/functions/Test-DbaLsnChain.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function Test-DbaLsnChain {
}
$DiffAnchor = $TestHistory | Where-Object { $_.$TypeName -in ('Database Differential', 'Differential') }
#Check for no more than a single Differential backup
if (($DiffAnchor.FirstLSN | Select-Object -unique | Measure-Object).count -gt 1) {
if (($DiffAnchor.FirstLSN | Select-Object -Unique | Measure-Object).count -gt 1) {
Write-Message -Level Warning -Message "More than 1 differential backup, not supported"
return $false
break;
Expand All @@ -96,27 +96,30 @@ function Test-DbaLsnChain {


#Check T-log LSNs form a chain.
$TranLogBackups = $TestHistory | Where-Object { $_.$TypeName -in ('Transaction Log', 'Log') -and $_.DatabaseBackupLSN -eq $FullDBAnchor.CheckPointLSN } | Sort-Object -Property LastLSN, FirstLsn
for ($i = 0; $i -lt ($TranLogBackups.count)) {
Write-Message -Level Debug -Message "looping t logs"
if ($i -eq 0) {
if ($TranLogBackups[$i].FirstLSN -gt $TlogAnchor.LastLSN) {
Write-Message -Level Warning -Message "Break in LSN Chain between $($TlogAnchor.FullName) and $($TranLogBackups[($i)].FullName) "
Write-Message -Level Verbose -Message "Anchor $($TlogAnchor.LastLSN) - FirstLSN $($TranLogBackups[$i].FirstLSN)"
return $false
break
}
} else {
if ($TranLogBackups[($i - 1)].LastLsn -ne $TranLogBackups[($i)].FirstLSN -and ($TranLogBackups[($i)] -ne $TranLogBackups[($i - 1)])) {
Write-Message -Level Warning -Message "Break in transaction log between $($TranLogBackups[($i-1)].FullName) and $($TranLogBackups[($i)].FullName) "
return $false
break
}
}
$i++
$TranLogBackups = $TestHistory | Where-Object {
$_.$TypeName -in ('Transaction Log', 'Log') -and (($_.DatabaseBackupLSN.ToString() -eq $FullDBAnchor.CheckPointLSN) -or (($_.DatabaseBackupLSN.ToString() -ne $FullDBAnchor.CheckPointLSN) -and ($TranLogBackups[$i].FirstLSN -gt $FullDBAnchor.CheckPointLSN)))
} | Sort-Object -Property LastLSN, FirstLsn

for ($i = 0; $i -lt ($TranLogBackups.count)) {
Write-Message -Level Debug -Message "looping t logs"
if ($i -eq 0) {
if ($TranLogBackups[$i].FirstLSN.ToString() -gt $TlogAnchor.LastLSN) {
Write-Message -Level Warning -Message "Break in LSN Chain between $($TlogAnchor.FullName) and $($TranLogBackups[($i)].FullName) "
Write-Message -Level Verbose -Message "Anchor $($TlogAnchor.LastLSN) - FirstLSN $($TranLogBackups[$i].FirstLSN)"
return $false
break
}
} else {
if ($TranLogBackups[($i - 1)].LastLsn -ne $TranLogBackups[($i)].FirstLSN -and ($TranLogBackups[($i)] -ne $TranLogBackups[($i - 1)])) {
Write-Message -Level Warning -Message "Break in transaction log between $($TranLogBackups[($i-1)].FullName) and $($TranLogBackups[($i)].FullName) "
return $false
break
}
}
Write-Message -Level VeryVerbose -Message "Passed LSN Chain checks"
return $true
$i++

}
Write-Message -Level VeryVerbose -Message "Passed LSN Chain checks"
return $true
}
}
4 changes: 2 additions & 2 deletions tests/Test-DbaBackupInformation.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Describe "$commandname Integration Tests" -Tag 'IntegrationTests' {
It "Should pass as all systems Green" {
$output = $BackupHistory | Test-DbaBackupInformation -SqlInstance NotExist -WarningVariable warnvar -WarningAction SilentlyContinue
($output.Count) -gt 0 | Should be $true
$false -in ($Output.IsVerified) | Should be $False
"False" -in ($Output.IsVerified) | Should be $False
($null -ne $WarnVar) | Should be $True
}
}
Expand Down Expand Up @@ -250,4 +250,4 @@ Describe "$commandname Integration Tests" -Tag 'IntegrationTests' {
}
}
}
}
}
6 changes: 3 additions & 3 deletions tests/Test-DbaLsnChain.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ 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[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
[object[]]$knownParameters = 'FilteredRestoreFiles', 'Continue', '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
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0
}
}
}
Expand All @@ -22,7 +22,7 @@ Describe "$commandname Integration Tests" -Tag 'IntegrationTests' {

$filteredFiles = $header | Select-DbaBackupInformation
It "Should Return 7" {
$FilteredFiles.count | should be 7
$FilteredFiles.count | Should be 7
}
It "Should return True" {
$Output = Test-DbaLsnChain -FilteredRestoreFiles $FilteredFiles -WarningAction SilentlyContinue
Expand Down

0 comments on commit bf8423c

Please sign in to comment.