Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 96e6e15

Browse files
committed
Update sample
1 parent a32dd63 commit 96e6e15

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

samples/samples/snippets.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,15 @@ def insert_data(instance_id, database_id):
403403
# [END spanner_insert_data]
404404

405405

406-
# [START spanner_batch_write]
406+
# [START spanner_batch_write_at_least_once]
407407
def batch_write(instance_id, database_id):
408408
"""Inserts sample data into the given database via BatchWrite API.
409409
410410
The database and table must already exist and can be created using
411411
`create_database`.
412412
"""
413+
from google.rpc.code_pb2 import OK
414+
413415
spanner_client = spanner.Client()
414416
instance = spanner_client.instance(instance_id)
415417
database = instance.database(database_id)
@@ -429,7 +431,7 @@ def batch_write(instance_id, database_id):
429431
table="Singers",
430432
columns=("SingerId", "FirstName", "LastName"),
431433
values=[
432-
(17, "Marc", "Richards"),
434+
(17, "Marc", ""),
433435
(18, "Catalina", "Smith"),
434436
],
435437
)
@@ -443,12 +445,21 @@ def batch_write(instance_id, database_id):
443445
)
444446

445447
for response in groups.batch_write():
446-
print(response)
447-
448-
print("Inserted data.")
448+
if response.status.code == OK:
449+
print(
450+
"Mutation group indexes {} have been applied with commit timestamp {}".format(
451+
response.indexes, response.commit_timestamp
452+
)
453+
)
454+
else:
455+
print(
456+
"Mutation group indexes {} could not be applied with error {}".format(
457+
response.indexes, response.status
458+
)
459+
)
449460

450461

451-
# [END spanner_batch_write]
462+
# [END spanner_batch_write_at_least_once]
452463

453464

454465
# [START spanner_delete_data]

samples/samples/snippets_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def test_insert_data(capsys, instance_id, sample_database):
294294
def test_batch_write(capsys, instance_id, sample_database):
295295
snippets.batch_write(instance_id, sample_database.database_id)
296296
out, _ = capsys.readouterr()
297-
assert "Inserted data" in out
297+
assert "could not be applied with error" not in out
298298

299299

300300
@pytest.mark.dependency(depends=["insert_data"])

0 commit comments

Comments
 (0)