feat: replace Prisma CLI sync with raw SQL initialization and update Dockerfile dependencies

This commit is contained in:
Michael committed 2026-04-10 10:31:02 +02:00
1 parent 2fd431b907
commit 1de5448e55
4 files changed
+63 -36

No files matched your search

+8 -16
View File
@@ -1,32 +1,24 @@
import { PrismaClient } from '@prisma/client';
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
import { initializeDatabase } from './initDb';
const globalForPrisma = global as unknown as { prisma: PrismaClient };
const createPrismaClient = () => {
try {
const dbUrl = process.env.DATABASE_URL || 'file:/app/prisma/dev.db';
const filePath = dbUrl.replace('file:', '');
console.log(`[DB Init] Attempting to connect to SQLite at: ${filePath}`);
// Check if the URL is valid
if (!filePath) {
throw new Error("DATABASE_URL must be provided and contain a valid path.");
}
console.log(`[DB] Connecting to: ${dbUrl}`);
// Ensure schema exists before creating the Prisma client
initializeDatabase();
const adapter = new PrismaBetterSqlite3({ url: dbUrl });
const client = new PrismaClient({
adapter,
log: ['error', 'warn']
});
const client = new PrismaClient({ adapter });
console.log(`[DB Init] Prisma Client initialized successfully with adapter.`);
console.log('[DB] Prisma client ready.');
return client;
} catch (error) {
console.error(`[DB Init Error] Critical failure during database initialization:`, error);
// Rethrow to let Next.js handle the server component error
console.error('[DB] Critical initialization failure:', error);
throw error;
}
};
+45
View File
@@ -0,0 +1,45 @@
import Database from 'better-sqlite3';
import path from 'path';
/**
* Initializes the SQLite database schema using raw SQL.
* This bypasses Prisma CLI entirely, avoiding CLI environment issues in Docker.
* Safe to run multiple times (CREATE TABLE IF NOT EXISTS).
*/
export function initializeDatabase(): void {
const dbUrl = process.env.DATABASE_URL || 'file:/app/prisma/dev.db';
const filePath = dbUrl.replace(/^file:/, '');
const resolvedPath = path.isAbsolute(filePath)
? filePath
: path.join(process.cwd(), filePath);
console.log(`[DB Init] Ensuring schema at: ${resolvedPath}`);
try {
const db = new Database(resolvedPath);
// Create Config table
db.exec(`
CREATE TABLE IF NOT EXISTS "Config" (
"id" TEXT NOT NULL PRIMARY KEY,
"value" TEXT NOT NULL
);
`);
// Create Button table
db.exec(`
CREATE TABLE IF NOT EXISTS "Button" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"url" TEXT NOT NULL,
"order" INTEGER NOT NULL DEFAULT 0
);
`);
db.close();
console.log('[DB Init] Schema ready.');
} catch (err) {
console.error('[DB Init Error] Failed to initialize schema:', err);
throw err;
}
}