Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d964673

Browse filesBrowse files
hegemonicjsimonweb
authored andcommitted
Improve and fix Cloud Spanner samples that transfer marketing budget (GoogleCloudPlatform#2198)
The samples that transfer part of an album's marketing budget had some issues: + `read_write_transaction`: Compared `second_album_budget` with an arbitrary integer, rather than explicitly checking against `transfer_amount`. + `write_with_dml_transaction`: Moved money from album 1 to album 2, even though `read_write_transaction` was the other way around. Also retrieved album 1's budget where it should have retrieved album 2's budget. This change fixes those issues and updates the tests accordingly.
1 parent e124961 commit d964673
Copy full SHA for d964673

File tree

Expand file treeCollapse file tree

2 files changed

+15
-15
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+15
-15
lines changed

‎spanner/cloud-client/snippets.py

Copy file name to clipboardExpand all lines: spanner/cloud-client/snippets.py
+14-14Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def update_albums(transaction):
414414

415415
transfer_amount = 200000
416416

417-
if second_album_budget < 300000:
417+
if second_album_budget < transfer_amount:
418418
# Raising an exception will automatically roll back the
419419
# transaction.
420420
raise ValueError(
@@ -965,7 +965,7 @@ def query_data_with_parameter(instance_id, database_id):
965965

966966

967967
def write_with_dml_transaction(instance_id, database_id):
968-
""" Transfers a marketing budget from one album to another. """
968+
""" Transfers part of a marketing budget from one album to another. """
969969
# [START spanner_dml_getting_started_update]
970970
# instance_id = "your-spanner-instance"
971971
# database_id = "your-spanner-db-id"
@@ -977,28 +977,28 @@ def write_with_dml_transaction(instance_id, database_id):
977977
def transfer_budget(transaction):
978978
# Transfer marketing budget from one album to another. Performed in a
979979
# single transaction to ensure that the transfer is atomic.
980-
first_album_result = transaction.execute_sql(
980+
second_album_result = transaction.execute_sql(
981981
"SELECT MarketingBudget from Albums "
982-
"WHERE SingerId = 1 and AlbumId = 1"
982+
"WHERE SingerId = 2 and AlbumId = 2"
983983
)
984-
first_album_row = list(first_album_result)[0]
985-
first_album_budget = first_album_row[0]
984+
second_album_row = list(second_album_result)[0]
985+
second_album_budget = second_album_row[0]
986986

987-
transfer_amount = 300000
987+
transfer_amount = 200000
988988

989989
# Transaction will only be committed if this condition still holds at
990990
# the time of commit. Otherwise it will be aborted and the callable
991991
# will be rerun by the client library
992-
if first_album_budget >= transfer_amount:
993-
second_album_result = transaction.execute_sql(
992+
if second_album_budget >= transfer_amount:
993+
first_album_result = transaction.execute_sql(
994994
"SELECT MarketingBudget from Albums "
995995
"WHERE SingerId = 1 and AlbumId = 1"
996996
)
997-
second_album_row = list(second_album_result)[0]
998-
second_album_budget = second_album_row[0]
997+
first_album_row = list(first_album_result)[0]
998+
first_album_budget = first_album_row[0]
999999

1000-
first_album_budget -= transfer_amount
1001-
second_album_budget += transfer_amount
1000+
second_album_budget -= transfer_amount
1001+
first_album_budget += transfer_amount
10021002

10031003
# Update first album
10041004
transaction.execute_update(
@@ -1018,7 +1018,7 @@ def transfer_budget(transaction):
10181018
param_types={"AlbumBudget": spanner.param_types.INT64}
10191019
)
10201020

1021-
print("Transferred {} from Album1's budget to Album2's".format(
1021+
print("Transferred {} from Album2's budget to Album1's".format(
10221022
transfer_amount))
10231023

10241024
database.run_in_transaction(transfer_budget)

‎spanner/cloud-client/snippets_test.py

Copy file name to clipboardExpand all lines: spanner/cloud-client/snippets_test.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def test_query_data_with_parameter(capsys):
272272
def test_write_with_dml_transaction(capsys):
273273
snippets.write_with_dml_transaction(INSTANCE_ID, DATABASE_ID)
274274
out, _ = capsys.readouterr()
275-
assert "Transferred 300000 from Album1's budget to Album2's" in out
275+
assert "Transferred 200000 from Album2's budget to Album1's" in out
276276

277277

278278
def update_data_with_partitioned_dml(capsys):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.