Repository navigation
Conversation
Migrating existing primary keys into a sequence-backed table skips reserved ID ranges via skip_range_min and skip_range_max. Add a standalone spanner_alter_sequence sample and test suite.
|
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 adds functionality and tests for altering GoogleSQL sequences in Google Cloud Spanner. The review feedback identifies two key issues: first, in the live test's cleanup block, the Customers table must be dropped before the sequence is dropped to prevent a dependency violation; second, the main script's error handling should separate the sequence alteration and transaction execution to avoid misleading error messages if the customer insertion fails.
| finally: | ||
| drop_sequence.drop_sequence( | ||
| conftest.LIVE_PROJECT_ID, | ||
| conftest.LIVE_INSTANCE_ID, | ||
| conftest.LIVE_DATABASE_ID, | ||
| ) | ||
| admin.update_database_ddl( | ||
| request={ | ||
| "database": db_path, | ||
| "statements": ["DROP TABLE IF EXISTS Customers"], | ||
| } | ||
| ).result(timeout=240) |
There was a problem hiding this comment.
In Cloud Spanner, you cannot drop a sequence if it is still referenced by a table column's default value constraint. Since the Customers table references the Seq sequence, calling drop_sequence.drop_sequence before dropping the Customers table will fail with a FailedPrecondition error. The Customers table must be dropped first in the finally block.
| finally: | |
| drop_sequence.drop_sequence( | |
| conftest.LIVE_PROJECT_ID, | |
| conftest.LIVE_INSTANCE_ID, | |
| conftest.LIVE_DATABASE_ID, | |
| ) | |
| admin.update_database_ddl( | |
| request={ | |
| "database": db_path, | |
| "statements": ["DROP TABLE IF EXISTS Customers"], | |
| } | |
| ).result(timeout=240) | |
| finally: | |
| admin.update_database_ddl( | |
| request={ | |
| "database": db_path, | |
| "statements": ["DROP TABLE IF EXISTS Customers"], | |
| } | |
| ).result(timeout=240) | |
| drop_sequence.drop_sequence( | |
| conftest.LIVE_PROJECT_ID, | |
| conftest.LIVE_INSTANCE_ID, | |
| conftest.LIVE_DATABASE_ID, | |
| ) |
There was a problem hiding this comment.
Thanks! The cleanup works in this order because the drop-sequence sample first removes the column default (ALTER TABLE Customers ALTER COLUMN CustomerId DROP DEFAULT) and then drops the sequence, so the table no longer references it.
| try: | ||
| operation = admin_client.update_database_ddl(request=ddl_request) | ||
| print(f"Waiting for sequence 'Seq' alteration on '{database_id}'...") | ||
| operation.result(timeout=300) | ||
| database.run_in_transaction(insert_customers) | ||
| except exceptions.NotFound as err: | ||
| print( | ||
| f"error: database '{database_id}' on instance '{instance_id}' " | ||
| f"was not found: {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| except TimeoutError as err: | ||
| print( | ||
| f"error: timed out waiting for the operation on database '{database_id}' " | ||
| f"to complete; the operation may still be running: {err}", | ||
| file=sys.stderr, | ||
| ) | ||
| except exceptions.GoogleAPICallError as err: | ||
| print( | ||
| f"error: failed to alter sequence on database '{database_id}' " | ||
| f"in instance '{instance_id}': {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| else: | ||
| print(f"Resource ID: {database_id}") | ||
| for cid in customer_ids: | ||
| print(f"Customer ID: {cid}") | ||
| print(f"Inserted Customers: {len(customer_ids)}") |
There was a problem hiding this comment.
If database.run_in_transaction(insert_customers) fails (for example, due to a schema mismatch or missing table), the current error handling will catch it and print a misleading error message stating that the sequence alteration failed. Splitting the sequence alteration and the customer insertion into separate try-except blocks provides clearer and more accurate error messages.
try:
operation = admin_client.update_database_ddl(request=ddl_request)
print(f"Waiting for sequence 'Seq' alteration on '{database_id}'...")
operation.result(timeout=300)
except exceptions.NotFound as err:
print(
f"error: database '{database_id}' on instance '{instance_id}' "
f"was not found: {err.message}",
file=sys.stderr,
)
return
except TimeoutError as err:
print(
f"error: timed out waiting for the operation on database '{database_id}' "
f"to complete; the operation may still be running: {err}",
file=sys.stderr,
)
return
except exceptions.GoogleAPICallError as err:
print(
f"error: failed to alter sequence on database '{database_id}' "
f"in instance '{instance_id}': {err.message}",
file=sys.stderr,
)
return
try:
database.run_in_transaction(insert_customers)
except exceptions.GoogleAPICallError as err:
print(
f"error: failed to insert customers into database '{database_id}': {err.message}",
file=sys.stderr,
)
return
print(f"Resource ID: {database_id}")
for cid in customer_ids:
print(f"Customer ID: {cid}")
print(f"Inserted Customers: {len(customer_ids)}")There was a problem hiding this comment.
Thanks! Keeping a single block here for consistency with the other samples in this set; the error message includes the server message, which identifies the step that failed.
Problem: multi-line SQL and DDL statements were written as adjacent string fragments. That style is easy to get wrong (a missing trailing space joins two SQL tokens) and is harder to read than the statement itself, and the samples in this set mixed it with triple-quoted SQL. Solution: write each multi-line statement as a single triple-quoted string, matching the layout already used by the other samples. Only whitespace between SQL tokens changes; the statements are otherwise identical. Files updated: - spanner/database_admin/src/spanner_database_admin/alter_sequence.py
Description
Adds
spanner_alter_sequenceconfiguringskip_range_minandskip_range_maxon a GoogleSQL bit-reversed sequence.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions