Skip to content

feat(spanner): Add GoogleSQL bit-reversed sequence creation sample - #14954

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

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

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_create_sequence creating a GoogleSQL bit_reversed_positive sequence and inserting rows via THEN RETURN.

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

Auto-generated primary keys avoid write hotspots in GoogleSQL using bit_reversed_positive sequences with GET_NEXT_SEQUENCE_VALUE defaults. Add spanner_create_sequence creating Seq and inserting Customers.
@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 and corresponding tests to create a GoogleSQL bit-reversed sequence and insert customer records in Google Cloud Spanner. The review feedback points out a dependency issue in the live test cleanup where dropping the sequence before dropping the referencing table will result in a Spanner error; the table must be dropped first.

Comment on lines +158 to +168
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)

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 Google Cloud Spanner, a sequence cannot be dropped if it is still referenced by a table's column default value (such as CustomerId INT64 DEFAULT (GET_NEXT_SEQUENCE_VALUE(SEQUENCE Seq))). Calling drop_sequence.drop_sequence before dropping the Customers table will result in a dependency error and fail the test cleanup. The table must be dropped first.

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

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

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