Skip to content

Commit

Permalink
fix: Include warnings for restore failures (#266)
Browse files Browse the repository at this point in the history
* Update restore.ps1

* Update restore.ps1

* Update main.yml

* Update main.yml

* chore: add release info

* Update CHANGELOG.rst
  • Loading branch information
lowlydba authored Oct 6, 2024
1 parent 20b7897 commit d75de03
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ lowlydba.sqlserver Release Notes

.. contents:: Topics

v2.3.4
======

Release Summary
---------------

Minor bugfix for failed database restores.

Bugfixes
--------

- Include warning logs in failure output for the restore module to indicate root causes (https://github.com/lowlydba/lowlydba.sqlserver/pull/266).

v2.3.3
======

Expand Down
9 changes: 9 additions & 0 deletions changelogs/changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,12 @@ releases:
- 2-3-3-release-summary.yml
- 245-ag_listener-ip_address-fix.yml
release_date: '2024-06-06'
2.3.4:
changes:
bugfixes:
- Include warning logs in failure output for the restore module to indicate
root causes (https://github.com/lowlydba/lowlydba.sqlserver/pull/266).
release_summary: Minor bugfix for failed database restores.
fragments:
- 266-restore-warnings.yaml
release_date: '2024-10-06'
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace: lowlydba
name: sqlserver
version: 2.3.3
version: 2.3.4
readme: README.md
authors:
- John McCall (github.com/lowlydba)
Expand Down
9 changes: 7 additions & 2 deletions plugins/modules/restore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ try {
if ($null -ne $keepCDC) {
$restoreSplat.Add("KeepCDC", $keepCDC)
}
$output = Restore-DbaDatabase @restoreSplat
$output = Restore-DbaDatabase @restoreSplat -WarningVariable warnings

if ($null -ne $output) {
$resultData = ConvertTo-SerializableObject -InputObject $output
Expand All @@ -166,5 +166,10 @@ try {
$module.ExitJson()
}
catch {
$module.FailJson("Error restoring database: $($_.Exception.Message).", $_)
# Restore command hides relevant error info as warnings, so append warning logs to any failures
$warningMessage = ""
if ($warnings) {
$warningMessage = " Additional warnings: $warnings."
}
$module.FailJson("Error restoring database: $($_.Exception.Message).$warningMessage", $_)
}
20 changes: 20 additions & 0 deletions tests/integration/targets/win_restore/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,23 @@
that:
- result.data.SqlInstance != None
- result.data.Database == restore_database

- name: Test error when restoring to an existing database
lowlydba.sqlserver.restore:
sql_instance: "{{ sqlserver_instance }}"
sql_username: "{{ sqlserver_username }}"
sql_password: "{{ sqlserver_password }}"
database: "{{ restore_database }}"
path: "{{ backup_result.data.BackupPath }}"
replace_db_name_in_file: true
block_size: "16kb"
destination_file_suffix: "_new"
destination_file_prefix: "db_"
register: error_result
failed_when: error_result.failed
ignore_errors: true
- assert:
that:
- error_result.failed == true
- "'already exists' in error_result.msg"
- error_result.msg != None

0 comments on commit d75de03

Please sign in to comment.