EnsureConn initializes the database connection pool if not already done. This is useful for database-only mode where we need to connect before analyzing queries.
(ctx context.Context, migrations []string)
| 485 | // EnsureConn initializes the database connection pool if not already done. |
| 486 | // This is useful for database-only mode where we need to connect before analyzing queries. |
| 487 | func (a *Analyzer) EnsureConn(ctx context.Context, migrations []string) error { |
| 488 | if a.pool != nil { |
| 489 | return nil |
| 490 | } |
| 491 | |
| 492 | var uri string |
| 493 | if a.db.Managed { |
| 494 | if a.client == nil { |
| 495 | return fmt.Errorf("client is nil") |
| 496 | } |
| 497 | edb, err := a.client.CreateDatabase(ctx, &dbmanager.CreateDatabaseRequest{ |
| 498 | Engine: "postgresql", |
| 499 | Migrations: migrations, |
| 500 | }) |
| 501 | if err != nil { |
| 502 | return err |
| 503 | } |
| 504 | uri = edb.Uri |
| 505 | } else if a.dbg.OnlyManagedDatabases { |
| 506 | return fmt.Errorf("database: connections disabled via SQLCDEBUG=databases=managed") |
| 507 | } else { |
| 508 | uri = a.replacer.Replace(a.db.URI) |
| 509 | } |
| 510 | |
| 511 | conf, err := pgxpool.ParseConfig(uri) |
| 512 | if err != nil { |
| 513 | return err |
| 514 | } |
| 515 | pool, err := pgxpool.NewWithConfig(ctx, conf) |
| 516 | if err != nil { |
| 517 | return err |
| 518 | } |
| 519 | a.pool = pool |
| 520 | return nil |
| 521 | } |
| 522 | |
| 523 | // GetColumnNames implements the expander.ColumnGetter interface. |
| 524 | // It prepares a query and returns the column names from the result set description. |
nothing calls this directly
no test coverage detected