forked from prisma/prisma
/
migrateEngineCommands.ts
288 lines (259 loc) · 7.19 KB
/
migrateEngineCommands.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { BinaryType } from '@prisma/fetch-engine'
import execa from 'execa'
import fs from 'fs'
import path from 'path'
import { promisify } from 'util'
import { getSchemaDir } from './cli/getSchema'
import { protocolToConnectorType } from './convertCredentials'
import { resolveBinary } from './resolveBinary'
const exists = promisify(fs.exists)
// ### Exit codes
// `0`: normal exit
// `1`: abnormal (error) exit
// `101`: panic
// Non-zero exit codes should always be accompanied by a log message on stderr with the `ERROR` level.
export enum MigrateEngineExitCode {
Success = 0,
Error = 1,
Panic = 101,
}
// Logging and crash reporting happens through JSON logs on the Migration Engine's
// stderr. Every line contains a single JSON object conforming to the following
// interface:
// {"timestamp":"2021-06-11T15:35:34.084486+00:00","level":"ERROR","fields":{"is_panic":false,"error_code":"","message":"Failed to delete SQLite database at `dev.db`.\nNo such file or directory (os error 2)\n"},"target":"migration_engine::logger"}
// {"timestamp":"2021-06-11T15:35:34.320358+00:00","level":"INFO","fields":{"message":"Starting migration engine CLI","git_hash":"a92947d63ede9b0b5b5aab253c2a7d9ad6cabe15"},"target":"migration_engine"}
export interface MigrateEngineLogLine {
timestamp: string
level: LogLevel
fields: LogFields
target: string
}
type LogLevel = 'INFO' | 'ERROR' | 'DEBUG' | 'WARN'
interface LogFields {
message: string
git_hash?: string
/// Hint from the engine to the CLI to log this line.
migrate_action?: 'log'
// Only for ERROR level messages
is_panic?: boolean
error_code?: string
[key: string]: any
}
// https://github.com/prisma/specs/tree/master/errors#common
export type DatabaseErrorCodes =
| 'P1000'
| 'P1001'
| 'P1002'
| 'P1003'
| 'P1009'
| 'P1010'
export type ConnectionResult = true | ConnectionError
export interface ConnectionError {
message: string
code: DatabaseErrorCodes
}
function parseJsonFromStderr(stderr: string): MigrateEngineLogLine[] {
// split by new line
const lines = stderr.split(/\r?\n/).slice(1) // Remove first element
const logs: any = []
for (const line of lines) {
const data = String(line)
try {
const json: MigrateEngineLogLine = JSON.parse(data)
logs.push(json)
} catch (e) {
throw new Error(`Could not parse migration engine response: ${e}`)
}
}
return logs
}
export async function canConnectToDatabase(
connectionString: string,
cwd = process.cwd(),
migrationEnginePath?: string,
): Promise<ConnectionResult> {
if (!connectionString) {
throw new Error(
'Connection url is empty. See https://www.prisma.io/docs/reference/database-reference/connection-urls',
)
}
const provider = protocolToConnectorType(`${connectionString.split(':')[0]}:`)
if (provider === 'sqlite') {
const sqliteExists = await doesSqliteDbExist(connectionString, cwd)
if (sqliteExists) {
return true
} else {
return {
code: 'P1003',
message: "SQLite database file doesn't exist",
}
}
}
try {
await execaCommand({
connectionString,
cwd,
migrationEnginePath,
engineCommandName: 'can-connect-to-database',
})
} catch (e) {
if (e.stderr) {
const logs = parseJsonFromStderr(e.stderr)
const error = logs.find((it) => it.level === 'ERROR')
if (error && error.fields.error_code && error.fields.message) {
return {
code: error.fields.error_code as DatabaseErrorCodes,
message: error.fields.message,
}
} else {
throw new Error(
`Migration engine error:\n${logs
.map((log) => log.fields.message)
.join('\n')}`,
)
}
} else {
throw new Error(`Migration engine exited.`)
}
}
return true
}
export async function createDatabase(
connectionString: string,
cwd = process.cwd(),
migrationEnginePath?: string,
) {
const dbExists = await canConnectToDatabase(
connectionString,
cwd,
migrationEnginePath,
)
// If database is already created, stop here, don't create it
if (dbExists === true) {
return false
}
try {
await execaCommand({
connectionString,
cwd,
migrationEnginePath,
engineCommandName: 'create-database',
})
return true
} catch (e) {
if (e.stderr) {
const logs = parseJsonFromStderr(e.stderr)
const error = logs.find((it) => it.level === 'ERROR')
if (error && error.fields.error_code && error.fields.message) {
throw new Error(`${error.fields.error_code}: ${error.fields.message}`)
} else {
throw new Error(
`Migration engine error:\n${logs
.map((log) => log.fields.message)
.join('\n')}`,
)
}
} else {
throw new Error(`Migration engine exited.`)
}
}
}
export async function dropDatabase(
connectionString: string,
cwd = process.cwd(),
migrationEnginePath?: string,
) {
try {
const result = await execaCommand({
connectionString,
cwd,
migrationEnginePath,
engineCommandName: 'drop-database',
})
if (
result &&
result.exitCode === 0 &&
result.stderr.includes('The database was successfully dropped')
) {
return true
} else {
// We should not arrive here normally
throw Error(
`An error occurred during the drop: ${JSON.stringify(
result,
undefined,
2,
)}`,
)
}
} catch (e) {
if (e.stderr) {
const logs = parseJsonFromStderr(e.stderr)
throw new Error(
`Migration engine error:\n${logs
.map((log) => log.fields.message)
.join('\n')}`,
)
} else {
throw new Error(`Migration engine exited.`)
}
}
}
export async function execaCommand({
connectionString,
cwd,
migrationEnginePath,
engineCommandName,
}: {
connectionString: string
cwd: string
migrationEnginePath?: string
engineCommandName:
| 'create-database'
| 'drop-database'
| 'can-connect-to-database'
}) {
migrationEnginePath =
migrationEnginePath || (await resolveBinary(BinaryType.migrationEngine))
try {
return await execa(
migrationEnginePath,
['cli', '--datasource', connectionString, engineCommandName],
{
cwd,
env: {
RUST_BACKTRACE: '1',
RUST_LOG: 'info',
},
},
)
} catch (e) {
if (e.message) {
e.message = e.message.replace(connectionString, '<REDACTED>')
}
if (e.stdout) {
e.stdout = e.stdout.replace(connectionString, '<REDACTED>')
}
if (e.stderr) {
e.stderr = e.stderr.replace(connectionString, '<REDACTED>')
}
throw e
}
}
export async function doesSqliteDbExist(
connectionString: string,
schemaDir?: string,
): Promise<boolean> {
let filePath = connectionString
if (filePath.startsWith('file:')) {
filePath = filePath.slice(5)
} else if (filePath.startsWith('sqlite:')) {
filePath = filePath.slice(7)
}
const cwd = schemaDir || (await getSchemaDir())
if (!cwd) {
throw new Error(`Could not find schema.prisma in ${process.cwd()}`)
}
const absoluteTarget = path.resolve(cwd, filePath)
return exists(absoluteTarget)
}