MCPcopy Create free account
hub / github.com/cloudspannerecosystem/spanner-cli / DatabaseExists

Method DatabaseExists

session.go:329–351  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

327}
328
329func (s *Session) DatabaseExists() (bool, error) {
330 // For users who don't have `spanner.databases.get` IAM permission,
331 // check database existence by running an actual query.
332 // cf. https://github.com/cloudspannerecosystem/spanner-cli/issues/10
333 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
334 defer cancel()
335 stmt := spanner.NewStatement("SELECT 1")
336 iter := s.client.Single().QueryWithOptions(ctx, stmt, spanner.QueryOptions{Priority: s.currentPriority()})
337 defer iter.Stop()
338
339 _, err := iter.Next()
340 if err == nil {
341 return true, nil
342 }
343 switch spanner.ErrCode(err) {
344 case codes.NotFound:
345 return false, nil
346 case codes.InvalidArgument:
347 return false, nil
348 default:
349 return false, fmt.Errorf("checking database existence failed: %v", err)
350 }
351}
352
353// RecreateClient closes the current client and creates a new client for the session.
354func (s *Session) RecreateClient() error {

Callers 1

RunInteractiveMethod · 0.80

Calls 1

currentPriorityMethod · 0.95

Tested by

no test coverage detected