-
Notifications
You must be signed in to change notification settings - Fork 7
/
migrate.ts
executable file
·60 lines (49 loc) · 1.39 KB
/
migrate.ts
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
57
58
59
60
#!/usr/bin/env ts-node
import * as path from 'path'
import { spawn } from 'child_process'
import { env } from 'decentraland-commons'
import { loadEnv, resolvePath } from './utils'
export function migrate(
commandArguments: string[],
migrationsDir: string = __dirname
) {
let CONNECTION_STRING = process.env.CONNECTION_STRING
if (!CONNECTION_STRING) {
loadEnv()
CONNECTION_STRING = env.get('CONNECTION_STRING')
if (!CONNECTION_STRING) {
throw new Error(
'Please set a CONNECTION_STRING env variable before running migrations'
)
}
}
const spawnArgs = [
'--database-url-var',
'CONNECTION_STRING',
'--migration-file-language',
'ts',
'--migrations-dir',
migrationsDir,
'--ignore-pattern',
'\\..*|.*migrate',
...commandArguments
]
const child = spawn(resolvePath(__dirname + '/node-pg-migrate'), spawnArgs, {
env: { ...process.env, CONNECTION_STRING }
})
console.log('Running command:')
console.dir(`node-pg-migrate ${spawnArgs.join(' ')}`)
child.on('error', function(error) {
console.log(error.message)
})
child.stdout.on('data', function(data) {
process.stdout.write(data.toString())
})
child.stderr.on('data', function(data) {
process.stdout.write(data.toString())
})
return child
}
if (require.main === module) {
migrate(process.argv.slice(2), path.resolve(__dirname, '../migrations'))
}