Repository navigation
feat(spanner): Add DeleteBackup sample waiting on referencing DBs #14980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/spanner-copy-backup
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # [START spanner_delete_backup] | ||
| import sys | ||
| import time | ||
|
|
||
| from google.api_core import exceptions | ||
| from google.cloud import spanner_admin_database_v1 | ||
|
|
||
|
|
||
| def delete_backup(project_id: str, instance_id: str, backup_id: str) -> None: | ||
| """Waits for referencing databases to finish optimizing and deletes a backup. | ||
|
|
||
| Args: | ||
| project_id: The Google Cloud project ID. | ||
| instance_id: The Spanner instance ID. | ||
| backup_id: The backup ID to delete. | ||
| """ | ||
| client = spanner_admin_database_v1.DatabaseAdminClient() | ||
| backup_path = client.backup_path(project_id, instance_id, backup_id) | ||
| get_request = spanner_admin_database_v1.GetBackupRequest(name=backup_path) | ||
| delete_request = spanner_admin_database_v1.DeleteBackupRequest(name=backup_path) | ||
|
|
||
| def wait_until_unreferenced() -> None: | ||
| deadline = time.monotonic() + 1800 | ||
| backup = client.get_backup(request=get_request) | ||
| if backup.referencing_databases: | ||
| print(f"Waiting for backup '{backup_id}' to be unreferenced...") | ||
| while backup.referencing_databases: | ||
| if time.monotonic() > deadline: | ||
| raise TimeoutError("restored databases still reference the backup") | ||
| time.sleep(10) | ||
| backup = client.get_backup(request=get_request) | ||
|
|
||
| try: | ||
| wait_until_unreferenced() | ||
| client.delete_backup(request=delete_request) | ||
| except TimeoutError as err: | ||
| print( | ||
| f"error: timed out waiting to delete backup '{backup_id}' on " | ||
| f"instance '{instance_id}': {err}", | ||
| file=sys.stderr, | ||
| ) | ||
| except exceptions.NotFound as err: | ||
| print( | ||
| f"error: backup '{backup_id}' on instance '{instance_id}' " | ||
| f"was not found: {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| except exceptions.GoogleAPICallError as err: | ||
| print( | ||
| f"error: failed to delete backup '{backup_id}' on instance " | ||
| f"'{instance_id}': {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| else: | ||
| print(f"Resource ID: {backup_id}") | ||
| print(f"Deleted Backup: {backup_id}") | ||
|
|
||
|
|
||
| # [END spanner_delete_backup] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import argparse | ||
| import os | ||
|
|
||
| parser = argparse.ArgumentParser(description="Delete a Spanner database backup.") | ||
| parser.add_argument( | ||
| "--project-id", | ||
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | ||
| ) | ||
| parser.add_argument( | ||
| "--instance-id", | ||
| default=os.environ.get("SPANNER_INSTANCE_ID", "example-instance"), | ||
| ) | ||
| parser.add_argument( | ||
| "--backup-id", | ||
| default=os.environ.get("SPANNER_DISPOSABLE_BACKUP_ID", "example-backup"), | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| delete_backup( | ||
| project_id=args.project_id, | ||
| instance_id=args.instance_id, | ||
| backup_id=args.backup_id, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import runpy | ||
| from unittest import mock | ||
|
|
||
| from google.api_core import exceptions | ||
| import pytest | ||
| from spanner_backups import delete_backup | ||
|
|
||
| from . import conftest | ||
|
|
||
| PROJECT_ID = "example-project-id" | ||
| INSTANCE_ID = "example-instance" | ||
| BACKUP_ID = "example-backup" | ||
| ADMIN_CLIENT = "google.cloud.spanner_admin_database_v1.DatabaseAdminClient" | ||
|
|
||
|
|
||
| def _setup_admin(mock_client_class: mock.MagicMock) -> None: | ||
| client = mock_client_class.return_value | ||
| client.backup_path.return_value = ( | ||
| f"projects/{PROJECT_ID}/instances/{INSTANCE_ID}/backups/{BACKUP_ID}" | ||
| ) | ||
| client.get_backup.return_value.referencing_databases = [] | ||
|
|
||
|
|
||
| @mock.patch(ADMIN_CLIENT) | ||
| def test_delete_backup_success( | ||
| mock_client_class: mock.MagicMock, capsys: pytest.CaptureFixture[str] | ||
| ) -> None: | ||
| _setup_admin(mock_client_class) | ||
|
|
||
| delete_backup.delete_backup(PROJECT_ID, INSTANCE_ID, BACKUP_ID) | ||
|
|
||
| mock_client_class.return_value.delete_backup.assert_called_once() | ||
| out, err = capsys.readouterr() | ||
| assert f"Resource ID: {BACKUP_ID}" in out | ||
| assert f"Deleted Backup: {BACKUP_ID}" in out | ||
| assert PROJECT_ID not in out | ||
| assert err == "" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "error, expected_prefix", | ||
| [ | ||
| (exceptions.NotFound("missing"), "error: backup 'example-backup'"), | ||
| ( | ||
| exceptions.FailedPrecondition("in use"), | ||
| "error: failed to delete backup 'example-backup'", | ||
| ), | ||
| ], | ||
| ) | ||
| @mock.patch(ADMIN_CLIENT) | ||
| def test_delete_backup_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.side_effect = error | ||
|
|
||
| delete_backup.delete_backup(PROJECT_ID, INSTANCE_ID, BACKUP_ID) | ||
|
|
||
| _out, err = capsys.readouterr() | ||
| assert err.startswith(expected_prefix) | ||
| assert PROJECT_ID not in err | ||
|
|
||
|
|
||
| @mock.patch(ADMIN_CLIENT) | ||
| def test_delete_backup_times_out_while_referenced( | ||
| mock_client_class: mock.MagicMock, capsys: pytest.CaptureFixture[str] | ||
| ) -> None: | ||
| _setup_admin(mock_client_class) | ||
| client = mock_client_class.return_value | ||
| client.get_backup.return_value.referencing_databases = ["restored-db"] | ||
|
|
||
| with mock.patch.object(delete_backup, "time") as mock_time: | ||
| mock_time.monotonic.side_effect = [0.0, 2000.0] | ||
| delete_backup.delete_backup(PROJECT_ID, INSTANCE_ID, BACKUP_ID) | ||
|
|
||
| client.delete_backup.assert_not_called() | ||
| out, err = capsys.readouterr() | ||
| assert f"Waiting for backup '{BACKUP_ID}' to be unreferenced..." in out | ||
| assert err.startswith(f"error: timed out waiting to delete backup '{BACKUP_ID}'") | ||
| assert PROJECT_ID not in err | ||
|
|
||
|
|
||
| @mock.patch(ADMIN_CLIENT) | ||
| def test_delete_backup_cli_entrypoint( | ||
| mock_client_class: mock.MagicMock, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| capsys: pytest.CaptureFixture[str], | ||
| ) -> None: | ||
| _setup_admin(mock_client_class) | ||
| monkeypatch.setattr( | ||
| "sys.argv", | ||
| [ | ||
| "delete_backup.py", | ||
| "--project-id", | ||
| PROJECT_ID, | ||
| "--instance-id", | ||
| INSTANCE_ID, | ||
| "--backup-id", | ||
| BACKUP_ID, | ||
| ], | ||
| ) | ||
|
|
||
| runpy.run_module("spanner_backups.delete_backup", run_name="__main__") | ||
|
|
||
| assert f"Deleted Backup: {BACKUP_ID}" in capsys.readouterr().out | ||
|
|
||
|
|
||
| @conftest.live_only | ||
| def test_delete_backup_live(capsys: pytest.CaptureFixture[str]) -> None: | ||
| required = ("SPANNER_DISPOSABLE_BACKUP_ID",) | ||
| missing = [name for name in required if not os.environ.get(name)] | ||
| if missing: | ||
| pytest.skip(f"Set {', '.join(missing)} to run this live test.") | ||
| disposable_bkp = os.environ["SPANNER_DISPOSABLE_BACKUP_ID"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing disposable_bkp = os.environ.get("SPANNER_DISPOSABLE_BACKUP_ID")
if not disposable_bkp:
pytest.skip("SPANNER_DISPOSABLE_BACKUP_ID environment variable not set.")
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, done: the live test now checks for its required environment variables up front and skips with a clear message when one is missing, instead of failing with a |
||
| delete_backup.delete_backup( | ||
| conftest.LIVE_PROJECT_ID, | ||
| conftest.LIVE_INSTANCE_ID, | ||
| disposable_bkp, | ||
| ) | ||
| out, err = capsys.readouterr() | ||
| assert err == "" | ||
| assert conftest.LIVE_PROJECT_ID not in out | ||
| assert f"Deleted Backup: {disposable_bkp}" in out | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of
wait_until_unreferencedloops indefinitely without providing any feedback to the user or having a timeout mechanism. Since database optimization after a restore can take a significant amount of time, a silent infinite loop can make the script appear frozen.Adding a status message inside the loop and a timeout mechanism (e.g., 30 minutes) will improve usability and prevent the script from hanging indefinitely in CI/CD pipelines or local executions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, done: the wait now prints a progress message, stops after 30 minutes, and reports the timeout on
stderr; a unit test covers it (1336509).