Skip to content

Commit

Permalink
add generator validation
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Oct 8, 2019
1 parent 1feba71 commit 3668c1e
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 74 deletions.
2 changes: 1 addition & 1 deletion cli/generator-helper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prisma/generator-helper",
"version": "0.0.2",
"version": "0.0.3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"typings": "dist/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions cli/sdk/migrationEngine-darwin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log("Please wait until the 'prisma migrationEngine' download completes!")
2 changes: 1 addition & 1 deletion cli/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prisma/sdk",
"version": "0.0.0",
"version": "0.0.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": "git@github.com:prisma/prisma2.git",
Expand Down
2 changes: 2 additions & 0 deletions cli/sdk/queryEngine-darwin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
console.log("Please wait until the 'prisma queryEngine' download completes!")
84 changes: 24 additions & 60 deletions cli/sdk/src/__tests__/getGenerators/getGenerators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('getGenerators', () => {
}

const generators = await getGenerators(
path.join(__dirname, 'schema.prisma'),
path.join(__dirname, 'valid-minimal-schema.prisma'),
aliases,
)

Expand Down Expand Up @@ -42,8 +42,8 @@ describe('getGenerators', () => {
).toMatchInlineSnapshot(`
Object {
"datamodel": "generator gen {
provider = \\"predefined-generator\\"
platforms = [\\"darwin\\"]
provider = \\"predefined-generator\\"
binaryTargets = [\\"darwin\\"]
}
model User {
Expand All @@ -52,10 +52,10 @@ describe('getGenerators', () => {
}",
"datasources": Array [],
"generator": Object {
"binaryTargets": Array [],
"config": Object {
"platforms": "(array)",
},
"binaryTargets": Array [
"darwin",
],
"config": Object {},
"name": "gen",
"output": null,
"provider": "predefined-generator",
Expand All @@ -64,66 +64,30 @@ describe('getGenerators', () => {
}
`)
})
})

describe('getGenerator', () => {
test('minimal', async () => {
test('fail on platforms', async () => {
const aliases = {
'predefined-generator': path.join(__dirname, 'generator'),
}

const generator = await getGenerator(
path.join(__dirname, 'schema.prisma'),
aliases,
)
expect(
getGenerators(
path.join(__dirname, 'invalid-platforms-schema.prisma'),
aliases,
),
).rejects.toThrow('deprecated')
})

expect(generator.manifest).toMatchInlineSnapshot(`
Object {
"defaultOutput": "default-output",
"denylist": Array [
"SomeForbiddenType",
],
"prettyName": "This is a pretty pretty name",
"requiresEngines": Array [
"queryEngine",
"migrationEngine",
],
"requiresGenerators": Array [
"photonjs",
],
}
`)
test('fail on invalid binaryTarget', async () => {
const aliases = {
'predefined-generator': path.join(__dirname, 'generator'),
}

expect(
pick(generator.options, [
'generator',
'datamodel',
'datasources',
'otherGenerators',
]),
).toMatchInlineSnapshot(`
Object {
"datamodel": "generator gen {
provider = \\"predefined-generator\\"
platforms = [\\"darwin\\"]
}
model User {
id Int @id
name String
}",
"datasources": Array [],
"generator": Object {
"binaryTargets": Array [],
"config": Object {
"platforms": "(array)",
},
"name": "gen",
"output": null,
"provider": "predefined-generator",
},
"otherGenerators": Array [],
}
`)
getGenerators(
path.join(__dirname, 'invalid-binary-target-schema.prisma'),
aliases,
),
).rejects.toThrow('Unknown')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generator gen {
provider = "predefined-generator"
binaryTargets = ["marvin"]
}

model User {
id Int @id
name String
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
generator gen {
provider = "predefined-generator"
platforms = ["darwin"]
// platforms is deprecated
}

model User {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generator gen {
provider = "predefined-generator"
binaryTargets = ["darwin"]
}

model User {
id Int @id
name String
}
83 changes: 72 additions & 11 deletions cli/sdk/src/getGenerators.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import fs from 'fs'
import pMap from 'p-map'
import path from 'path'
import { GeneratorOptions } from '@prisma/generator-helper'
import {
GeneratorOptions,
GeneratorConfig,
EngineType,
} from '@prisma/generator-helper'
import 'flat-map-polyfill'

import { getConfig, getDMMF } from './engineCommands'
import { download } from '@prisma/fetch-engine'
import { unique } from './unique'
import { pick } from './pick'
import { Generator } from './Generator'
import chalk from 'chalk'
import { BinaryDownloadConfiguration } from '@prisma/fetch-engine/dist/download'

/**
* Makes sure that all generators have the binaries they deserve and returns a
Expand All @@ -31,6 +37,8 @@ export async function getGenerators(
const dmmf = await getDMMF(schema)
const config = await getConfig(schema)

validateGenerators(config.generators)

const runningGenerators: Generator[] = []
try {
// 1. Get all generators
Expand Down Expand Up @@ -79,20 +87,25 @@ export async function getGenerators(
config.generators.flatMap(g => g.binaryTargets || []),
)

const binariesConfig = binaries.reduce((acc, curr) => {
acc[curr] = path.join(__dirname, '../')
return acc
}, {})
const binariesConfig: BinaryDownloadConfiguration = binaries.reduce(
(acc, curr) => {
acc[engineTypeToBinaryType(curr)] = path.join(__dirname, '../')
return acc
},
{},
)

const binaryPaths = await download({
const downloadParams = {
binaries: binariesConfig,
binaryTargets: binaryTargets as any[],
showProgress:
typeof printDownloadProgress === 'boolean'
? printDownloadProgress
: true,
version,
})
version: version || 'latest',
}

const binaryPaths = await download(downloadParams)

for (const generator of generators) {
if (generator.manifest && generator.manifest.requiresEngines) {
Expand All @@ -113,9 +126,7 @@ export async function getGenerators(
}

/**
* Makes sure that all generator have the binaries they deserve and returns a
* `Generator` class per generator defined in the schema.prisma file.
* In other words, this is basically a generator factory function.
* Shortcut for getGenerators, if there is only one generator defined. Useful for testing
* @param schemaPath path to schema.prisma
* @param generatorAliases Aliases like `photonjs` -> `node_modules/photonjs/gen.js`
* @param version Version of the binary, commit hash of https://github.com/prisma/prisma-engine/commits/master
Expand All @@ -139,3 +150,53 @@ export async function getGenerator(
export function skipIndex<T = any>(arr: T[], index: number): T[] {
return [...arr.slice(0, index), ...arr.slice(index + 1)]
}

export const knownBinaryTargets = [
'native',
'darwin',
'linux-glibc-libssl1.0.1',
'linux-glibc-libssl1.0.2',
'linux-glibc-libssl1.1.0',
'linux-musl-libssl1.1.0',
'windows',
]

function validateGenerators(generators: GeneratorConfig[]) {
for (const generator of generators) {
if (generator.config.platforms) {
throw new Error(
`The \`platforms\` field on the generator definition is deprecated. Please rename it to \`binaryTargets\`.`,
)
}
if (generator.binaryTargets) {
for (const binaryTarget of generator.binaryTargets) {
if (!knownBinaryTargets.includes(binaryTarget)) {
throw new Error(
`Unknown binary target ${chalk.red(
binaryTarget,
)} in generator ${chalk.bold(generator.name)}.
Possible binaryTargets: ${chalk.greenBright(knownBinaryTargets.join(', '))}`,
)
}
}
}
}
}

function engineTypeToBinaryType(
engineType: EngineType,
): keyof BinaryDownloadConfiguration {
if (engineType === 'introspectionEngine') {
return 'introspection-engine' as any // TODO: Remove as any as soon as type added to @prisma/fetch-engine
}

if (engineType === 'migrationEngine') {
return 'migration-engine'
}

if (engineType === 'queryEngine') {
return 'query-engine'
}

throw new Error(`Could not convert binary type ${engineType}`)
}
5 changes: 5 additions & 0 deletions cli/sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { Generator } from './Generator'
export { getGenerators, getGenerator } from './getGenerators'
export { isdlToDatamodel2 } from './isdlToDatamodel2'
export { isdlToDmmfDatamodel } from './isdlToDmmfDatamodel'
export { getDMMF, getConfig, dmmfToDml } from './engineCommands'
2 changes: 1 addition & 1 deletion cli/sdk/src/isdlToDatamodel2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ISDL } from 'prisma-datamodel'
import { dmmfToDml } from './engineCommands'
import { isdlToDmmfDatamodel } from './isdlToDmmf'
import { isdlToDmmfDatamodel } from './isdlToDmmfDatamodel'
import { DataSource, GeneratorConfig } from '@prisma/generator-helper'

export type ConnectorType = 'mysql' | 'mongo' | 'sqlite' | 'postgresql'
Expand Down
File renamed without changes.

0 comments on commit 3668c1e

Please sign in to comment.