From 1de5448e55e58aed1476035bd39f4c9d9a58f17b Mon Sep 17 00:00:00 2001 From: R0m1k3 Date: Fri, 10 Apr 2026 10:31:02 +0200 Subject: [PATCH] feat: replace Prisma CLI sync with raw SQL initialization and update Dockerfile dependencies --- Dockerfile | 5 ++++- entrypoint.sh | 25 ++++++------------------- lib/db.ts | 24 ++++++++---------------- lib/initDb.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 lib/initDb.ts diff --git a/Dockerfile b/Dockerfile index 03a9a72..2bb2821 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,11 +46,14 @@ COPY --from=builder /app/.next/static ./.next/static COPY --from=builder /app/prisma ./prisma COPY --from=builder /app/prisma.config.ts ./prisma.config.ts -# Explicitly copy Prisma and native node_modules (required for standalone mode) +# Explicitly copy all native/prisma node_modules (required for standalone mode) COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma COPY --from=builder /app/node_modules/better-sqlite3 ./node_modules/better-sqlite3 COPY --from=builder /app/node_modules/bcrypt ./node_modules/bcrypt +COPY --from=builder /app/node_modules/bindings ./node_modules/bindings +COPY --from=builder /app/node_modules/file-uri-to-path ./node_modules/file-uri-to-path +COPY --from=builder /app/node_modules/node-addon-api ./node_modules/node-addon-api # Ensure prisma directory is accessible and writable for SQLite RUN mkdir -p /app/prisma && chmod -R 777 /app/prisma diff --git a/entrypoint.sh b/entrypoint.sh index 8658b1d..268cf52 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,29 +1,16 @@ #!/bin/sh - -# Exit immediately if a command exits with a non-zero status set -e echo "=== Menu Professional Entrypoint ===" echo "Node version: $(node -v)" -echo "Working directory: $(pwd)" +echo "DATABASE_URL: ${DATABASE_URL}" -# Check permissions -echo "Checking prisma directory permissions..." -ls -ld /app/prisma -touch /app/prisma/test_write && echo "Write check: SUCCESS" && rm /app/prisma/test_write || echo "Write check: FAILED" +# Ensure the prisma directory exists and is writable +mkdir -p /app/prisma +chmod -R 777 /app/prisma -echo "Locating Prisma engine binary..." -find ./node_modules -name "libquery_engine*" || echo "Engine binary NOT found!" +echo "Write check on /app/prisma..." +touch /app/prisma/.write_test && echo "OK" && rm /app/prisma/.write_test -# Run database synchronization -echo "Synchronizing database schema with Prisma..." -# Ensure DATABASE_URL is set for the CLI -export DATABASE_URL=${DATABASE_URL:-"file:/app/prisma/dev.db"} -npx prisma db push --accept-data-loss || echo "Database sync failed, but proceeding..." - -# Show database file if exists -ls -l /app/prisma/dev.db || echo "Database file dev.db not yet created" - -# Start the application echo "Launching server on port ${PORT:-3214}..." exec node server.js diff --git a/lib/db.ts b/lib/db.ts index e044937..16170f2 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -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; } }; diff --git a/lib/initDb.ts b/lib/initDb.ts new file mode 100644 index 0000000..e30a00e --- /dev/null +++ b/lib/initDb.ts @@ -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; + } +}