/
up.js
179 lines (154 loc) · 4.49 KB
/
up.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import fs from 'fs'
import path from 'path'
import { Listr } from 'listr2'
import terminalLink from 'terminal-link'
import { registerApiSideBabelHook } from '@redwoodjs/internal/dist/build/babel/api'
import { errorTelemetry } from '@redwoodjs/telemetry'
import { getPaths } from '../../lib'
import c from '../../lib/colors'
// sorts migrations by date, oldest first
const sortMigrations = (migrations) => {
return migrations.sort((a, b) => {
const aVersion = parseInt(Object.keys(a)[0])
const bVersion = parseInt(Object.keys(b)[0])
if (aVersion > bVersion) {
return 1
}
if (aVersion < bVersion) {
return -1
}
return 0
})
}
const SUPPORTED_EXTENSIONS = ['.js', '.ts']
// Return the list of migrations that haven't run against the database yet
const getMigrations = async (db) => {
const basePath = path.join(getPaths().api.dataMigrations)
if (!fs.existsSync(basePath)) {
return []
}
// gets all migrations present in the app
const files = fs
.readdirSync(basePath)
.filter((m) => SUPPORTED_EXTENSIONS.includes(path.extname(m)))
.map((m) => {
return {
[m.split('-')[0]]: path.join(basePath, m),
}
})
// gets all migration versions that have already run against the database
const ranMigrations = await db.rW_DataMigration.findMany({
orderBy: { version: 'asc' },
})
const ranVersions = ranMigrations.map((migration) =>
migration.version.toString()
)
const unrunMigrations = files.filter((migration) => {
return !ranVersions.includes(Object.keys(migration)[0])
})
return sortMigrations(unrunMigrations)
}
// adds data for completed migrations to the DB
const record = async (db, { version, name, startedAt, finishedAt }) => {
await db.rW_DataMigration.create({
data: { version, name, startedAt, finishedAt },
})
}
// output run status to the console
const report = (counters) => {
console.log('')
if (counters.run) {
console.info(
c.green(`${counters.run} data migration(s) completed successfully.`)
)
}
if (counters.error) {
console.error(
c.error(`${counters.error} data migration(s) exited with errors.`)
)
}
if (counters.skipped) {
console.warn(
c.warning(
`${counters.skipped} data migration(s) skipped due to previous error.`
)
)
}
console.log('')
}
const runScript = async (db, scriptPath) => {
const script = await import(scriptPath)
const startedAt = new Date()
await script.default({ db })
const finishedAt = new Date()
return { startedAt, finishedAt }
}
export const command = 'up'
export const description =
'Run any outstanding Data Migrations against the database'
export const builder = (yargs) => {
yargs.epilogue(
`Also see the ${terminalLink(
'Redwood CLI Reference',
'https://redwoodjs.com/docs/cli-commands#datamigrate-up'
)}`
)
}
export const handler = async () => {
// Import babel settings so we can write es6 scripts
registerApiSideBabelHook()
const { db } = require(path.join(getPaths().api.lib, 'db'))
const migrations = await getMigrations(db)
// exit immediately if there aren't any migrations to run
if (!migrations.length) {
console.info(c.green('\nNo data migrations run, already up-to-date.\n'))
process.exit(0)
}
const counters = { run: 0, skipped: 0, error: 0 }
const migrationTasks = migrations.map((migration) => {
const version = Object.keys(migration)[0]
const migrationPath = Object.values(migration)[0]
const migrationName = path.basename(migrationPath, '.js')
return {
title: migrationName,
skip: () => {
if (counters.error > 0) {
counters.skipped++
return true
}
},
task: async () => {
try {
const { startedAt, finishedAt } = await runScript(db, migrationPath)
counters.run++
await record(db, {
version,
name: migrationName,
startedAt,
finishedAt,
})
} catch (e) {
counters.error++
console.error(c.error(`Error in data migration: ${e.message}`))
}
},
}
})
const tasks = new Listr(migrationTasks, {
rendererOptions: { collapse: false },
renderer: 'verbose',
})
try {
await tasks.run()
await db.$disconnect()
report(counters)
if (counters.error) {
process.exit(1)
}
} catch (e) {
await db.$disconnect()
report(counters)
errorTelemetry(process.argv, e.message)
process.exit(e?.exitCode || 1)
}
}