(ctx context.Context, s config.SQL)
| 405 | } |
| 406 | |
| 407 | func (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 | |
| 461 | func (c *checker) DSN(dsn string) (string, error) { |
| 462 | return c.Replacer.Replace(dsn), nil |
no test coverage detected