-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbsetup.js
More file actions
executable file
·41 lines (35 loc) · 1.17 KB
/
dbsetup.js
File metadata and controls
executable file
·41 lines (35 loc) · 1.17 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
#!/usr/bin/env node
const { spawn } = require('node:child_process')
const path = require('node:path')
const fs = require('node:fs')
const env = { ...process.env }
;(async() => {
// place Sqlite3 database on volume
const source = path.resolve('./dev.db')
const target = '/data/' + path.basename(source)
if (!fs.existsSync(source) && fs.existsSync('/data')) fs.symlinkSync(target, source)
const newDb = !fs.existsSync(target)
if (newDb && process.env.BUCKET_NAME) {
await exec(`npx litestream restore -config litestream.yml -if-replica-exists ${target}`)
}
// prepare database
await exec('npx prisma migrate deploy')
// launch application
if (process.env.BUCKET_NAME) {
await exec(`npx litestream replicate -config litestream.yml -exec ${JSON.stringify(process.argv.slice(2).join(' '))}`)
} else {
await exec(process.argv.slice(2).join(' '))
}
})()
function exec(command) {
const child = spawn(command, { shell: true, stdio: 'inherit', env })
return new Promise((resolve, reject) => {
child.on('exit', code => {
if (code === 0) {
resolve()
} else {
reject(new Error(`${command} failed rc=${code}`))
}
})
})
}