fix: provide prisma datasource fallback
This commit is contained in:
parent
81eca14de4
commit
a4b407c56c
1 changed files with 48 additions and 2 deletions
|
|
@ -1,7 +1,53 @@
|
||||||
import "dotenv/config"
|
import "dotenv/config"
|
||||||
import path from "node:path"
|
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({
|
export default defineConfig({
|
||||||
schema: path.join("prisma", "schema.prisma"),
|
schema: path.join("prisma", "schema.prisma"),
|
||||||
|
|
@ -9,6 +55,6 @@ export default defineConfig({
|
||||||
path: path.join("prisma", "migrations"),
|
path: path.join("prisma", "migrations"),
|
||||||
},
|
},
|
||||||
datasource: {
|
datasource: {
|
||||||
url: env("DATABASE_URL"),
|
url: normalizeDatasourceUrl(process.env.DATABASE_URL),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue