-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdrizzle.config.ts
More file actions
56 lines (48 loc) · 1.65 KB
/
Copy pathdrizzle.config.ts
File metadata and controls
56 lines (48 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { defineConfig } from "drizzle-kit"
import { existsSync, readFileSync } from "node:fs"
import { resolve } from "node:path"
const isGenerateCommand = process.argv.some((arg) => arg === "generate")
const databaseUrl = process.env.DATABASE_URL ?? readDatabaseUrlFromEnvLocal()
const migrationsTable = process.env.DRIZZLE_MIGRATIONS_TABLE ?? "__drizzle_migrations"
const migrationsSchema = process.env.DRIZZLE_MIGRATIONS_SCHEMA ?? "drizzle"
if (!databaseUrl && !isGenerateCommand) {
throw new Error("DATABASE_URL is required to run Drizzle migrate commands")
}
export default defineConfig({
schema: "./lib/db/schema.ts",
out: "./drizzle",
dialect: "postgresql",
strict: true,
verbose: true,
migrations: {
table: migrationsTable,
schema: migrationsSchema,
},
dbCredentials: {
url: databaseUrl ?? "postgres://postgres:postgres@127.0.0.1:5432/postgres",
},
})
function readDatabaseUrlFromEnvLocal() {
const envLocalPath = resolve(process.cwd(), ".env.local")
if (!existsSync(envLocalPath)) {
return undefined
}
const contents = readFileSync(envLocalPath, "utf8")
for (const rawLine of contents.split(/\r?\n/)) {
const line = rawLine.trim()
if (!line || line.startsWith("#")) continue
const separatorIndex = line.indexOf("=")
if (separatorIndex === -1) continue
const key = line.slice(0, separatorIndex).trim()
if (key !== "DATABASE_URL") continue
const value = line.slice(separatorIndex + 1).trim()
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
return value.slice(1, -1)
}
return value
}
return undefined
}