Skip to content

Commit f72d98a

Browse files
Get-DbaDbRestoreHistory - Fix LastRestorePoint fallback logic (review of #10249)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a269116 commit f72d98a

3 files changed

Lines changed: 45 additions & 7 deletions

File tree

docs/trackers/features/commit-bug-review-TRACKER.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Find real bugs (logic errors, null refs, incorrect behavior) and fix them. Skip
7272
| 63c906f9d | Set-DbaDbCompression - Add SortInTempDB parameter and fix views T-SQL bug (#10248) | DONE | Fixed indexed-view output/error metadata to use the view name instead of a stale table reference; added integration regression test. |
7373
| 4d1a9d80c | v2.7.27 | DONE | version bump - skip |
7474
| 232395207 | Set-DbaPrivilege, Get-DbaPrivilege - Add CreateGlobalObjects privilege support (#10235) | DONE | Fixed empty privilege-entry updates so Set-DbaPrivilege still grants rights when secedit exports a blank line; added unit regression test. |
75-
| 099624061 | Get-DbaDbRestoreHistory - Add BackupStartDate, StopAt, and LastRestorePoint columns (#10249) | PENDING | |
75+
| 099624061 | Get-DbaDbRestoreHistory - Add BackupStartDate, StopAt, and LastRestorePoint columns (#10249) | DONE | Fixed LastRestorePoint so StopAt is used whenever specified; added unit regression test. |
7676
| 4f1e56ce4 | New-DbaDbMailAccount, Set-DbaDbMailAccount - Add Port, SSL, and authentication parameters (#10257) | PENDING | |
7777
| 8218d327e | Restore-DbaDatabase, Invoke-DbaAdvancedRestore - Add ErrorBrokerConversations parameter (#10253) | PENDING | |
7878
| 9a4e4bacb | Connect-DbaInstance - Set NonPooledConnection on ServerConnection (#10260) | PENDING | |

public/Get-DbaDbRestoreHistory.ps1

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function Get-DbaDbRestoreHistory {
8686
- BackupStartDate: Timestamp when the backup operation started
8787
- BackupFinishDate: Timestamp when the backup operation completed
8888
- StopAt: The point-in-time stop value specified during the restore operation (NULL if not specified)
89-
- LastRestorePoint: The effective point in time the database was restored to (StopAt if specified and earlier than BackupStartDate, otherwise BackupStartDate)
89+
- LastRestorePoint: The effective point in time the database was restored to (StopAt if specified, otherwise BackupStartDate)
9090
9191
All properties from the underlying DataRow object are accessible using Select-Object *.
9292
@@ -188,10 +188,7 @@ function Get-DbaDbRestoreHistory {
188188
bs.backup_finish_date,
189189
bs.backup_finish_date AS BackupFinishDate,
190190
rsh.stop_at AS StopAt,
191-
CASE
192-
WHEN COALESCE(rsh.stop_at, '9999-12-31') < bs.backup_start_date THEN rsh.stop_at
193-
ELSE bs.backup_start_date
194-
END AS LastRestorePoint
191+
COALESCE(rsh.stop_at, bs.backup_start_date) AS LastRestorePoint
195192
"
196193
}
197194

tests/Get-DbaDbRestoreHistory.Tests.ps1

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
3-
$ModuleName = "dbatools",
3+
$ModuleName = "dbatools",
44
$CommandName = "Get-DbaDbRestoreHistory",
55
$PSDefaultParameterValues = $TestConfig.Defaults
66
)
@@ -27,6 +27,47 @@ Describe $CommandName -Tag UnitTests {
2727

2828
}
2929

30+
Describe $CommandName -Tag UnitTests {
31+
InModuleScope "dbatools" {
32+
BeforeAll {
33+
$script:capturedQuery = $null
34+
$script:mockServer = [PSCustomObject]@{
35+
ComputerName = "sql01"
36+
ServiceName = "MSSQLSERVER"
37+
DomainInstanceName = "sql01"
38+
ConnectionContext = [PSCustomObject]@{ }
39+
}
40+
Add-Member -InputObject $script:mockServer.ConnectionContext -Name ExecuteWithResults -MemberType ScriptMethod -Value {
41+
param($Query)
42+
$script:capturedQuery = $Query
43+
[PSCustomObject]@{
44+
Tables = [PSCustomObject]@{
45+
Rows = @()
46+
}
47+
}
48+
} -Force
49+
50+
Mock Connect-DbaInstance {
51+
$script:mockServer
52+
}
53+
}
54+
55+
Context "LastRestorePoint query generation" {
56+
BeforeEach {
57+
$script:capturedQuery = $null
58+
}
59+
60+
It "uses StopAt whenever it is present" {
61+
$null = Get-DbaDbRestoreHistory -SqlInstance "sql01" -Database "db1"
62+
$normalizedQuery = $script:capturedQuery -replace "\s+", " "
63+
64+
$normalizedQuery | Should -Match "COALESCE\(rsh\.stop_at,\s*bs\.backup_start_date\)\s+AS\s+LastRestorePoint"
65+
$normalizedQuery | Should -Not -Match "COALESCE\(rsh\.stop_at,\s*'9999-12-31'\)\s*<\s*bs\.backup_start_date"
66+
}
67+
}
68+
}
69+
}
70+
3071
Describe $CommandName -Tag IntegrationTests {
3172

3273
BeforeAll {

0 commit comments

Comments
 (0)