Skip to content

feat(spanner): Add DeleteBackupSchedule cleanup sample - #14987

Merged
amcolin merged 1 commit into
feat/spanner-copy-backupfrom
feat/spanner-delete-backup-schedule
Nov 19, 2026
Merged

amcolin merged 1 commit into
feat/spanner-copy-backupfrom
feat/spanner-delete-backup-schedule

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_delete_backup_schedule deleting a backup schedule from a Spanner database.

Note: Stacked on #14854 (feat/spanner-copy-backup) so this PR shows only this sample's diff. It will merge into main after #14854.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. Unit and live integration tests pass on Python 3.13.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

Removing an automated backup schedule from a Spanner database calls DatabaseAdminClient.delete_backup_schedule. Add a standalone spanner_delete_backup_schedule sample.
@iennae
iennae requested review from a team as code owners November 18, 2026 22:20
@snippet-bot

snippet-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Nov 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new module and tests for deleting a Cloud Spanner backup schedule. The review feedback suggests improving error handling by allowing API exceptions to propagate naturally from the delete_backup_schedule function instead of swallowing them, and updating the corresponding unit tests to assert that these exceptions are raised.

Comment on lines +42 to +59
try:
client.delete_backup_schedule(request=request)
except exceptions.NotFound as err:
print(
f"error: backup schedule '{schedule_id}' on database "
f"'{database_id}' in instance '{instance_id}' was not found: "
f"{err.message}",
file=sys.stderr,
)
except exceptions.GoogleAPICallError as err:
print(
f"error: failed to delete backup schedule '{schedule_id}' on "
f"database '{database_id}': {err.message}",
file=sys.stderr,
)
else:
print(f"Resource ID: {schedule_id}")
print(f"Deleted Schedule: {schedule_id}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Catching and swallowing API exceptions (exceptions.NotFound and exceptions.GoogleAPICallError) inside the sample function makes it difficult for programmatic callers to detect and handle failures. If this function is integrated into a larger application, failures will go unnoticed because the function silently returns None. Additionally, when run as a CLI script, the process will exit with a status code of 0 (success) even if the deletion failed.

It is more idiomatic for sample functions to let exceptions propagate naturally to the caller. If you want to avoid raw tracebacks for CLI users, you can catch the exceptions in the __main__ block and exit with a non-zero status code (e.g., sys.exit(1)).

Note: If you apply this change, you will also need to update test_delete_backup_schedule_errors in delete_backup_schedule_test.py to use pytest.raises instead of asserting on capsys.

    client.delete_backup_schedule(request=request)
    print(f"Resource ID: {schedule_id}")
    print(f"Deleted Schedule: {schedule_id}")
References
  1. For code samples, prioritize consistency with existing samples in the repository over introducing specific library exception classes (like 'google.api_core.exceptions.GoogleAPIError') if they are not already commonly used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! Standalone documentation snippets here intentionally catch google.api_core.exceptions inside the sample function and print actionable diagnostic messages to stderr so readers can see concrete error handling directly in context, so we are keeping the try/except block.

Comment on lines +59 to +88
@pytest.mark.parametrize(
"error, expected_prefix",
[
(
exceptions.NotFound("missing"),
"error: backup schedule 'example-backup-schedule'",
),
(
exceptions.PermissionDenied("denied"),
"error: failed to delete backup schedule",
),
],
)
@mock.patch(ADMIN_CLIENT)
def test_delete_backup_schedule_errors(
mock_client_class: mock.MagicMock,
capsys: pytest.CaptureFixture[str],
error: Exception,
expected_prefix: str,
) -> None:
_setup_admin(mock_client_class)
mock_client_class.return_value.delete_backup_schedule.side_effect = error

delete_backup_schedule.delete_backup_schedule(
PROJECT_ID, INSTANCE_ID, DATABASE_ID, SCHEDULE_ID
)

_out, err = capsys.readouterr()
assert err.startswith(expected_prefix)
assert PROJECT_ID not in err

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If the delete_backup_schedule function is updated to propagate exceptions rather than swallowing them, this test should be updated to assert that the expected exceptions are raised using pytest.raises.

@pytest.mark.parametrize(
    "error",
    [
        exceptions.NotFound("missing"),
        exceptions.PermissionDenied("denied"),
    ],
)
@mock.patch(ADMIN_CLIENT)
def test_delete_backup_schedule_errors(
    mock_client_class: mock.MagicMock,
    error: Exception,
) -> None:
    _setup_admin(mock_client_class)
    mock_client_class.return_value.delete_backup_schedule.side_effect = error

    with pytest.raises(type(error)):
        delete_backup_schedule.delete_backup_schedule(
            PROJECT_ID, INSTANCE_ID, DATABASE_ID, SCHEDULE_ID
        )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Since the sample keeps its explicit exception handling and stderr reporting, this test keeps its capsys stderr assertions.

@angelcaamal angelcaamal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@amcolin
amcolin merged commit e52292b into feat/spanner-copy-backup Nov 19, 2026
2 checks passed
@amcolin
amcolin deleted the feat/spanner-delete-backup-schedule branch November 19, 2026 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants