Skip to content

feat(spanner): Add GoogleSQL DROP SEQUENCE DDL sample - #14956

Open
iennae wants to merge 2 commits into
feat/spanner-create-databasefrom
feat/spanner-drop-sequence
Open

iennae wants to merge 2 commits into
feat/spanner-create-databasefrom
feat/spanner-drop-sequence

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_drop_sequence detaching a column DEFAULT and dropping a GoogleSQL sequence via DDL.

Note: Stacked on #14943 (feat/spanner-create-database) so this PR shows only this sample's diff. It will merge into main after #14943.

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

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.
@iennae
iennae requested review from a team as code owners November 18, 2026 22:18
@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 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)

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

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",
            ],
        )
    )

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

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

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.

Suggested change
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

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.

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

1 participant