-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
getDmmf.ts
208 lines (190 loc) · 6.56 KB
/
getDmmf.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
import Debug from '@prisma/debug'
import { NodeAPILibraryTypes } from '@prisma/engine-core'
import { getCliQueryEngineBinaryType } from '@prisma/engines'
import { BinaryType } from '@prisma/fetch-engine'
import { DataSource, DMMF, GeneratorConfig } from '@prisma/generator-helper'
import { isNodeAPISupported } from '@prisma/get-platform'
import chalk from 'chalk'
import execa, { ExecaChildProcess, ExecaReturnValue } from 'execa'
import fs from 'fs'
import tmpWrite from 'temp-write'
import { promisify } from 'util'
import { resolveBinary } from '../resolveBinary'
import { load } from '../utils/load'
const debug = Debug('prisma:getDMMF')
const unlink = promisify(fs.unlink)
const MAX_BUFFER = 1_000_000_000
export interface ConfigMetaFormat {
datasources: DataSource[]
generators: GeneratorConfig[]
warnings: string[]
}
export type GetDMMFOptions = {
datamodel?: string
cwd?: string
prismaPath?: string
datamodelPath?: string
retry?: number
previewFeatures?: string[]
}
// TODO add error handling functions
export async function getDMMF(options: GetDMMFOptions): Promise<DMMF.Document> {
warnOnDeprecatedFeatureFlag(options.previewFeatures)
const cliEngineBinaryType = getCliQueryEngineBinaryType()
let dmmf: DMMF.Document | undefined
if (cliEngineBinaryType === BinaryType.libqueryEngine) {
dmmf = await getDmmfNodeAPI(options)
} else {
dmmf = await getDmmfBinary(options)
}
return dmmf
}
async function getDmmfNodeAPI(options: GetDMMFOptions): Promise<DMMF.Document> {
const queryEnginePath = await resolveBinary(
BinaryType.libqueryEngine,
options.prismaPath,
)
await isNodeAPISupported()
debug(`Using Node-API Query Engine at: ${queryEnginePath}`)
const NodeAPIQueryEngineLibrary =
load<NodeAPILibraryTypes.Library>(queryEnginePath)
const datamodel =
options.datamodel ?? fs.readFileSync(options.datamodelPath!, 'utf-8')
let dmmf: DMMF.Document | undefined
try {
dmmf = JSON.parse(
await NodeAPIQueryEngineLibrary.dmmf(datamodel),
) as DMMF.Document
} catch (e) {
const error = JSON.parse(e.message)
const message = addMissingOpenSSLInfo(error.message)
throw new Error(chalk.redBright.bold('Schema parsing\n') + message)
}
return dmmf
}
async function getDmmfBinary(options: GetDMMFOptions): Promise<DMMF.Document> {
let result: ExecaChildProcess<string> | undefined | ExecaReturnValue<string>
const queryEnginePath = await resolveBinary(
BinaryType.queryEngine,
options.prismaPath,
)
debug(`Using Query Engine Binary at: ${queryEnginePath}`)
try {
let tempDatamodelPath: string | undefined = options.datamodelPath
if (!tempDatamodelPath) {
try {
tempDatamodelPath = await tmpWrite(options.datamodel!)
} catch (err) {
throw new Error(
chalk.redBright.bold('Get DMMF ') +
'unable to write temp data model path',
)
}
}
const execaOptions = {
cwd: options.cwd,
env: {
PRISMA_DML_PATH: tempDatamodelPath,
RUST_BACKTRACE: '1',
...(process.env.NO_COLOR ? {} : { CLICOLOR_FORCE: '1' }),
},
maxBuffer: MAX_BUFFER,
}
const args = ['--enable-raw-queries', 'cli', 'dmmf']
result = await execa(queryEnginePath, args, execaOptions)
if (!options.datamodelPath) {
await unlink(tempDatamodelPath)
}
if (
result.stdout.includes('Please wait until the') &&
options.retry &&
options.retry > 0
) {
debug('Retrying after "Please wait until"')
await new Promise((r) => setTimeout(r, 5000))
return getDMMF({
...options,
retry: options.retry - 1,
})
}
// necessary, as sometimes the query engine prints some other stuff
const firstCurly = result.stdout.indexOf('{')
const stdout = result.stdout.slice(firstCurly)
return JSON.parse(stdout)
} catch (e) {
debug('getDMMF failed', e)
// If this unlikely event happens, try it at least once more
if (
e.message.includes('Command failed with exit code 26 (ETXTBSY)') &&
options.retry &&
options.retry > 0
) {
await new Promise((resolve) => setTimeout(resolve, 500))
debug('Retrying after ETXTBSY')
return getDMMF({
...options,
retry: options.retry - 1,
})
}
const output = e.stderr || e.stdout
if (output) {
let json
try {
json = JSON.parse(output)
} catch (e) {
//
}
let message = (json && json.message) || output
message = addMissingOpenSSLInfo(message)
throw new Error(chalk.redBright.bold('Schema parsing\n') + message)
}
if (e.message.includes('in JSON at position')) {
throw new Error(
`Problem while parsing the query engine response at ${queryEnginePath}. ${result?.stdout}\n${e.stack}`,
)
}
throw new Error(e)
}
}
function addMissingOpenSSLInfo(message: string) {
if (
message.includes(
'debian-openssl-1.1.x: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory',
) ||
message.includes(
'debian-openssl-1.0.x: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory',
)
) {
message += `\n${chalk.green(
`Your linux installation misses the openssl package. You can install it like so:\n`,
)}${chalk.green.bold('apt-get -qy update && apt-get -qy install openssl')}`
}
return message
}
// See also removedFlags at
// https://github.com/prisma/prisma/blob/main/packages/engine-core/src/binary/BinaryEngine.ts#L174
function warnOnDeprecatedFeatureFlag(previewFeatures?: string[]) {
const getMessage = (flag: string) =>
`${chalk.blueBright(
'info',
)} The preview flag "${flag}" is not needed anymore, please remove it from your schema.prisma`
const removedFeatureFlagMap = {
insensitiveFilters: getMessage('insensitiveFilters'),
atomicNumberOperations: getMessage('atomicNumberOperations'),
connectOrCreate: getMessage('connectOrCreate'),
transaction: getMessage('transaction'),
nApi: getMessage('nApi'),
transactionApi: getMessage('transactionApi'),
uncheckedScalarInputs: getMessage('uncheckedScalarInputs'),
nativeTypes: getMessage('nativeTypes'),
createMany: getMessage('createMany'),
groupBy: getMessage('groupBy'),
microsoftSqlServer: getMessage('microsoftSqlServer'),
}
previewFeatures?.forEach((f) => {
const removedMessage = removedFeatureFlagMap[f]
if (removedMessage && !process.env.PRISMA_HIDE_PREVIEW_FLAG_WARNINGS) {
console.warn(removedMessage)
}
})
}