28 lines
925 B
JavaScript
28 lines
925 B
JavaScript
import { mkdirSync, readFileSync, rmSync } from 'node:fs';
|
|
import { dirname, resolve } from 'node:path';
|
|
import { DatabaseSync } from 'node:sqlite';
|
|
|
|
function databasePathFromUrl(url) {
|
|
if (!url?.startsWith('file:')) {
|
|
throw new Error('DATABASE_URL must use SQLite file: URL');
|
|
}
|
|
const rawPath = url.slice('file:'.length);
|
|
if (rawPath.startsWith('/')) {
|
|
return rawPath;
|
|
}
|
|
return resolve('prisma', rawPath);
|
|
}
|
|
|
|
const databasePath = databasePathFromUrl(process.env.DATABASE_URL ?? 'file:./dev.db');
|
|
mkdirSync(dirname(databasePath), { recursive: true });
|
|
rmSync(databasePath, { force: true });
|
|
rmSync(`${databasePath}-journal`, { force: true });
|
|
|
|
const sql = readFileSync(resolve('prisma/migrations/202606280001_init/migration.sql'), 'utf8');
|
|
const db = new DatabaseSync(databasePath);
|
|
db.exec('PRAGMA foreign_keys = ON;');
|
|
db.exec(sql);
|
|
db.close();
|
|
|
|
console.log(`SQLite schema applied to ${databasePath}`);
|