Repository navigation
Conversation
Capturing deleted primary keys and generated columns during a DELETE statement requires DELETE ... THEN RETURN inside run_in_transaction.
|
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 sample script delete_dml_returning.py and its associated tests to demonstrate deleting rows in Google Cloud Spanner using DML with a THEN RETURN clause. The feedback suggests replacing an obsolete Python 2 pattern—using a single-element list (row_counts) to emulate a mutable reference across nested scopes—with the modern Python 3 nonlocal keyword, and updating the corresponding print statement to use this new variable.
| 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' " | ||
| "THEN RETURN 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 single-element list (row_counts) to emulate a mutable reference for an outer scope variable is an obsolete pattern from Python 2. In Python 3, the nonlocal keyword should be used instead to declare that a variable inside a nested function refers to a variable in the nearest enclosing 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' " | |
| "THEN RETURN 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 row_count | |
| deleted_singers.clear() | |
| results = transaction.execute_sql( | |
| "DELETE FROM Singers WHERE FirstName = 'David' " | |
| "THEN RETURN 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.
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/dml/src/spanner_dml/delete_dml_returning.py
Description
Using
DELETE ... THEN RETURNcaptures deleted row keys and generated columns atomically as rows are removed.Adds
spanner_delete_dml_returningusing the canonical cross-language region tag.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions