Spaces:
Sleeping
Sleeping
Hetansh Waghela
Day 3: legal grounding β curated seeds, pgvector+FTS retrieval, literal-quote verifier, cited route plans
2e15351 | import { drizzle } from "drizzle-orm/node-postgres"; | |
| import { migrate } from "drizzle-orm/node-postgres/migrator"; | |
| import { sql } from "drizzle-orm"; | |
| import { Pool } from "pg"; | |
| import { readFileSync } from "node:fs"; | |
| import { fileURLToPath } from "node:url"; | |
| import { dirname, join, resolve } from "node:path"; | |
| const __dirname = dirname(fileURLToPath(import.meta.url)); | |
| // Auto-load infra/.env if DATABASE_URL is not already set. | |
| if (!process.env["DATABASE_URL"]) { | |
| const envPath = resolve(__dirname, "../../../infra/.env"); | |
| try { | |
| const raw = readFileSync(envPath, "utf8"); | |
| for (const line of raw.split("\n")) { | |
| const trimmed = line.trim(); | |
| if (!trimmed || trimmed.startsWith("#")) continue; | |
| const eqIdx = trimmed.indexOf("="); | |
| if (eqIdx === -1) continue; | |
| const key = trimmed.slice(0, eqIdx).trim(); | |
| const val = trimmed.slice(eqIdx + 1).trim(); | |
| if (!process.env[key]) process.env[key] = val; | |
| } | |
| } catch { | |
| // Env not found β will fail below with a clear error message. | |
| } | |
| } | |
| async function main() { | |
| const connectionString = process.env.DATABASE_URL; | |
| if (!connectionString) throw new Error("DATABASE_URL is not set"); | |
| const pool = new Pool({ connectionString }); | |
| const db = drizzle(pool); | |
| // pgvector must exist before the legal_chunks vector column migration runs. | |
| await db.execute(sql`CREATE EXTENSION IF NOT EXISTS vector`); | |
| await migrate(db, { migrationsFolder: join(__dirname, "migrations") }); | |
| console.log("migrations applied"); | |
| await pool.end(); | |
| } | |
| main().catch((err) => { | |
| console.error(err); | |
| process.exit(1); | |
| }); | |