Skip to content

Commit

Permalink
feature(introspection): add re-introspection flag & warnings (#2820)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Suchanek <Tim.Suchanek@gmail.com>
  • Loading branch information
Jolg42 and timsuchanek committed Jun 23, 2020
1 parent a6631d7 commit 57217a9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 23 deletions.
30 changes: 28 additions & 2 deletions src/packages/introspection/src/commands/Introspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export class Introspect implements Command {
Instead of saving the result to the filesystem, you can also print it
${chalk.dim('$')} prisma introspect --print'
${chalk.bold('Flags')}
--experimental-reintrospection Enables the experimental re-introspection feature
`)

private printUrlAsDatasource(url: string): string {
Expand All @@ -68,6 +71,7 @@ export class Introspect implements Command {
'--url': String,
'--print': Boolean,
'--schema': String,
'--experimental-reintrospection': Boolean,
})

const log = (...messages): void => {
Expand Down Expand Up @@ -118,8 +122,12 @@ export class Introspect implements Command {
let introspectionSchema = ''
let introspectionWarnings: IntrospectionWarnings[]
let introspectionSchemaVersion: IntrospectionSchemaVersion
const enableReintrospection = args['--experimental-reintrospection']
try {
const introspectionResult = await engine.introspect(schema)
const introspectionResult = await engine.introspect(
schema,
enableReintrospection,
)
introspectionSchema = introspectionResult.datamodel
introspectionWarnings = introspectionResult.warnings
introspectionSchemaVersion = introspectionResult.version
Expand Down Expand Up @@ -164,7 +172,9 @@ Then you can run ${chalk.green('prisma introspect')} again.
for (const warning of warnings) {
message += `\n${warning.message}\n`

if (warning.code === 1) {
if (warning.code === 0) {
// affected === null
} else if (warning.code === 1) {
message += warning.affected
.map((it) => `- "${it.model}"`)
.join('\n')
Expand Down Expand Up @@ -199,6 +209,22 @@ Then you can run ${chalk.green('prisma introspect')} again.
message += warning.affected
.map((it) => `- Model "${it.model}", field: "${it.field}"`)
.join('\n')
} else if (warning.code === 6) {
message += warning.affected
.map((it) => `- Model "${it.model}", field: "${it.field}"`)
.join('\n')
} else if (warning.code === 7) {
message += warning.affected
.map((it) => `- Model "${it.model}"`)
.join('\n')
} else if (warning.code === 8) {
message += warning.affected
.map((it) => `- Model "${it.model}", field: "${it.field}"`)
.join('\n')
} else if (warning.code === 9) {
message += warning.affected
.map((it) => `- Enum "${it.enm}"`)
.join('\n')
}

message += `\n`
Expand Down
85 changes: 64 additions & 21 deletions src/packages/sdk/src/IntrospectionEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,80 @@ export class IntrospectionError extends Error {
}
}

// See https://github.com/prisma/prisma-engines/blob/ReIntrospection/introspection-engine/connectors/sql-introspection-connector/src/warnings.rs
export type IntrospectionWarnings =
| IntrospectionWarningsInvalidReintro
| IntrospectionWarningsMissingUnique
| IntrospectionWarningsEmptyFieldName
| IntrospectionWarningsUnsupportedType
| IntrospectionWarningsInvalidEnumName
| IntrospectionWarningsPrisma1
| IntrospectionWarningsCuidPrisma1
| IntrospectionWarningsUuidPrisma1
| IntrospectionWarningsFieldModelReintro
| IntrospectionWarningsFieldMapReintro
| IntrospectionWarningsEnumMapReintro

interface IntrospectionWarningsMissingUnique {
code: 1
type AffectedModel = { model: string }[]
type AffectedModelAndField = { model: string; field: string }[]
type AffectedModelAndFieldAndType = {
model: string
field: string
tpe: string
}[]
type AffectedEnum = { enm: string }[]
type AffectedEnumAndValue = { enm: string; value: string }[]

interface IntrospectionWarning {
code: number
message: string
affected: { model: string }[]
affected:
| AffectedModel
| AffectedModelAndField
| AffectedModelAndFieldAndType
| AffectedEnum
| AffectedEnumAndValue
| null
}

interface IntrospectionWarningsEmptyFieldName {
interface IntrospectionWarningsInvalidReintro extends IntrospectionWarning {
code: 0
affected: null
}
interface IntrospectionWarningsMissingUnique extends IntrospectionWarning {
code: 1
affected: AffectedModel
}
interface IntrospectionWarningsEmptyFieldName extends IntrospectionWarning {
code: 2
message: string
affected: { model: string; field: string }[]
affected: AffectedModelAndField
}

interface IntrospectionWarningsUnsupportedType {
interface IntrospectionWarningsUnsupportedType extends IntrospectionWarning {
code: 3
message: string
affected: { model: string; field: string; tpe: string }[]
affected: AffectedModelAndFieldAndType
}

interface IntrospectionWarningsInvalidEnumName {
interface IntrospectionWarningsInvalidEnumName extends IntrospectionWarning {
code: 4
message: string
affected: { enm: string; value: string }[]
affected: AffectedEnumAndValue
}

interface IntrospectionWarningsPrisma1 {
interface IntrospectionWarningsCuidPrisma1 extends IntrospectionWarning {
code: 5
message: string
affected: { model: string; field: string }[]
affected: AffectedModelAndField
}
interface IntrospectionWarningsUuidPrisma1 extends IntrospectionWarning {
code: 6
affected: AffectedModelAndField
}
interface IntrospectionWarningsFieldModelReintro extends IntrospectionWarning {
code: 7
affected: AffectedModel
}
interface IntrospectionWarningsFieldMapReintro extends IntrospectionWarning {
code: 8
affected: AffectedModelAndField
}
interface IntrospectionWarningsEnumMapReintro extends IntrospectionWarning {
code: 9
affected: AffectedEnum
}

export type IntrospectionSchemaVersion =
Expand Down Expand Up @@ -135,13 +174,16 @@ export class IntrospectionEngine {
}
public introspect(
schema: string,
reintrospect?: Boolean,
): Promise<{
datamodel: string
warnings: IntrospectionWarnings[]
version: IntrospectionSchemaVersion
}> {
this.lastUrl = schema
return this.runCommand(this.getRPCPayload('introspect', { schema }))
return this.runCommand(
this.getRPCPayload('introspect', { schema, reintrospect }),
)
}
public listDatabases(schema: string): Promise<string[]> {
this.lastUrl = schema
Expand Down Expand Up @@ -202,10 +244,11 @@ export class IntrospectionEngine {
try {
const binaryPath = await resolveBinary('introspection-engine')
debugRpc('starting introspection engine with binary: ' + binaryPath)

this.child = spawn(binaryPath, {
stdio: ['pipe', 'pipe', 'pipe'],
env: process.env,
cwd: this.cwd,
stdio: ['pipe', 'pipe', 'pipe'],
})

this.isRunning = true
Expand Down

0 comments on commit 57217a9

Please sign in to comment.