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

Function waitForMySQL

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

Source from the content-addressed store, hash-verified

162}
163
164func waitForMySQL(ctx context.Context, uri string, timeout time.Duration) error {
165 deadline := time.Now().Add(timeout)
166 ticker := time.NewTicker(500 * time.Millisecond)
167 defer ticker.Stop()
168
169 // Make an immediate first attempt before waiting for the ticker
170 if err := tryMySQLConnection(ctx, uri); err == nil {
171 return nil
172 }
173
174 var lastErr error
175 for {
176 select {
177 case <-ctx.Done():
178 return fmt.Errorf("context cancelled: %w (last error: %v)", ctx.Err(), lastErr)
179 case <-ticker.C:
180 if time.Now().After(deadline) {
181 return fmt.Errorf("timeout waiting for MySQL (last error: %v)", lastErr)
182 }
183 if err := tryMySQLConnection(ctx, uri); err != nil {
184 lastErr = err
185 continue
186 }
187 return nil
188 }
189 }
190}
191
192func tryMySQLConnection(ctx context.Context, uri string) error {
193 db, err := sql.Open("mysql", uri)

Callers 1

startMySQLServerFunction · 0.70

Calls 3

tryMySQLConnectionFunction · 0.85
AddMethod · 0.45
ErrMethod · 0.45

Tested by

no test coverage detected