Repository navigation
Conversation
PostgreSQL-dialect Spanner uses DELETE ... RETURNING to capture deleted row identifiers and generated columns.
|
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 Python sample and corresponding tests demonstrating how to perform a PostgreSQL-dialect Spanner DELETE DML operation with a RETURNING clause. The feedback suggests a cleaner, more idiomatic Python 3 refactoring: replacing the legacy Python 2 pattern of using a list (row_counts) to emulate a mutable cell with the nonlocal keyword to rebind variables in the outer scope.
| deleted_singers: list[tuple[int, str]] = [] | ||
| row_counts: list[int] = [] | ||
|
|
||
| def delete_singers(transaction: spanner_v1.Transaction) -> None: | ||
| deleted_singers.clear() | ||
| row_counts.clear() | ||
| results = transaction.execute_sql( | ||
| "DELETE FROM Singers WHERE FirstName = 'David' " | ||
| "RETURNING SingerId, FullName" | ||
| ) | ||
| for singer_id, full_name in results: | ||
| deleted_singers.append((int(singer_id), str(full_name))) | ||
| row_counts.append(int(results.stats.row_count_exact)) |
There was a problem hiding this comment.
Using a list (row_counts) to emulate a mutable cell is a legacy Python 2 pattern. In Python 3, you can use the nonlocal keyword to rebind variables in the outer scope from within a nested function. This is cleaner and more idiomatic.
| deleted_singers: list[tuple[int, str]] = [] | |
| row_counts: list[int] = [] | |
| def delete_singers(transaction: spanner_v1.Transaction) -> None: | |
| deleted_singers.clear() | |
| row_counts.clear() | |
| results = transaction.execute_sql( | |
| "DELETE FROM Singers WHERE FirstName = 'David' " | |
| "RETURNING SingerId, FullName" | |
| ) | |
| for singer_id, full_name in results: | |
| deleted_singers.append((int(singer_id), str(full_name))) | |
| row_counts.append(int(results.stats.row_count_exact)) | |
| deleted_singers: list[tuple[int, str]] = [] | |
| row_count = 0 | |
| def delete_singers(transaction: spanner_v1.Transaction) -> None: | |
| nonlocal deleted_singers, row_count | |
| deleted_singers = [] | |
| results = transaction.execute_sql( | |
| "DELETE FROM Singers WHERE FirstName = 'David' " | |
| "RETURNING SingerId, FullName" | |
| ) | |
| for singer_id, full_name in results: | |
| deleted_singers.append((int(singer_id), str(full_name))) | |
| row_count = int(results.stats.row_count_exact) |
There was a problem hiding this comment.
Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.
| ) | ||
| else: | ||
| print(f"Resource ID: {database.name}") | ||
| print(f"Deleted Rows: {row_counts[-1]}") |
There was a problem hiding this comment.
Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.
Uses a nonlocal row counter with a defensive results.stats check and omits project identifiers from stdout and stderr.
…l_returning 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/postgresql/src/spanner_postgresql/postgresql_delete_dml_returning.py
Description
PostgreSQL-dialect Spanner databases use
DELETE ... RETURNINGto return deleted row values atomically inside a transaction.Adds
spanner_postgresql_delete_dml_returningusing the canonical cross-language region tag.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions