spanner/spannertest: support primary keys with no columns
Change-Id: I4caf85e1a090fce3a50076384b0e90ec92a52d64
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/44350
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Knut Olav Løite <koloite@gmail.com>
diff --git a/spanner/spannertest/db.go b/spanner/spannertest/db.go
index a055c76..34619e4 100644
--- a/spanner/spannertest/db.go
+++ b/spanner/spannertest/db.go
@@ -49,7 +49,7 @@
// They are reordered on table creation so the primary key columns come first.
cols []colInfo
colIndex map[string]int // col name to index
- pkCols int // number of primary key columns
+ pkCols int // number of primary key columns (may be 0)
rows []row
}
@@ -124,9 +124,6 @@
for i, kp := range stmt.PrimaryKey {
pk[kp.Column] = -1000 + i
}
- if len(pk) == 0 {
- return status.Newf(codes.InvalidArgument, "missing primary key")
- }
sort.SliceStable(stmt.Columns, func(i, j int) bool {
a, b := pk[stmt.Columns[i].Name], pk[stmt.Columns[j].Name]
return a < b
@@ -266,6 +263,9 @@
func (d *database) Update(tbl string, cols []string, values []*structpb.ListValue) error {
return d.writeValues(tbl, cols, values, func(t *table, colIndexes []int, r row) error {
+ if t.pkCols == 0 {
+ return status.Errorf(codes.InvalidArgument, "cannot update table %s with no columns in primary key", tbl)
+ }
pk := r[:t.pkCols]
rowNum := t.rowForPK(pk)
if rowNum < 0 {
@@ -552,7 +552,7 @@
}
// rowForPK returns the index of t.rows that holds the row for the given primary key.
-// It returns -1 if it isn't found.
+// It returns -1 if it isn't found, including when the table's primary key has no columns.
func (t *table) rowForPK(pk []interface{}) int {
if len(pk) != t.pkCols {
panic(fmt.Sprintf("primary key length mismatch: got %d values, table has %d", len(pk), t.pkCols))