Repository navigation
Conversation
Bulk data retention and cleanup jobs delete matching rows across key-range partitions via database.execute_partitioned_dml(). Add a standalone spanner_dml_partitioned_delete sample and test suite.
|
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 module dml_partitioned_delete.py to execute bulk Partitioned DML DELETE statements on a Google Cloud Spanner database, along with its corresponding unit and live tests. The feedback suggests improving the live test's backup and restore logic by dynamically querying and restoring all columns (SELECT *) rather than hardcoding specific columns, which prevents potential data loss or corruption of other columns in a shared test database.
| with db.snapshot(multi_use=True) as snap: | ||
| saved_singers = list( | ||
| snap.execute_sql( | ||
| "SELECT SingerId, FirstName, LastName FROM Singers WHERE SingerId > 10" | ||
| ) | ||
| ) | ||
| saved_albums = list( | ||
| snap.execute_sql( | ||
| "SELECT SingerId, AlbumId, AlbumTitle, MarketingBudget " | ||
| "FROM Albums WHERE SingerId > 10" | ||
| ) | ||
| ) |
There was a problem hiding this comment.
To prevent data loss or corruption of other columns (such as BirthDate or SingerInfo in Singers, or other custom columns in Albums) in the shared test database, it is safer to query all columns using SELECT * and dynamically retrieve the column names from the cursor's fields. This ensures that when the rows are restored in the finally block, all column values are preserved.
| with db.snapshot(multi_use=True) as snap: | |
| saved_singers = list( | |
| snap.execute_sql( | |
| "SELECT SingerId, FirstName, LastName FROM Singers WHERE SingerId > 10" | |
| ) | |
| ) | |
| saved_albums = list( | |
| snap.execute_sql( | |
| "SELECT SingerId, AlbumId, AlbumTitle, MarketingBudget " | |
| "FROM Albums WHERE SingerId > 10" | |
| ) | |
| ) | |
| with db.snapshot(multi_use=True) as snap: | |
| singers_cursor = snap.execute_sql( | |
| "SELECT * FROM Singers WHERE SingerId > 10" | |
| ) | |
| saved_singers = list(singers_cursor) | |
| singers_columns = [field.name for field in singers_cursor.fields] | |
| albums_cursor = snap.execute_sql( | |
| "SELECT * FROM Albums WHERE SingerId > 10" | |
| ) | |
| saved_albums = list(albums_cursor) | |
| albums_columns = [field.name for field in albums_cursor.fields] |
References
- 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.
There was a problem hiding this comment.
Done, thank you: the live test saves all columns with SELECT * and restores them using the column names from the result set (537908f).
| if saved_singers: | ||
| with db.batch() as batch: | ||
| batch.insert_or_update( | ||
| table="Singers", | ||
| columns=("SingerId", "FirstName", "LastName"), | ||
| values=saved_singers, | ||
| ) | ||
| if saved_albums: | ||
| batch.insert_or_update( | ||
| table="Albums", | ||
| columns=( | ||
| "SingerId", | ||
| "AlbumId", | ||
| "AlbumTitle", | ||
| "MarketingBudget", | ||
| ), | ||
| values=saved_albums, | ||
| ) |
There was a problem hiding this comment.
Update the restore logic in the finally block to use the dynamically retrieved column names (singers_columns and albums_columns). This ensures that all columns (including any nullable or default columns not hardcoded here) are correctly restored to their original values.
if saved_singers:
with db.batch() as batch:
batch.insert_or_update(
table="Singers",
columns=singers_columns,
values=saved_singers,
)
if saved_albums:
batch.insert_or_update(
table="Albums",
columns=albums_columns,
values=saved_albums,
)References
- 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.
There was a problem hiding this comment.
Done, thank you: the restore uses the column names captured from the result set (537908f).
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/dml/tests/dml_partitioned_delete_test.py
Description
Adds
spanner_dml_partitioned_deletedemonstrating bulk row deletion viadatabase.execute_partitioned_dml().Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions