update doc and optimize
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
// Generates prisma/schema.sqlite.prisma from the canonical schema.prisma by
|
||||
// swapping only the datasource provider to sqlite. Keeping a single source of
|
||||
// truth for the models avoids drift; the sqlite schema is used for local tests
|
||||
// (PrismaClient with a mysql provider cannot use a `file:` URL).
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
const src = resolve('prisma/schema.prisma');
|
||||
const dest = resolve('prisma/schema.sqlite.prisma');
|
||||
|
||||
const original = readFileSync(src, 'utf8');
|
||||
if (!/provider\s*=\s*"mysql"/.test(original)) {
|
||||
throw new Error('Expected datasource provider "mysql" in schema.prisma');
|
||||
}
|
||||
const sqlite = original.replace(/provider\s*=\s*"mysql"/, 'provider = "sqlite"');
|
||||
const banner = '// AUTO-GENERATED from schema.prisma by scripts/make-sqlite-schema.mjs — do not edit.\n';
|
||||
writeFileSync(dest, banner + sqlite);
|
||||
console.log(`Wrote ${dest} (sqlite provider) from schema.prisma`);
|
||||
@@ -1,27 +0,0 @@
|
||||
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}`);
|
||||
Reference in New Issue
Block a user