Repository navigation
Conversation
Dropping a GoogleSQL sequence referenced by a column DEFAULT expression detaches the default before issuing DROP SEQUENCE. Add a standalone spanner_drop_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 introduces a new script drop_sequence.py and its corresponding tests in drop_sequence_test.py to detach a column DEFAULT expression and drop a GoogleSQL sequence in Spanner. The review feedback suggests two improvements to the unit tests: first, asserting that update_database_ddl is called with the correct request parameters to ensure proper DDL statement construction; second, mocking errors on the operation's result() method rather than the initial client call to more accurately reflect Spanner's asynchronous error behavior.
| f"projects/{PROJECT_ID}/instances/{INSTANCE_ID}/databases/{DATABASE_ID}" | ||
| ) | ||
|
|
||
| drop_sequence.drop_sequence(PROJECT_ID, INSTANCE_ID, DATABASE_ID) |
There was a problem hiding this comment.
It is highly recommended to assert that update_database_ddl was called with the correct request parameters. This ensures that the DDL statements and database path are constructed correctly.
drop_sequence.drop_sequence(PROJECT_ID, INSTANCE_ID, DATABASE_ID)
mock_client_class.return_value.update_database_ddl.assert_called_once_with(
request=spanner_admin_database_v1.UpdateDatabaseDdlRequest(
database=f"projects/{PROJECT_ID}/instances/{INSTANCE_ID}/databases/{DATABASE_ID}",
statements=[
"ALTER TABLE Customers ALTER COLUMN CustomerId DROP DEFAULT",
"DROP SEQUENCE Seq",
],
)
)There was a problem hiding this comment.
Good suggestion, done: the unit test now asserts the database path and both DDL statements in the request (e7f68aa).
| mock_client_class.return_value.database_path.return_value = ( | ||
| f"projects/{PROJECT_ID}/instances/{INSTANCE_ID}/databases/{DATABASE_ID}" | ||
| ) | ||
| mock_client_class.return_value.update_database_ddl.side_effect = error |
There was a problem hiding this comment.
In a real Spanner environment, asynchronous DDL errors (like FailedPrecondition) and timeouts (TimeoutError) are raised when calling operation.result(), rather than during the initial update_database_ddl call. Mocking the error on the operation's result method makes the unit test more realistic and robust.
| mock_client_class.return_value.update_database_ddl.side_effect = error | |
| mock_operation = mock.MagicMock() | |
| mock_operation.result.side_effect = error | |
| mock_client_class.return_value.update_database_ddl.return_value = mock_operation |
There was a problem hiding this comment.
Thanks! Both the initial call and operation.result() are inside the same try block and reach the same handlers, so the test keeps the simpler mock used across this set.
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/database_admin/tests/drop_sequence_test.py
Description
Adds
spanner_drop_sequencedetaching a columnDEFAULTand dropping a GoogleSQL sequence via DDL.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions