Skip to content

Commit

Permalink
add sample for apply at leaast once
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuranZhang committed May 8, 2024
1 parent ea98d57 commit 8ffcfc1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions spanner/spanner_snippets/spanner/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,13 @@ func TestSample(t *testing.T) {
out = runSample(t, directedReadOptions, dbName, "failed to read using directed read options")
assertContains(t, out, "1 1 Total Junk")

out = runSample(t, rwTxnExcludedFromChangeStreams, dbName, "failed to commit rw txn excluded from change streams")
assertContains(t, out, "New singer inserted.")
assertContains(t, out, "Singer first name updated.")
out = runSample(t, applyExcludedFromChangeStreams, dbName, "failed to apply mutations excluded from change streams")
assertContains(t, out, "New singer inserted.")
out = runSample(t, rwTxnExcludedFromChangeStreams, dbName, "failed to commit rw txn excluded from change streams")
out = runSample(t, applyAtLeastOnceExcludedFromChangeStreams, dbName, "failed to apply at least once mutations excluded from change streams")
assertContains(t, out, "New singer inserted.")
assertContains(t, out, "Singer first name updated.")
out = runSample(t, batchWriteExcludedFromChangeStreams, dbName, "failed to write data using BatchWrite excluded from change streams")
assertNotContains(t, out, "could not be applied with error")
out = runSample(t, pdmlExcludedFromChangeStreams, dbName, "failed to update using partitioned DML excluded from change streams")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ func applyExcludedFromChangeStreams(w io.Writer, db string) error {
return err
}

// applyExcludedFromChangeStreams apply the insert mutations on Singers table excluded from allowed tracking change streams
func applyAtLeastOnceExcludedFromChangeStreams(w io.Writer, db string) error {
// db = `projects/<project>/instances/<instance-id>/database/<database-id>`
ctx := context.Background()
client, err := spanner.NewClient(ctx, db)
if err != nil {
return fmt.Errorf("applyExcludedFromChangeStreams.NewClient: %w", err)
}
defer client.Close()
m := spanner.Insert("Singers",
[]string{"SingerId", "FirstName", "LastName"},
[]interface{}{989, "Hellen", "Lee"})
_, err = client.Apply(ctx, []*spanner.Mutation{m}, []spanner.ApplyOption{spanner.ExcludeTxnFromChangeStreams(), spanner.ApplyAtLeastOnce()}...)

if err != nil {
return err
}
fmt.Fprint(w, "applyExcludedFromChangeStreams.ApplyAtLeastOnce: New singer inserted.")
return err
}

// batchWriteExcludedFromChangeStreams executes the insert mutation on Singers table excluded from allowed tracking change streams
func batchWriteExcludedFromChangeStreams(w io.Writer, db string) error {
// db := "projects/my-project/instances/my-instance/databases/my-database"
Expand Down

0 comments on commit 8ffcfc1

Please sign in to comment.