Files
Makers3D/apps/api/src/scripts/migrate.ts
T

32 lines
835 B
TypeScript

import { readFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { pool } from "../db.js";
const currentDir = dirname(fileURLToPath(import.meta.url));
const schemaCandidates = [
join(currentDir, "../db/schema.sql"),
join(process.cwd(), "apps/api/src/db/schema.sql"),
join(process.cwd(), "dist/apps/api/src/db/schema.sql")
];
let sql: string | undefined;
for (const schemaPath of schemaCandidates) {
try {
sql = await readFile(schemaPath, "utf8");
break;
} catch {
// Try the next known location. The runtime container keeps the schema outside dist.
}
}
if (!sql) {
throw new Error(`Schema file not found. Checked: ${schemaCandidates.join(", ")}`);
}
await pool.query(sql);
console.log("Database schema applied");
await pool.end();