-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdefault_sequence_kind.go
More file actions
151 lines (138 loc) · 4.58 KB
/
Copy pathdefault_sequence_kind.go
File metadata and controls
151 lines (138 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package spannerdriver
import (
"context"
"database/sql/driver"
"fmt"
"regexp"
"strings"
"cloud.google.com/go/spanner"
adminapi "cloud.google.com/go/spanner/admin/database/apiv1"
adminpb "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
)
var reMissingDefaultSequenceKind = regexp.MustCompile(`Please specify the sequence kind explicitly or set the database option\s+['\x60]?default_sequence_kind['\x60]?\.`)
func isMissingDefaultSequenceKindError(err error) bool {
if err == nil {
return false
}
return reMissingDefaultSequenceKind.MatchString(err.Error())
}
func (c *conn) executeDDLWithDefaultSequenceKindRetry(ctx context.Context, originalStatements []spanner.Statement, ddlStatements []string) (driver.Result, error) {
op, err := c.adminClient.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{
Database: c.database,
Statements: ddlStatements,
})
var opRetry *adminapi.UpdateDatabaseDdlOperation
var restartIndex int
var retryErr error
if err != nil {
// The RPC execution returned an error.
defaultSequenceKind := propertyDefaultSequenceKind.GetValueOrDefault(c.state)
if defaultSequenceKind != "" && isMissingDefaultSequenceKindError(err) {
if errAlter := c.setDefaultSequenceKind(ctx, defaultSequenceKind); errAlter == nil {
opRetry, retryErr = c.adminClient.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{
Database: c.database,
Statements: ddlStatements,
})
}
}
} else {
c.lastDDLOperationID = op.Name()
err = c.waitForDDLOperation(ctx, op.Name(), func(ctx context.Context) error {
return op.Wait(ctx)
})
if err != nil {
// The long-running operation returned an error.
defaultSequenceKind := propertyDefaultSequenceKind.GetValueOrDefault(c.state)
if defaultSequenceKind != "" && isMissingDefaultSequenceKindError(err) {
if errAlter := c.setDefaultSequenceKind(ctx, defaultSequenceKind); errAlter == nil {
restartIndex = getSuccessCount(op)
if restartIndex < len(ddlStatements) {
opRetry, retryErr = c.adminClient.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{
Database: c.database,
Statements: ddlStatements[restartIndex:],
})
}
}
}
}
}
// If a retry was successfully scheduled
if opRetry != nil && retryErr == nil {
c.lastDDLOperationID = opRetry.Name()
err = c.waitForDDLOperation(ctx, opRetry.Name(), func(ctx context.Context) error {
return opRetry.Wait(ctx)
})
if err == nil {
mode := propertyDDLExecutionMode.GetValueOrDefault(c.state)
if mode == DDLExecutionModeAsync || mode == DDLExecutionModeAsyncWait {
return &result{operationID: opRetry.Name()}, nil
}
return driver.ResultNoRows, nil
}
} else if retryErr != nil {
err = retryErr
}
if err != nil {
if len(originalStatements) > 1 {
be := &BatchError{
Err: err,
BatchUpdateCounts: []int64{},
}
successCount := getSuccessCount(op)
if opRetry != nil {
successCount = restartIndex + getSuccessCount(opRetry)
}
for i := 0; i < successCount; i++ {
be.BatchUpdateCounts = append(be.BatchUpdateCounts, int64(-1))
}
return nil, be
}
return nil, err
}
mode := propertyDDLExecutionMode.GetValueOrDefault(c.state)
if mode == DDLExecutionModeAsync || mode == DDLExecutionModeAsyncWait {
return &result{operationID: op.Name()}, nil
}
return driver.ResultNoRows, nil
}
func (c *conn) setDefaultSequenceKind(ctx context.Context, defaultSequenceKind string) error {
dbID := c.databaseID()
var alterStatement string
if c.parser.Dialect == adminpb.DatabaseDialect_POSTGRESQL {
alterStatement = fmt.Sprintf(`ALTER DATABASE "%s" SET spanner.default_sequence_kind = '%s'`, strings.ReplaceAll(dbID, `"`, `""`), defaultSequenceKind)
} else {
alterStatement = fmt.Sprintf("ALTER DATABASE `%s` SET OPTIONS (default_sequence_kind = '%s')", strings.ReplaceAll(dbID, "`", "``"), defaultSequenceKind)
}
opAlter, errAlter := c.adminClient.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{
Database: c.database,
Statements: []string{alterStatement},
})
if errAlter != nil {
return errAlter
}
return c.waitForDDLOperation(ctx, opAlter.Name(), func(ctx context.Context) error {
return opAlter.Wait(ctx)
})
}
func (c *conn) databaseID() string {
parts := strings.Split(c.database, "/")
return parts[len(parts)-1]
}
func getSuccessCount(op *adminapi.UpdateDatabaseDdlOperation) int {
if op == nil {
return 0
}
metadata, err := op.Metadata()
if err != nil || metadata == nil {
return 0
}
var count int
for _, ts := range metadata.CommitTimestamps {
if ts != nil {
count++
} else {
break
}
}
return count
}