This repository has been archived by the owner on Apr 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.ts
105 lines (90 loc) · 2.45 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { flags } from '@oclif/command'
import { cli } from 'cli-ux'
import Command from '../command'
export default class Migrate extends Command {
static description = [
`migrate database to specific version`,
`Runs necessary migrations to reach the target migration version.`
].join('\n')
static examples = [
`$ synor migrate 42`,
`$ synor migrate --from=00 --to=42`,
`$ synor migrate 42 --outOfOrder`
]
static flags = {
...Command.flags,
from: flags.string({
char: 'f',
description: 'from migration version',
dependsOn: ['to']
}),
to: flags.string({
char: 't',
description: 'to migration version',
dependsOn: ['from']
}),
outOfOrder: flags.boolean({
char: 'z',
description: 'include out of order pending migrations',
default: false,
env: 'SYNOR_OUT_OF_ORDER'
})
}
static args: typeof Command.args = [
{
name: 'targetVersion',
description: 'target migration version'
}
]
async run() {
const { args, flags } = this.parse(Migrate)
const { migrator } = this.synor
const targetVersion: string = flags.to || args.targetVersion
if (!targetVersion) {
this.error('Must provide either --to= or TARGETVERSION')
}
let currentVersion: string
migrator
.on('current', record => {
currentVersion = record.version
})
.on('migrate:start', () => {
this.debug('Starting migration...')
})
.on('migrate:end', () => {
this.debug('Finished migration!')
})
.on('migrate:run:start', source => {
cli.action.start(
`Running ${source.version} [${source.type}] ${source.title}`
)
})
.on('migrate:run:end', () => {
cli.action.stop('done!')
})
await migrator.current()
let confirmed = Boolean(flags.from)
if (confirmed && flags.from !== currentVersion!) {
this.error(
`Provided --from=${
flags.from
} but Current Version is ${currentVersion!}`
)
}
if (!confirmed) {
confirmed = await cli.confirm(
[
`Current Version: ${currentVersion!}`,
`Target Version: ${targetVersion}`,
`Continue? (y/n)`
].join('\n')
)
}
if (confirmed) {
await migrator.validate()
await migrator.migrate(targetVersion, { outOfOrder: flags.outOfOrder })
} else {
this.debug('Skipping migrate...')
}
}
}