Switch to using in-mem sequence generator only when explicitly configured in connection string. Move spanner-related functions to pkg/util/spanner. (#103363)

* Switch to using in-mem sequence generator only when explicitly configured in connection string.
Move spanner-related functions to pkg/util/spanner.
This commit is contained in:
Peter Štibraný
2025-04-08 16:36:37 +02:00
committed by GitHub
parent 6e134ed974
commit 4efdef9b9c
5 changed files with 46 additions and 39 deletions
+2 -36
View File
@@ -11,10 +11,7 @@ import (
spannerclient "cloud.google.com/go/spanner"
_ "github.com/googleapis/go-sql-spanner"
spannerdriver "github.com/googleapis/go-sql-spanner"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"xorm.io/core"
)
@@ -386,10 +383,8 @@ func (s *spanner) CreateSequenceGenerator(db *sql.DB) (SequenceGenerator, error)
return nil, err
}
if UsePlainText(connectorConfig) {
// Plain-text means we're either using spannertest or Spanner emulator.
// Switch to fake in-memory sequence number generator in that case.
//
if connectorConfig.Params["inMemSequenceGenerator"] == "true" {
// Switch to using in-memory sequence number generator.
// Using database-based sequence generator doesn't work with emulator, as emulator
// only supports single transaction. If there is already another transaction started
// generating new ID via database-based sequence generator would always fail.
@@ -399,35 +394,6 @@ func (s *spanner) CreateSequenceGenerator(db *sql.DB) (SequenceGenerator, error)
return newSequenceGenerator(db), nil
}
func UsePlainText(connectorConfig spannerdriver.ConnectorConfig) bool {
if strval, ok := connectorConfig.Params["useplaintext"]; ok {
if val, err := strconv.ParseBool(strval); err == nil {
return val
}
}
return false
}
// SpannerConnectorConfigToClientOptions is adapted from https://github.com/googleapis/go-sql-spanner/blob/main/driver.go#L341-L477, from version 1.11.1.
func SpannerConnectorConfigToClientOptions(connectorConfig spannerdriver.ConnectorConfig) []option.ClientOption {
var opts []option.ClientOption
if connectorConfig.Host != "" {
opts = append(opts, option.WithEndpoint(connectorConfig.Host))
}
if strval, ok := connectorConfig.Params["credentials"]; ok {
opts = append(opts, option.WithCredentialsFile(strval))
}
if strval, ok := connectorConfig.Params["credentialsjson"]; ok {
opts = append(opts, option.WithCredentialsJSON([]byte(strval)))
}
if UsePlainText(connectorConfig) {
opts = append(opts,
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
option.WithoutAuthentication())
}
return opts
}
func (s *spanner) RetryOnError(err error) bool {
return err != nil && spannerclient.ErrCode(spannerclient.ToSpannerError(err)) == codes.Aborted
}