Repository navigation
Conversation
Consistent multi-statement reads share a single snapshot timestamp via database.snapshot(multi_use=True). Add a standalone spanner_read_only_transaction sample combining SQL and KeySet reads.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new script and corresponding tests to demonstrate executing consistent multi-statement reads inside a multi-use read-only transaction in Cloud Spanner. The review feedback suggests allowing the project_id parameter to accept None and adjusting the CLI argument defaults to enable seamless Application Default Credentials (ADC) inference. Additionally, it recommends making the NotFound exception error message more generic, as the exception can be raised if the table is missing, not just the database.
| from google.cloud import spanner | ||
|
|
||
|
|
||
| def read_only_transaction(project_id: str, instance_id: str, database_id: str) -> None: |
There was a problem hiding this comment.
To allow the project ID to be inferred from Application Default Credentials (ADC) when it is not explicitly provided, update the project_id parameter type to accept None.
| def read_only_transaction(project_id: str, instance_id: str, database_id: str) -> None: | |
| def read_only_transaction(project_id: str | None, instance_id: str, database_id: str) -> None: |
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
| except exceptions.NotFound as err: | ||
| print( | ||
| f"error: database '{database_id}' on instance '{instance_id}' " | ||
| f"was not found: {err.message}", | ||
| file=sys.stderr, | ||
| ) |
There was a problem hiding this comment.
The exceptions.NotFound exception can be raised if the Albums table does not exist, not just if the database itself is missing. Printing that the database was not found can be misleading. Consider using a more generic error message that includes the actual exception message.
except exceptions.NotFound as err:
print(
f"error: resource not found: {err.message}",
file=sys.stderr,
)There was a problem hiding this comment.
Done: the message now says a resource was not found in the database and includes the server message (dd48243).
| "--project-id", | ||
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | ||
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | ||
| ) |
There was a problem hiding this comment.
By defaulting --project-id to 'example-project-id', the script prevents the Spanner client from automatically inferring the project ID from Application Default Credentials (ADC) when the argument is omitted and GOOGLE_CLOUD_PROJECT is not set. Defaulting to None (or omitting the default value) allows ADC inference to work seamlessly.
parser.add_argument(
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT"),
help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)",
)References
- When using
argparsefor a command-line script, the--project_idargument can be optional, as the project can be inferred from Application Default Credentials (ADC) if not provided.
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
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/transactions/src/spanner_transactions/read_only_transaction.py - spanner/transactions/tests/read_only_transaction_test.py
Description
Adds
spanner_read_only_transactiondemonstrating consistent multi-statementexecute_sql()andread()calls insidedatabase.snapshot(multi_use=True).Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions