Skip to content

feat(spanner): Add GetDatabaseDdl schema inspection sample - #14949

Open
iennae wants to merge 1 commit into
feat/spanner-create-databasefrom
feat/spanner-get-database-ddl
Open

iennae wants to merge 1 commit into
feat/spanner-create-databasefrom
feat/spanner-get-database-ddl

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_get_database_ddl retrieving schema DDL statements via DatabaseAdminClient.get_database_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

Exporting or auditing active Spanner table, index, and graph definitions calls DatabaseAdminClient.get_database_ddl. Add a standalone spanner_get_database_ddl 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

@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 module get_database_ddl.py and its associated tests to retrieve and print schema DDL statements for a Google Cloud Spanner database. Feedback on the changes points out a potential IndexError when parsing empty or whitespace-only DDL statements, suggesting a safer check before accessing the first line of the split statement.

Comment on lines +50 to +52
for statement in statements:
first_line = statement.strip().splitlines()[0]
print(f"DDL Statement: {first_line}")

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

If a DDL statement is empty or contains only whitespace, statement.strip().splitlines() will return an empty list, causing an IndexError when accessing the first element [0]. It is safer to check if the list is non-empty before accessing its elements.

Suggested change
for statement in statements:
first_line = statement.strip().splitlines()[0]
print(f"DDL Statement: {first_line}")
for statement in statements:
lines = statement.strip().splitlines()
first_line = lines[0] if lines else ""
print(f"DDL Statement: {first_line}")

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! Keeping this as is: the statements returned for a database's schema are not empty, and the extra branching would distract from what the sample shows.

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