From e2f8d7be66704eb6ed9dfe1723563198dbe90042 Mon Sep 17 00:00:00 2001 From: FalahMobile Date: Wed, 8 Jul 2026 19:28:55 +0800 Subject: [PATCH] fix(prisma): redirect SQLite file target to writeable /tmp path in serverless contexts --- playwright.config.ts | 2 +- src/lib/prisma.ts | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/playwright.config.ts b/playwright.config.ts index fb66be9..3fd1758 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -8,7 +8,7 @@ export default defineConfig({ workers: process.env.CI ? 1 : undefined, reporter: 'html', use: { - baseURL: 'https://6a4e32b8782b7f28bdd55a45--mobile2-staging-falah.netlify.app', + baseURL: 'https://6a4e33d683bf9228e13b4f35--mobile2-staging-falah.netlify.app', trace: 'on-first-retry', video: 'retain-on-failure', }, diff --git a/src/lib/prisma.ts b/src/lib/prisma.ts index 8d439f8..cd37c50 100644 --- a/src/lib/prisma.ts +++ b/src/lib/prisma.ts @@ -1,7 +1,42 @@ import { PrismaClient } from '@prisma/client' +import fs from 'fs' +import path from 'path' -const globalForPrisma = globalThis as unknown as { prisma: PrismaClient } +let prisma: PrismaClient -export const prisma = globalForPrisma.prisma ?? new PrismaClient() +if (process.env.LAMBDA_TASK_ROOT || process.env.NETLIFY) { + const targetDbPath = '/tmp/dev.db' + + if (!fs.existsSync(targetDbPath)) { + let sourceDbPath = path.join(process.cwd(), 'prisma', 'dev.db') + if (!fs.existsSync(sourceDbPath)) { + sourceDbPath = path.join(process.cwd(), 'dev.db') + } + + try { + if (fs.existsSync(sourceDbPath)) { + fs.copyFileSync(sourceDbPath, targetDbPath) + fs.chmodSync(targetDbPath, 0o666) + console.log(`Copied database to writeable path: ${targetDbPath}`) + } else { + console.error(`Database template not found at: ${sourceDbPath}`) + } + } catch (e) { + console.error('Error copying SQLite database to /tmp:', e) + } + } -if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma + prisma = new PrismaClient({ + datasources: { + db: { + url: `file:${targetDbPath}` + } + } + }) +} else { + const globalForPrisma = globalThis as unknown as { prisma: PrismaClient } + prisma = globalForPrisma.prisma ?? new PrismaClient() + if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma +} + +export { prisma }