Skip to content

Commit 8ffcfc1

Browse files
committed
add sample for apply at leaast once
1 parent ea98d57 commit 8ffcfc1

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

spanner/spanner_snippets/spanner/integration_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,13 @@ func TestSample(t *testing.T) {
414414
out = runSample(t, directedReadOptions, dbName, "failed to read using directed read options")
415415
assertContains(t, out, "1 1 Total Junk")
416416

417+
out = runSample(t, rwTxnExcludedFromChangeStreams, dbName, "failed to commit rw txn excluded from change streams")
418+
assertContains(t, out, "New singer inserted.")
419+
assertContains(t, out, "Singer first name updated.")
417420
out = runSample(t, applyExcludedFromChangeStreams, dbName, "failed to apply mutations excluded from change streams")
418421
assertContains(t, out, "New singer inserted.")
419-
out = runSample(t, rwTxnExcludedFromChangeStreams, dbName, "failed to commit rw txn excluded from change streams")
422+
out = runSample(t, applyAtLeastOnceExcludedFromChangeStreams, dbName, "failed to apply at least once mutations excluded from change streams")
420423
assertContains(t, out, "New singer inserted.")
421-
assertContains(t, out, "Singer first name updated.")
422424
out = runSample(t, batchWriteExcludedFromChangeStreams, dbName, "failed to write data using BatchWrite excluded from change streams")
423425
assertNotContains(t, out, "could not be applied with error")
424426
out = runSample(t, pdmlExcludedFromChangeStreams, dbName, "failed to update using partitioned DML excluded from change streams")

spanner/spanner_snippets/spanner/spanner_change_streams_txn_exclusion.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@ func applyExcludedFromChangeStreams(w io.Writer, db string) error {
8383
return err
8484
}
8585

86+
// applyExcludedFromChangeStreams apply the insert mutations on Singers table excluded from allowed tracking change streams
87+
func applyAtLeastOnceExcludedFromChangeStreams(w io.Writer, db string) error {
88+
// db = `projects/<project>/instances/<instance-id>/database/<database-id>`
89+
ctx := context.Background()
90+
client, err := spanner.NewClient(ctx, db)
91+
if err != nil {
92+
return fmt.Errorf("applyExcludedFromChangeStreams.NewClient: %w", err)
93+
}
94+
defer client.Close()
95+
m := spanner.Insert("Singers",
96+
[]string{"SingerId", "FirstName", "LastName"},
97+
[]interface{}{989, "Hellen", "Lee"})
98+
_, err = client.Apply(ctx, []*spanner.Mutation{m}, []spanner.ApplyOption{spanner.ExcludeTxnFromChangeStreams(), spanner.ApplyAtLeastOnce()}...)
99+
100+
if err != nil {
101+
return err
102+
}
103+
fmt.Fprint(w, "applyExcludedFromChangeStreams.ApplyAtLeastOnce: New singer inserted.")
104+
return err
105+
}
106+
86107
// batchWriteExcludedFromChangeStreams executes the insert mutation on Singers table excluded from allowed tracking change streams
87108
func batchWriteExcludedFromChangeStreams(w io.Writer, db string) error {
88109
// db := "projects/my-project/instances/my-instance/databases/my-database"

0 commit comments

Comments
 (0)