Skip to content

feat(spanner): Add DeleteBackup sample waiting on referencing DBs - #14980

Open
iennae wants to merge 2 commits into
feat/spanner-copy-backupfrom
feat/spanner-delete-backup
Open

iennae wants to merge 2 commits into
feat/spanner-copy-backupfrom
feat/spanner-delete-backup

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_delete_backup checking referencing_databases and deleting a Spanner backup.

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

Deleting a Spanner backup waits for any optimizing restored databases in referencing_databases before calling DatabaseAdminClient.delete_backup. Add spanner_delete_backup.
@iennae
iennae requested review from a team as code owners November 18, 2026 22:19
@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 script and associated tests for deleting a Google Cloud Spanner database backup after waiting for referencing databases to finish optimizing. The feedback recommends adding a timeout mechanism and status logging to the polling loop in delete_backup to prevent indefinite hangs, and gracefully skipping the live integration test if the required environment variable is missing.

Comment on lines +36 to +40
def wait_until_unreferenced() -> None:
backup = client.get_backup(request=get_request)
while backup.referencing_databases:
time.sleep(10)
backup = client.get_backup(request=get_request)

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

The current implementation of wait_until_unreferenced loops 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.

    def wait_until_unreferenced(timeout_seconds: int = 1800) -> None:
        start_time = time.time()
        backup = client.get_backup(request=get_request)
        while backup.referencing_databases:
            if time.time() - start_time > timeout_seconds:
                raise TimeoutError(
                    f"Timed out after {timeout_seconds} seconds waiting for backup to be unreferenced."
                )
            print(
                f"Backup is still referenced by {len(backup.referencing_databases)} database(s). "
                "Waiting 10 seconds...",
                file=sys.stderr,
            )
            time.sleep(10)
            backup = client.get_backup(request=get_request)

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.

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).


@conftest.live_only
def test_delete_backup_live(capsys: pytest.CaptureFixture[str]) -> None:
disposable_bkp = os.environ["SPANNER_DISPOSABLE_BACKUP_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

Accessing os.environ["SPANNER_DISPOSABLE_BACKUP_ID"] directly will raise a KeyError if the environment variable is not set. It is safer to use os.environ.get and skip the test gracefully if the variable is missing, especially since this is a live integration test.

    disposable_bkp = os.environ.get("SPANNER_DISPOSABLE_BACKUP_ID")
    if not disposable_bkp:
        pytest.skip("SPANNER_DISPOSABLE_BACKUP_ID environment variable not set.")

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.

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 KeyError (1336509).

Problem: review of this sample identified correctness and robustness
improvements in the sample and its tests.

Solution: apply the accepted review suggestions and extend the tests to
cover the updated behavior. Details are in the review thread replies.

Files updated:
  - spanner/backups/src/spanner_backups/delete_backup.py
  - spanner/backups/tests/delete_backup_test.py
@angelcaamal angelcaamal self-assigned this Nov 19, 2026
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.

2 participants