MCPcopy
hub / github.com/sqlc-dev/sqlc / fetchDatabaseUri

Method fetchDatabaseUri

internal/cmd/vet.go:407–459  ·  view source on GitHub ↗
(ctx context.Context, s config.SQL)

Source from the content-addressed store, hash-verified

405}
406
407func (c *checker) fetchDatabaseUri(ctx context.Context, s config.SQL) (string, func() error, error) {
408 cleanup := func() error {
409 return nil
410 }
411
412 if s.Database == nil {
413 panic("fetch database URI called with nil database")
414 }
415 if !s.Database.Managed {
416 uri, err := c.DSN(s.Database.URI)
417 return uri, cleanup, err
418 }
419
420 // Initialize the client exactly once, even if called concurrently
421 c.clientOnce.Do(func() {
422 c.Client = dbmanager.NewClient(c.Conf.Servers)
423 })
424
425 var ddl []string
426 files, err := sqlpath.Glob(s.Schema)
427 if err != nil {
428 return "", cleanup, err
429 }
430 for _, schema := range files {
431 contents, err := os.ReadFile(schema)
432 if err != nil {
433 return "", cleanup, fmt.Errorf("read file: %w", err)
434 }
435 ddl = append(ddl, migrations.RemoveRollbackStatements(string(contents)))
436 }
437
438 resp, err := c.Client.CreateDatabase(ctx, &dbmanager.CreateDatabaseRequest{
439 Engine: string(s.Engine),
440 Migrations: ddl,
441 })
442 if err != nil {
443 return "", cleanup, fmt.Errorf("managed: create database: %w", err)
444 }
445
446 var uri string
447 switch s.Engine {
448 case config.EngineMySQL:
449 dburi, err := quickdb.MySQLReformatURI(resp.Uri)
450 if err != nil {
451 return "", cleanup, fmt.Errorf("reformat uri: %w", err)
452 }
453 uri = dburi
454 default:
455 uri = resp.Uri
456 }
457
458 return uri, cleanup, nil
459}
460
461func (c *checker) DSN(dsn string) (string, error) {
462 return c.Replacer.Replace(dsn), nil

Callers 1

checkSQLMethod · 0.95

Calls 6

DSNMethod · 0.95
NewClientFunction · 0.92
GlobFunction · 0.92
RemoveRollbackStatementsFunction · 0.92
MySQLReformatURIFunction · 0.92
CreateDatabaseMethod · 0.65

Tested by

no test coverage detected