executeQueries run the set of queries in the provided database connection
(sqlUser *sql.DB, queries []string)
| 431 | |
| 432 | // executeQueries run the set of queries in the provided database connection |
| 433 | func (info InitInfo) executeQueries(sqlUser *sql.DB, queries []string) error { |
| 434 | if len(queries) == 0 { |
| 435 | log.Debug("No queries to execute") |
| 436 | return nil |
| 437 | } |
| 438 | |
| 439 | // The pool pins search_path to pg_catalog. User-authored init scripts |
| 440 | // expect the standard `"$user", public` resolution, so we set that |
| 441 | // once at the start of the batch on a dedicated *sql.Conn and reset |
| 442 | // at the end. Any user SET inside the script persists across |
| 443 | // statements within the same batch, matching PostgreSQL's default |
| 444 | // session semantics. |
| 445 | ctx := context.Background() |
| 446 | conn, err := sqlUser.Conn(ctx) |
| 447 | if err != nil { |
| 448 | return fmt.Errorf("acquiring dedicated connection for init queries: %w", err) |
| 449 | } |
| 450 | defer func() { |
| 451 | // Reset in a defer so the pin is restored even if a query fails; |
| 452 | // pooled pgx connections keep session GUCs across reuse. |
| 453 | if _, resetErr := conn.ExecContext(ctx, `RESET search_path`); resetErr != nil { |
| 454 | log.Error(resetErr, "while resetting search_path after init queries") |
| 455 | } |
| 456 | _ = conn.Close() |
| 457 | }() |
| 458 | |
| 459 | if _, err := conn.ExecContext(ctx, `SET search_path TO "$user", public`); err != nil { |
| 460 | return fmt.Errorf("setting search_path before init queries: %w", err) |
| 461 | } |
| 462 | |
| 463 | for _, sqlQuery := range queries { |
| 464 | log.Debug("Executing query", "sqlQuery", sqlQuery) |
| 465 | if _, err := conn.ExecContext(ctx, sqlQuery); err != nil { |
| 466 | return err |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return nil |
| 471 | } |
| 472 | |
| 473 | // Bootstrap creates and configures this new PostgreSQL instance |
| 474 | func (info InitInfo) Bootstrap(ctx context.Context) error { |
no test coverage detected