This repository was 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
/
Copy pathvalidate.ts
123 lines (109 loc) · 3.19 KB
/
validate.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { color } from '@oclif/color'
import { isSynorError } from '@synor/core'
import { cli } from 'cli-ux'
import Command from '../command'
import { getFormattedDate } from '../utils/get-formatted-date'
type MigrationRecord = import('@synor/core').MigrationRecord
export default class Validate extends Command {
static description = [
`validate migration records`,
`Validates the records for migrations that are currently applied.`
].join('\n')
static examples = [`$ synor validate`]
static flags = {
extended: cli.table.Flags.extended,
...Command.flags
}
static args = []
async run() {
const { flags } = this.parse(Validate)
const { migrator } = this.synor
const recordById: Record<
number,
MigrationRecord & {
status: '...' | 'valid' | 'dirty' | 'hash_mismatch'
}
> = {}
let isInvalid = true
migrator
.on('validate:run:start', async record => {
recordById[record.id] = { ...record, status: '...' }
})
.on('validate:run:end', async record => {
recordById[record.id].status = 'valid'
})
.on('validate:error', (error, record) => {
if (
isSynorError(error, 'dirty') ||
isSynorError(error, 'hash_mismatch')
) {
recordById[record.id].status = error.type
} else {
throw error
}
})
.on('validate:end', async () => {
const records = Object.values(recordById)
cli.table(
records,
{
id: {
header: 'ID',
extended: true
},
version: {
header: 'Version',
get: row =>
row.status === 'valid'
? color.green(row.version)
: color.red(row.version)
},
type: {
header: 'Type'
},
title: {
header: 'Title'
},
hash: {
header: 'Hash',
get: row =>
row.status === 'hash_mismatch'
? color.red(row.hash)
: color.green(row.hash)
},
appliedAt: {
header: 'AppliedAt',
get: row => color.reset(getFormattedDate(row.appliedAt)),
extended: true
},
appliedBy: {
header: 'AppliedBy',
get: record => color.reset(record.appliedBy || 'N/A'),
extended: true
},
executionTime: {
header: 'ExecutionTime',
get: row =>
color.reset(`${Number(row.executionTime / 1000).toFixed(2)}s`),
extended: true
},
status: {
header: 'Status',
get: row =>
row.status === 'valid'
? color.green(row.status)
: color.red(row.status)
}
},
{
extended: flags.extended
}
)
isInvalid = records.some(record => record.status !== 'valid')
})
await migrator.validate()
if (isInvalid) {
this.error('Validation Error', { exit: 1 })
}
}
}