Repository navigation
Conversation
PostgreSQL-dialect schema evolution guides add columns online using UpdateDatabaseDdlRequest. Add a standalone spanner_postgresql_add_column sample adding MarketingBudget BIGINT with unit and live tests.
|
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 script and corresponding tests to add a MarketingBudget column to the Albums table in a PostgreSQL-dialect Spanner database. The reviewer suggests explicitly catching TimeoutError when waiting for the DDL operation to complete to prevent unhandled tracebacks in case of a timeout.
| try: | ||
| operation = client.update_database_ddl(request=request) | ||
| print(f"Waiting for PostgreSQL column '{column_name}' addition...") | ||
| operation.result(timeout=300) | ||
| except (exceptions.AlreadyExists, exceptions.FailedPrecondition) as err: | ||
| print( | ||
| f"error: column '{column_name}' already exists or preconditions " | ||
| f"failed on database '{database_id}' in instance '{instance_id}': " | ||
| f"{err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| except exceptions.GoogleAPICallError as err: | ||
| print( | ||
| f"error: failed to add column '{column_name}' on database " | ||
| f"'{database_id}' in instance '{instance_id}': {err.message}", | ||
| file=sys.stderr, | ||
| ) |
There was a problem hiding this comment.
The operation.result(timeout=300) call can raise a TimeoutError (or concurrent.futures.TimeoutError) if the DDL operation takes longer than 5 minutes. Since this is not a subclass of GoogleAPICallError, it will result in an unhandled traceback. It is recommended to catch TimeoutError explicitly to provide a clean and user-friendly error message.
try:
operation = client.update_database_ddl(request=request)
print(f"Waiting for PostgreSQL column '{column_name}' addition...")
operation.result(timeout=300)
except (exceptions.AlreadyExists, exceptions.FailedPrecondition) as err:
print(
f"error: column '{column_name}' already exists or preconditions "
f"failed on database '{database_id}' in instance '{instance_id}': "
f"{err.message}",
file=sys.stderr,
)
except exceptions.GoogleAPICallError as err:
print(
f"error: failed to add column '{column_name}' on database "
f"'{database_id}' in instance '{instance_id}': {err.message}",
file=sys.stderr,
)
except TimeoutError as err:
print(
f"error: timeout waiting for column '{column_name}' addition: {err}",
file=sys.stderr,
)There was a problem hiding this comment.
Good catch, done: added an except TimeoutError handler and a unit test. Since the timeout only bounds the client-side wait, the message notes the operation may still be running (16b64b2).
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/postgresql/src/spanner_postgresql/postgresql_add_column.py - spanner/postgresql/tests/postgresql_add_column_test.py
Description
Adds
spanner_postgresql_add_columnadding aBIGINTcolumn viaDatabaseAdminClient.update_database_ddlon a PostgreSQL-dialect database.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions