forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-cli.ts
More file actions
49 lines (44 loc) · 1.54 KB
/
Copy pathmigrate-cli.ts
File metadata and controls
49 lines (44 loc) · 1.54 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
import { drizzle } from 'drizzle-orm/postgres-js'
import { migrate } from 'drizzle-orm/postgres-js/migrator'
import postgres from 'postgres'
/**
* Applies pending migrations. Safe to run repeatedly — drizzle records what it
* has applied and skips those.
*
* Loads .env when present for local use; on a platform the environment is
* already set, so a missing file is not an error.
*/
try {
process.loadEnvFile()
} catch {
// No .env — expected on Railway and in CI.
}
const url = process.env.DATABASE_URL
if (!url) {
console.error('DATABASE_URL is not set.')
process.exit(1)
}
/**
* Supabase's transaction pooler does not support prepared statements or hold a
* session across statements, so DDL through it fails in ways that read as
* random. Migrations need the session pooler or a direct connection.
*/
if (url.includes(':6543')) {
console.error(
'DATABASE_URL points at the transaction pooler (:6543).\n' +
'Use the session pooler or direct connection (:5432) — same host, change the port.',
)
process.exit(1)
}
// Drizzle creates its tracking table with IF NOT EXISTS, which emits NOTICEs on
// every run after the first. They are not errors and would be noise in CI.
const sql = postgres(url, { max: 1, connect_timeout: 15, onnotice: () => {} })
try {
await migrate(drizzle(sql), { migrationsFolder: './drizzle' })
console.log('migrations applied')
} catch (err) {
console.error('migration failed:', err instanceof Error ? err.message : String(err))
process.exitCode = 1
} finally {
await sql.end({ timeout: 5 })
}