(c context.Context)
| 39 | } |
| 40 | |
| 41 | func startPostgreSQLServer(c context.Context) (string, error) { |
| 42 | { |
| 43 | _, err := exec.Command("docker", "pull", "postgres:16").CombinedOutput() |
| 44 | if err != nil { |
| 45 | return "", fmt.Errorf("docker pull: postgres:16 %w", err) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | uri := "postgres://postgres:mysecretpassword@localhost:5432/postgres?sslmode=disable" |
| 50 | |
| 51 | var exists bool |
| 52 | { |
| 53 | cmd := exec.Command("docker", "container", "inspect", "sqlc_sqltest_docker_postgres") |
| 54 | // This means we've already started the container |
| 55 | exists = cmd.Run() == nil |
| 56 | } |
| 57 | |
| 58 | if !exists { |
| 59 | cmd := exec.Command("docker", "run", |
| 60 | "--name", "sqlc_sqltest_docker_postgres", |
| 61 | "-e", "POSTGRES_PASSWORD=mysecretpassword", |
| 62 | "-e", "POSTGRES_USER=postgres", |
| 63 | "-p", "5432:5432", |
| 64 | "-d", |
| 65 | "postgres:16", |
| 66 | "-c", "max_connections=200", |
| 67 | ) |
| 68 | |
| 69 | output, err := cmd.CombinedOutput() |
| 70 | fmt.Println(string(output)) |
| 71 | |
| 72 | msg := `Conflict. The container name "/sqlc_sqltest_docker_postgres" is already in use by container` |
| 73 | if !strings.Contains(string(output), msg) && err != nil { |
| 74 | return "", err |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | ctx, cancel := context.WithTimeout(c, 5*time.Second) |
| 79 | defer cancel() |
| 80 | |
| 81 | // Create a ticker that fires every 10ms |
| 82 | ticker := time.NewTicker(10 * time.Millisecond) |
| 83 | defer ticker.Stop() |
| 84 | |
| 85 | for { |
| 86 | select { |
| 87 | case <-ctx.Done(): |
| 88 | return "", fmt.Errorf("timeout reached: %w", ctx.Err()) |
| 89 | |
| 90 | case <-ticker.C: |
| 91 | // Run your function here |
| 92 | conn, err := pgx.Connect(ctx, uri) |
| 93 | if err != nil { |
| 94 | slog.Debug("sqltest", "connect", err) |
| 95 | continue |
| 96 | } |
| 97 | defer conn.Close(ctx) |
| 98 | if err := conn.Ping(ctx); err != nil { |
no test coverage detected