-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(spanner): Add Cloud Spanner last statement samples #5296
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
Open
sgorse123
wants to merge
14
commits into
GoogleCloudPlatform:main
Choose a base branch
from
sgorse123:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−0
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
12f07a9
Add last statement samples
sgorse12 a1ec121
Merge branch 'GoogleCloudPlatform:main' into main
sgorse123 6d5014e
Revert changes to go.mod and go.sum
sgorse12 59f1b07
Fix region name based on review
sgorse12 425574b
Merge branch 'main' into main
sgorse123 707f1fa
Merge branch 'main' into main
iennae b7f4e52
Address review feedback
sgorse12 d24b01a
Merge branch 'main' of https://github.com/sgorse123/golang-samples
sgorse12 b133fee
Merge branch 'GoogleCloudPlatform:main' into main
sgorse123 41c53a1
Merge branch 'main' into main
rahul2393 3f94368
Merge branch 'main' into main
telpirion e49048c
Merge branch 'main' into main
telpirion 75b6584
Merge branch 'main' into main
iennae c1301c2
Merge branch 'main' into main
telpirion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1332,6 +1332,44 @@ func TestAddSplitPointsSample(t *testing.T) { | |
assertContains(t, out, "Added split points") | ||
} | ||
|
||
func TestDmlWithLastStatementSample(t *testing.T) { | ||
_ = testutil.SystemTest(t) | ||
t.Parallel() | ||
|
||
_, dbName, cleanup := initTest(t, randomID()) | ||
defer cleanup() | ||
|
||
_, cancel := context.WithTimeout(context.Background(), time.Hour) | ||
defer cancel() | ||
mustRunSample(t, createDatabase, dbName, "failed to create a database") | ||
|
||
out := runSample(t, insertAndUpdateDmlWithLastStatement, dbName, "failed to insert and then update using DML with last statement option") | ||
assertContains(t, out, "1 record(s) inserted.") | ||
assertContains(t, out, "1 record(s) updated.") | ||
} | ||
|
||
func TestPgDmlWithLastStatementSample(t *testing.T) { | ||
_ = testutil.SystemTest(t) | ||
t.Parallel() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous comment. Please don't run tests in parallel. |
||
|
||
_, dbName, cleanup := initTest(t, randomID()) | ||
defer cleanup() | ||
dbCleanup, err := createTestPgDatabase(dbName, | ||
`CREATE TABLE Singers ( | ||
SingerId bigint NOT NULL PRIMARY KEY, | ||
FirstName varchar(1024), | ||
LastName varchar(1024) | ||
)`) | ||
if err != nil { | ||
t.Fatalf("failed to create test database: %v", err) | ||
} | ||
defer dbCleanup() | ||
|
||
out := runSample(t, pgInsertAndUpdateDmlWithLastStatement, dbName, "failed to insert and then update using DML with last statement option") | ||
assertContains(t, out, "1 record(s) inserted.") | ||
assertContains(t, out, "1 record(s) updated.") | ||
} | ||
|
||
func maybeCreateKey(projectId, locationId, keyRingId, keyId string) error { | ||
client, err := kms.NewKeyManagementClient(context.Background()) | ||
if err != nil { | ||
|
67 changes: 67 additions & 0 deletions
67
spanner/spanner_snippets/spanner/pg_spanner_dml_last_statement.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package spanner | ||
|
||
// [START spanner_postgresql_dml_last_statement] | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"cloud.google.com/go/spanner" | ||
) | ||
|
||
// Inserts a record into the Singers table and then updates | ||
// the row while also setting the update DML as the last | ||
// statement. | ||
func pgInsertAndUpdateDmlWithLastStatement(w io.Writer, db string) error { | ||
telpirion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx := context.Background() | ||
client, err := spanner.NewClient(ctx, db) | ||
if err != nil { | ||
return err | ||
} | ||
defer client.Close() | ||
|
||
_, err = client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error { | ||
insertStmt := spanner.Statement{ | ||
SQL: `INSERT INTO Singers (SingerId, FirstName, LastName) | ||
VALUES (54214, 'John', 'Do')`, | ||
iennae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
insertRowCount, err := txn.Update(ctx, insertStmt) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintf(w, "%d record(s) inserted.\n", insertRowCount) | ||
|
||
updateStmt := spanner.Statement{ | ||
SQL: `UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54214`, | ||
} | ||
opts := spanner.QueryOptions{LastStatement: true} | ||
updateRowCount, err := txn.UpdateWithOptions(ctx, updateStmt, opts) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintf(w, "%d record(s) updated.\n", updateRowCount) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// [END spanner_postgresql_dml_last_statement] |
67 changes: 67 additions & 0 deletions
67
spanner/spanner_snippets/spanner/spanner_dml_last_statement.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package spanner | ||
|
||
// [START spanner_dml_last_statement] | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"cloud.google.com/go/spanner" | ||
) | ||
|
||
// Inserts a record into the Singers table and then updates | ||
// the row while also setting the update DML as the last | ||
// statement. | ||
func insertAndUpdateDmlWithLastStatement(w io.Writer, db string) error { | ||
telpirion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx := context.Background() | ||
client, err := spanner.NewClient(ctx, db) | ||
if err != nil { | ||
return err | ||
} | ||
defer client.Close() | ||
|
||
_, err = client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error { | ||
insertStmt := spanner.Statement{ | ||
SQL: `INSERT Singers (SingerId, FirstName, LastName) | ||
VALUES (54213, 'John', 'Do')`, | ||
sgorse123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
insertRowCount, err := txn.Update(ctx, insertStmt) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintf(w, "%d record(s) inserted.\n", insertRowCount) | ||
|
||
updateStmt := spanner.Statement{ | ||
SQL: `UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54213`, | ||
} | ||
opts := spanner.QueryOptions{LastStatement: true} | ||
updateRowCount, err := txn.UpdateWithOptions(ctx, updateStmt, opts) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintf(w, "%d record(s) updated.\n", updateRowCount) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// [END spanner_dml_last_statement] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: don't run these tests in parallel unless you are explicitly testing parallelization. This can cause race conditions and/or resource contention in our CI/CD.