fix(prisma): redirect SQLite file target to writeable /tmp path in serverless contexts
This commit is contained in:
@@ -8,7 +8,7 @@ export default defineConfig({
|
|||||||
workers: process.env.CI ? 1 : undefined,
|
workers: process.env.CI ? 1 : undefined,
|
||||||
reporter: 'html',
|
reporter: 'html',
|
||||||
use: {
|
use: {
|
||||||
baseURL: 'https://6a4e32b8782b7f28bdd55a45--mobile2-staging-falah.netlify.app',
|
baseURL: 'https://6a4e33d683bf9228e13b4f35--mobile2-staging-falah.netlify.app',
|
||||||
trace: 'on-first-retry',
|
trace: 'on-first-retry',
|
||||||
video: 'retain-on-failure',
|
video: 'retain-on-failure',
|
||||||
},
|
},
|
||||||
|
|||||||
+38
-3
@@ -1,7 +1,42 @@
|
|||||||
import { PrismaClient } from '@prisma/client'
|
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 (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 }
|
||||||
|
|||||||
Reference in New Issue
Block a user