automatically run migrations
This commit is contained in:
parent
b3025bb084
commit
64e33e4e79
1 changed files with 32 additions and 2 deletions
34
src/db/db.ts
34
src/db/db.ts
|
|
@ -1,8 +1,38 @@
|
||||||
|
import fs from "node:fs";
|
||||||
import { PrismaClient } from "@prisma/client";
|
import { PrismaClient } from "@prisma/client";
|
||||||
|
import { execSync } from "node:child_process";
|
||||||
|
|
||||||
|
// ensure db exists
|
||||||
|
if(!fs.existsSync("./data/mydb.sqlite")) {
|
||||||
|
// run bun prisma migrate deploy with child_process
|
||||||
|
console.log("Database not found, creating a new one...");
|
||||||
|
execSync("bun prisma migrate deploy");
|
||||||
|
}
|
||||||
|
|
||||||
|
// The db version before we switched to Prisma
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
|
const legacyVersion = await prisma.$queryRaw<{ user_version: bigint }[]>`PRAGMA user_version;`;
|
||||||
|
if (legacyVersion[0]?.user_version === 1n) {
|
||||||
|
// close prisma connection
|
||||||
|
await prisma.$disconnect();
|
||||||
|
// Existing legacy database found, needs migration
|
||||||
|
console.log("Legacy database found. Skipping initial migration...");
|
||||||
|
execSync("bun prisma migrate resolve --applied 0_init");
|
||||||
|
// reconnect prisma
|
||||||
|
await prisma.$connect();
|
||||||
|
// set user_version to 2
|
||||||
|
await prisma.$executeRaw`PRAGMA user_version = 2;`;
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize the database schema using Prisma migrations instead of raw SQL.
|
console.log("Running database migrations...");
|
||||||
// Ensure to run `npx prisma migrate dev` to apply the schema changes.
|
|
||||||
|
// run any pending migrations
|
||||||
|
await prisma.$disconnect();
|
||||||
|
execSync("bun prisma migrate deploy");
|
||||||
|
await prisma.$connect();
|
||||||
|
|
||||||
|
await prisma.$queryRaw`PRAGMA journal_mode = WAL;`.catch((e) => {
|
||||||
|
console.error("Failed to set journal mode to WAL:", e);
|
||||||
|
});
|
||||||
|
|
||||||
export default prisma;
|
export default prisma;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue