|
| 1 | +import path from 'path' |
| 2 | +import { spawnSync } from 'node:child_process' |
| 3 | +import { fileURLToPath, URL } from 'node:url' |
| 4 | + |
| 5 | +import { drizzle } from 'drizzle-orm/postgres-js' |
| 6 | +import { migrate } from 'drizzle-orm/postgres-js/migrator' |
| 7 | +import { eq } from 'drizzle-orm' |
| 8 | +import postgres from 'postgres' |
| 9 | + |
| 10 | +import * as schema from './schema' |
| 11 | +import { getE2EDatabaseUrl } from './e2e-constants' |
| 12 | + |
| 13 | +const databaseUrl = getE2EDatabaseUrl() |
| 14 | + |
| 15 | +// Safeguard: prevent accidentally running e2e migrations against non-local databases |
| 16 | +if (process.env.E2E_DATABASE_URL && process.env.ALLOW_REMOTE_E2E_DATABASE !== 'true') { |
| 17 | + const parsedUrl = new URL(databaseUrl) |
| 18 | + const hostname = parsedUrl.hostname |
| 19 | + |
| 20 | + if (hostname !== 'localhost' && hostname !== '127.0.0.1') { |
| 21 | + console.error( |
| 22 | + `Refusing to run e2e migrations against non-local database host "${hostname}". ` + |
| 23 | + 'Set ALLOW_REMOTE_E2E_DATABASE=true to override.' |
| 24 | + ) |
| 25 | + process.exit(1) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +process.env.E2E_DATABASE_URL = databaseUrl |
| 30 | +process.env.DATABASE_URL = databaseUrl |
| 31 | + |
| 32 | +const here = path.dirname(fileURLToPath(import.meta.url)) |
| 33 | +const composeFile = path.join(here, 'docker-compose.e2e.yml') |
| 34 | + |
| 35 | +const run = (command: string, args: string[]) => { |
| 36 | + const result = spawnSync(command, args, { cwd: here, stdio: 'inherit' }) |
| 37 | + if (result.error) { |
| 38 | + const errno = result.error as NodeJS.ErrnoException |
| 39 | + if (errno.code === 'ENOENT') { |
| 40 | + console.error( |
| 41 | + `Error: '${command}' command not found. Please ensure ${command} is installed and in your PATH.` |
| 42 | + ) |
| 43 | + } else { |
| 44 | + console.error(`Error executing '${command}':`, result.error.message) |
| 45 | + } |
| 46 | + process.exit(1) |
| 47 | + } |
| 48 | + if (result.status !== 0) { |
| 49 | + process.exit(result.status ?? 1) |
| 50 | + } |
| 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 | + |
| 84 | +await waitForPostgres(databaseUrl) |
| 85 | + |
| 86 | +const client = postgres(databaseUrl, { max: 1 }) |
| 87 | +const db = drizzle(client, { schema }) |
| 88 | + |
| 89 | +try { |
| 90 | + await migrate(db, { migrationsFolder: path.join(here, 'migrations') }) |
| 91 | + |
| 92 | + const userEmail = 'e2e@codebuff.com' |
| 93 | + const fallbackUserId = 'e2e-user' |
| 94 | + |
| 95 | + await db |
| 96 | + .insert(schema.user) |
| 97 | + .values({ |
| 98 | + id: fallbackUserId, |
| 99 | + email: userEmail, |
| 100 | + name: 'E2E User', |
| 101 | + handle: 'e2e-user', |
| 102 | + }) |
| 103 | + .onConflictDoNothing() |
| 104 | + |
| 105 | + const [userRow] = await db |
| 106 | + .select({ id: schema.user.id }) |
| 107 | + .from(schema.user) |
| 108 | + .where(eq(schema.user.email, userEmail)) |
| 109 | + .limit(1) |
| 110 | + |
| 111 | + const userId = userRow?.id ?? fallbackUserId |
| 112 | + const publisherId = 'codebuff' |
| 113 | + |
| 114 | + await db |
| 115 | + .insert(schema.publisher) |
| 116 | + .values({ |
| 117 | + id: publisherId, |
| 118 | + name: 'Codebuff', |
| 119 | + verified: true, |
| 120 | + user_id: userId, |
| 121 | + created_by: userId, |
| 122 | + }) |
| 123 | + .onConflictDoNothing() |
| 124 | + |
| 125 | + await db |
| 126 | + .insert(schema.agentConfig) |
| 127 | + .values({ |
| 128 | + id: 'base', |
| 129 | + version: '1.2.3', |
| 130 | + publisher_id: publisherId, |
| 131 | + data: { |
| 132 | + name: 'Base', |
| 133 | + description: 'desc', |
| 134 | + tags: ['test'], |
| 135 | + }, |
| 136 | + }) |
| 137 | + .onConflictDoNothing() |
| 138 | + |
| 139 | + console.log('E2E database setup completed successfully') |
| 140 | +} finally { |
| 141 | + await client.end() |
| 142 | +} |
0 commit comments