Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve and fix Cloud Spanner samples that transfer marketing budget #2198

Merged
merged 1 commit into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions spanner/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def update_albums(transaction):

transfer_amount = 200000

if second_album_budget < 300000:
if second_album_budget < transfer_amount:
# Raising an exception will automatically roll back the
# transaction.
raise ValueError(
Expand Down Expand Up @@ -965,7 +965,7 @@ def query_data_with_parameter(instance_id, database_id):


def write_with_dml_transaction(instance_id, database_id):
""" Transfers a marketing budget from one album to another. """
""" Transfers part of a marketing budget from one album to another. """
# [START spanner_dml_getting_started_update]
# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"
Expand All @@ -977,28 +977,28 @@ def write_with_dml_transaction(instance_id, database_id):
def transfer_budget(transaction):
# Transfer marketing budget from one album to another. Performed in a
# single transaction to ensure that the transfer is atomic.
first_album_result = transaction.execute_sql(
second_album_result = transaction.execute_sql(
"SELECT MarketingBudget from Albums "
"WHERE SingerId = 1 and AlbumId = 1"
"WHERE SingerId = 2 and AlbumId = 2"
)
first_album_row = list(first_album_result)[0]
first_album_budget = first_album_row[0]
second_album_row = list(second_album_result)[0]
second_album_budget = second_album_row[0]

transfer_amount = 300000
transfer_amount = 200000

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

first_album_budget -= transfer_amount
second_album_budget += transfer_amount
second_album_budget -= transfer_amount
first_album_budget += transfer_amount

# Update first album
transaction.execute_update(
Expand All @@ -1018,7 +1018,7 @@ def transfer_budget(transaction):
param_types={"AlbumBudget": spanner.param_types.INT64}
)

print("Transferred {} from Album1's budget to Album2's".format(
print("Transferred {} from Album2's budget to Album1's".format(
transfer_amount))

database.run_in_transaction(transfer_budget)
Expand Down
2 changes: 1 addition & 1 deletion spanner/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def test_query_data_with_parameter(capsys):
def test_write_with_dml_transaction(capsys):
snippets.write_with_dml_transaction(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert "Transferred 300000 from Album1's budget to Album2's" in out
assert "Transferred 200000 from Album2's budget to Album1's" in out


def update_data_with_partitioned_dml(capsys):
Expand Down