cg / hackerproducts
63 files · 5,740 lines · 159.1 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.
Quick jump: page.tsxpage.tsxglobals.cssschema.sqlpackage.json
src/db/migrate.ts · 32 lines · 948 B← back to folder
| 1 | import fs from "node:fs"; |
| 2 | import path from "node:path"; |
| 3 | import { getPool, readSchemaSqlFile } from "./index"; |
| 4 | |
| 5 | // Load .env.local when run directly via tsx |
| 6 | const envPath = path.join(process.cwd(), ".env.local"); |
| 7 | if (fs.existsSync(envPath)) { |
| 8 | const lines = fs.readFileSync(envPath, "utf8").split("\n"); |
| 9 | for (const line of lines) { |
| 10 | const trimmed = line.trim(); |
| 11 | if (!trimmed || trimmed.startsWith("#")) continue; |
| 12 | const eq = trimmed.indexOf("="); |
| 13 | if (eq === -1) continue; |
| 14 | const key = trimmed.slice(0, eq).trim(); |
| 15 | const val = trimmed.slice(eq + 1).trim().replace(/^["']|["']$/g, ""); |
| 16 | if (!process.env[key]) process.env[key] = val; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | async function main() { |
| 21 | const sql = readSchemaSqlFile(); |
| 22 | const pool = getPool(); |
| 23 | await pool.query(sql); |
| 24 | console.log("Database migrations applied successfully."); |
| 25 | await pool.end(); |
| 26 | } |
| 27 | |
| 28 | main().catch((err) => { |
| 29 | console.error("Migration failed:", err); |
| 30 | process.exit(1); |
| 31 | }); |
| 32 |