Skip to content

Testing Backup-DbaDatabase - Restore model after the all-databases backup test - #10582

Open
andreasjordan wants to merge 1 commit into
developmentfrom
fix-backup-dbadatabase-model-log-chain
Open

Testing Backup-DbaDatabase - Restore model after the all-databases backup test#10582
andreasjordan wants to merge 1 commit into
developmentfrom
fix-backup-dbadatabase-model-log-chain

Conversation

@andreasjordan

Copy link
Copy Markdown
Collaborator

The context "Properly backups all databases" backs up every database of InstanceCopy1, so it also takes a full backup of model. As model is in the full recovery model, that backup starts its log chain, and because no log backup ever follows, the log can never be reused and grows a little on every run of this file.

That growth is in the file size, so a restart does not undo it. It surfaces much later as a completely unrelated failure: a new database's log is never smaller than model's, so New-DbaDatabase.Tests.ps1 fails its log size assertion once model's log passes 32 MB. In our lab model had reached 72 MB and the failure looked like this, on an instance where the other assertions of the same test passed:

New-DbaDatabase.When creating databases.creates one new database on two servers
  $instance3.Databases[$newDbName].LogFiles["$($newDbName)_log"].Size | Should -Be 32768
  Expected 32768, but got 73728.

InstanceCopy1 and InstanceMulti2 are the same instance in our configuration, which is why this file damages that one.

The fix

The context's BeforeAll now records model's recovery model and log size before the backup, and a new AfterAll restores both.

Switching to the simple recovery model is what makes the log reusable again. Without it the shrink cannot release anything, because the log still waits for a log backup, and the file even grows instead - a plain DBCC SHRINKFILE took our model log from 72 MB to 136 MB. DBCC SHRINKFILE also resolves the logical file name in the current database, so it has to run in model while the two ALTER DATABASE statements run from master.

The original recovery model is restored rather than a hardcoded FULL, and the whole block is skipped when model is already in the simple recovery model, so there is nothing to break.

This also removes a TODO that is answered: the master key it asks about is created on every lab instance at startup.

Verification

  • Backup-DbaDatabase.Tests.ps1 passes unchanged: 48 passed, 2 skipped, 0 failed, the same counts as before.
  • model is left exactly as found: full recovery model, log_reuse_wait_desc NOTHING, log at its original 8 MB, and last_log_backup_lsn back to NULL, so the chain is really closed and not just the file shrunk.
  • New-DbaDatabase.Tests.ps1 passes 13/13 again once model is repaired.

This text was created by Claude and reviewed by Andreas Jordan.

馃 Generated with Claude Code

The context "Properly backups all databases" backs up every database of
InstanceCopy1, so it also takes a full backup of model. As model is in the
full recovery model, that backup starts its log chain, and because no log
backup ever follows, the log can never be reused and grows on every run of
this file. The growth is in the file size, so a restart does not undo it,
and once the log passes 32 MB it breaks New-DbaDatabase.Tests.ps1, which
asserts the exact log size of a new database on the same instance.

The new AfterAll records the recovery model and log size before the backup
and restores both afterwards. Switching to the simple recovery model is what
makes the log reusable again - without it the shrink cannot release anything
and the file even grows instead.

Also removes a TODO that is answered: 06_configure_instances.ps1 creates the
master key on every lab instance at startup.

(do Backup-DbaDatabase)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@andreasjordan andreasjordan changed the title Backup-DbaDatabase - Restore model after the all-databases backup test Testing Backup-DbaDatabase - Restore model after the all-databases backup test Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant