Skip to content

feat(spanner): Add fine-grained database role DDL lifecycle sample - #14958

Open
iennae wants to merge 2 commits into
feat/spanner-create-databasefrom
feat/spanner-add-and-drop-database-role
Open

iennae wants to merge 2 commits into
feat/spanner-create-databasefrom
feat/spanner-add-and-drop-database-role

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_add_and_drop_database_role creating, granting, revoking, and dropping fine-grained Spanner database roles.

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

Fine-grained access control (FGAC) defines hierarchical SQL roles with CREATE ROLE, GRANT, REVOKE, and DROP ROLE DDL statements. Add a standalone spanner_add_and_drop_database_role sample.
@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 sample script add_and_drop_database_role.py along with its corresponding tests to demonstrate creating, granting, and dropping fine-grained database roles in Google Cloud Spanner. The feedback suggests improving the live integration test by wrapping the sample execution in a try...finally block to guarantee that the created database roles are properly cleaned up even if the test fails.

Comment on lines +124 to +141
for cleanup_stmt in [
"REVOKE ROLE new_parent FROM ROLE new_child",
"DROP ROLE new_child",
"REVOKE SELECT ON TABLE Singers FROM ROLE new_parent",
"DROP ROLE new_parent",
]:
try:
admin.update_database_ddl(
request={"database": db_path, "statements": [cleanup_stmt]}
).result(timeout=120)
except exceptions.GoogleAPICallError:
pass

add_and_drop_database_role.add_and_drop_database_role(
conftest.LIVE_PROJECT_ID,
conftest.LIVE_INSTANCE_ID,
conftest.LIVE_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

The live integration test currently performs cleanup only before running the sample. Since the sample creates new_parent and new_child but only drops new_child, the new_parent role is left in the database after a successful run. Additionally, if the test fails midway, both roles may remain. Wrapping the sample execution in a try...finally block ensures that resources are always cleaned up, keeping the live test database clean.

    def cleanup():
        for cleanup_stmt in [
            "REVOKE ROLE new_parent FROM ROLE new_child",
            "DROP ROLE new_child",
            "REVOKE SELECT ON TABLE Singers FROM ROLE new_parent",
            "DROP ROLE new_parent",
        ]:
            try:
                admin.update_database_ddl(
                    request={"database": db_path, "statements": [cleanup_stmt]}
                ).result(timeout=120)
            except exceptions.GoogleAPICallError:
                pass

    cleanup()
    try:
        add_and_drop_database_role.add_and_drop_database_role(
            conftest.LIVE_PROJECT_ID,
            conftest.LIVE_INSTANCE_ID,
            conftest.LIVE_DATABASE_ID,
        )
    finally:
        cleanup()
References
  1. Maintain consistency in test structures by matching the patterns employed by existing or neighboring tests in the same directory, such as using a try...finally block with a teardown function for resource cleanup.

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 catch, done: the role cleanup now also runs in a finally block, so new_parent is not left behind after the test (5c3ce28).

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