Restore-DbaDatabase - Stop escaping the caller and resurrect the StopAt tests - #10636
Open
andreasjordan wants to merge 1 commit into
Open
Restore-DbaDatabase - Stop escaping the caller and resurrect the StopAt tests#10636andreasjordan wants to merge 1 commit into
andreasjordan wants to merge 1 commit into
Conversation
…At tests Two Stop-Function calls in the end block carried -Continue although no loop encloses them. When a restore or its verification failed without -EnableException, the continue unwound out of the command and consumed an iteration of whatever loop the caller was running in - a caller restoring in a foreach silently skipped their next instance, and under Pester it corrupted the test runner, surfacing as "Cannot bind argument to parameter ErrorRecord because it is null". That crash is what kept the StopAt tests undiagnosable since 2020. Both sites now stop and return. The StopAt tests themselves were skipped because their static fixture was broken from its first commit - StopAt_22.trn never made it into the appveyor-lab repo. They now generate their own chain in BeforeAll: two transactions named dbatoolstest carry marks, with a timestamp captured between them, and the assertions follow the layout of the generated steps table instead of magic values. Two traps encoded in comments: STOPATMARK references the transaction name, not the WITH MARK description, and a mark in the last log file recovers the database in the same statement, so no separate -Recover may follow. Verified via the lab harness on SQL03\SQL2019: 81 tests, 77 passed, 0 failed, 3 skipped (the Azure contexts), no warnings. Before the flow-control fix the same file aborted the whole Pester run with the null-ErrorRecord crash. (do Restore-DbaDatabase) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Aug 29, 2026
Stop-Function -Continue without an enclosing loop escapes the command and corrupts the caller
#10638
Open
Open
potatoqualitee
approved these changes
Aug 30, 2026
potatoqualitee
left a comment
Member
There was a problem hiding this comment.
Reviewed the complete current head, both end-block control-flow fixes, generated StopAt chain and assertions, cleanup, and exact-head CI evidence. No material correctness or compatibility issues found.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two fixes that belong together: a flow-control defect in
Restore-DbaDatabasethat has corrupted callers since at least 2020, and the resurrection of the StopAt tests whose failure it made undiagnosable.The command defect
Two
Stop-Functioncalls in theendblock carried-Continuealthough no loop encloses them (the catch aroundTest-DbaBackupInformationand the catch aroundInvoke-DbaAdvancedRestore).Stop-Function -Continueexecutes PowerShell'scontinue, and with no enclosing loop in the function, that flow-control unwinds out of the command into the caller:foreachover instances silently skips their next iteration when one restore fails without-EnableException;Cannot bind argument to parameter 'ErrorRecord' because it is null- the exact crash the old TODO comment above the skipped StopAt tests documented as "reason currently unknown".Both sites now
Stop-Function+return, with comments stating why-Continueis forbidden there. Reproduced before the fix with a plain script: a marker line after the restore call was never reached while thefinallystill ran - the signature of escaped flow control, not of an exception.The StopAt tests
Skipped since the 2025 Pester rewrite because their static fixture (
appveyor-lab\sql2008-backups\StopAt) was broken from its very first 2020 commit -StopAt_22.trnnever existed in git, and regenerating 2008-era files would need a SQL Server 2008 instance. The tests now generate their own chain inBeforeAll: astepstable, two marked transactions nameddbatoolstest(STOPATMARK references the transaction name; theWITH MARKstring is only a description - the first rewrite attempt proved this the hard way), a timestamp captured between the marks forStopAfterDate, and assertions that follow the generated layout instead of the old magic values.Semantics pinned: stop at mark includes the marked transaction (step 2),
-StopBeforeexcludes it (step 1),-StopAfterDatewith a between-marks timestamp selects the second mark (step 4). A mark in the last log file recovers the database in the same statement, so no separate-Recovermay follow it - encoded as a comment.Deliberately not changed
After stopping at a mark mid-chain, the command still attempts the remaining log files, which SQL Server refuses ("log too recent to apply") - with
-EnableExceptiona successful stop-at-mark therefore still throws on the tail file. Whether the restore loop should end gracefully once the stop is reached is a design question worth its own issue.Same file as #10631, different hunks (this end-block region vs. the backup-information splats); the branches merge cleanly in either order.
Tests
Lab harness on SQL03\SQL2019: 81 tests, 77 passed, 0 failed, 3 skipped (Azure contexts), no warnings. Before the flow-control fix, the same file aborted the entire Pester run with the null-ErrorRecord crash - which doubles as the red-on-old proof.
created by Claude and reviewed by Andreas Jordan
🤖 Generated with Claude Code