( url: string, maxAttempts = 30, delayMs = 1000 )
| 51 | } |
| 52 | |
| 53 | const waitForPostgres = async ( |
| 54 | url: string, |
| 55 | maxAttempts = 30, |
| 56 | delayMs = 1000 |
| 57 | ): Promise<void> => { |
| 58 | for (let attempt = 1; attempt <= maxAttempts; attempt++) { |
| 59 | let testClient: ReturnType<typeof postgres> | null = null |
| 60 | try { |
| 61 | testClient = postgres(url, { max: 1, connect_timeout: 5 }) |
| 62 | await testClient`SELECT 1` |
| 63 | return |
| 64 | } catch (error) { |
| 65 | if (attempt === maxAttempts) { |
| 66 | throw new Error( |
| 67 | `Failed to connect to Postgres after ${maxAttempts} attempts: ${error}` |
| 68 | ) |
| 69 | } |
| 70 | console.log( |
| 71 | `Waiting for Postgres to be ready... (attempt ${attempt}/${maxAttempts})` |
| 72 | ) |
| 73 | await new Promise((resolve) => setTimeout(resolve, delayMs)) |
| 74 | } finally { |
| 75 | if (testClient) { |
| 76 | await testClient.end() |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | run('docker', ['compose', '-f', composeFile, 'up', '-d', '--wait']) |
| 83 |
no test coverage detected