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

Function waitForPostgres

internal/sqltest/native/postgres.go:192–221  ·  view source on GitHub ↗
(ctx context.Context, uri string, timeout time.Duration)

Source from the content-addressed store, hash-verified

190}
191
192func waitForPostgres(ctx context.Context, uri string, timeout time.Duration) error {
193 deadline := time.Now().Add(timeout)
194 ticker := time.NewTicker(100 * time.Millisecond)
195 defer ticker.Stop()
196
197 var lastErr error
198 for {
199 select {
200 case <-ctx.Done():
201 return fmt.Errorf("context cancelled: %w (last error: %v)", ctx.Err(), lastErr)
202 case <-ticker.C:
203 if time.Now().After(deadline) {
204 return fmt.Errorf("timeout waiting for PostgreSQL (last error: %v)", lastErr)
205 }
206 conn, err := pgx.Connect(ctx, uri)
207 if err != nil {
208 lastErr = err
209 slog.Debug("native/postgres", "connect-attempt", err)
210 continue
211 }
212 if err := conn.Ping(ctx); err != nil {
213 lastErr = err
214 conn.Close(ctx)
215 continue
216 }
217 conn.Close(ctx)
218 return nil
219 }
220 }
221}

Callers 1

startPostgreSQLServerFunction · 0.85

Calls 3

CloseMethod · 0.65
AddMethod · 0.45
ErrMethod · 0.45

Tested by

no test coverage detected