From a4b407c56c64779192b7378de03d51b3a4a79e85 Mon Sep 17 00:00:00 2001 From: Esdras Renan Date: Wed, 19 Nov 2025 15:53:49 -0300 Subject: [PATCH] fix: provide prisma datasource fallback --- prisma.config.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/prisma.config.ts b/prisma.config.ts index c8770dc..bb5d9d3 100644 --- a/prisma.config.ts +++ b/prisma.config.ts @@ -1,7 +1,53 @@ import "dotenv/config" import path from "node:path" -import { defineConfig, env } from "prisma/config" +import { defineConfig } from "prisma/config" + +const PROJECT_ROOT = process.cwd() +const PRISMA_DIR = path.join(PROJECT_ROOT, "prisma") + +function resolveFileUrl(url: string) { + if (!url.startsWith("file:")) { + return url + } + + const filePath = url.slice("file:".length) + + if (filePath.startsWith("//")) { + return url + } + + if (path.isAbsolute(filePath)) { + return `file:${path.normalize(filePath)}` + } + + const normalized = path.normalize(filePath) + const prismaPrefix = `prisma${path.sep}` + const relativeToPrisma = normalized.startsWith(prismaPrefix) + ? normalized.slice(prismaPrefix.length) + : normalized + + const absolutePath = path.resolve(PRISMA_DIR, relativeToPrisma) + + if (!absolutePath.startsWith(PROJECT_ROOT)) { + throw new Error(`DATABASE_URL path escapes project directory: ${filePath}`) + } + + return `file:${absolutePath}` +} + +function normalizeDatasourceUrl(envUrl?: string | null) { + const trimmed = envUrl?.trim() + if (trimmed) { + return resolveFileUrl(trimmed) + } + + if (process.env.NODE_ENV === "production") { + return "file:/app/data/db.sqlite" + } + + return resolveFileUrl("file:./db.dev.sqlite") +} export default defineConfig({ schema: path.join("prisma", "schema.prisma"), @@ -9,6 +55,6 @@ export default defineConfig({ path: path.join("prisma", "migrations"), }, datasource: { - url: env("DATABASE_URL"), + url: normalizeDatasourceUrl(process.env.DATABASE_URL), }, })