Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
add support for v1 to v2 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jan 29, 2019
1 parent b0a3c63 commit 1306da8
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 82 deletions.
90 changes: 16 additions & 74 deletions cli/packages/prisma-cli-engine/src/Client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,80 +24,13 @@ import * as opn from 'opn'
import { concatName } from 'prisma-yml/dist/PrismaDefinition'
import { IntrospectionQuery } from 'graphql'
import { hasTypeWithField } from '../utils/graphql-schema'
import {
renderMigrationFragment,
renderStepFragment,
} from './migrationFragment'

const debug = require('debug')('client')

const STEP_FRAGMENT = `
type
__typename
... on CreateEnum {
name
ce_values: values
}
... on CreateField {
model
name
cf_typeName: typeName
cf_isRequired: isRequired
cf_isList: isList
cf_isUnique: unique
cf_relation: relation
cf_defaultValue: default
cf_enum: enum
}
... on CreateModel {
name
}
... on CreateRelation {
name
leftModel
rightModel
}
... on DeleteEnum {
name
}
... on DeleteField {
model
name
}
... on DeleteModel {
name
}
... on DeleteRelation {
name
}
... on UpdateEnum {
name
newName
values
}
... on UpdateField {
model
name
newName
typeName
isRequired
isList
isUnique: unique
relation
default
enum
}
... on UpdateModel {
name
um_newName: newName
}
`

const MIGRATION_FRAGMENT = `
fragment MigrationFragment on Migration {
revision
steps {
${STEP_FRAGMENT}
}
}
`

export class Client {
config: Config
env: Environment
Expand Down Expand Up @@ -765,7 +698,7 @@ export class Client {
}
}
}
${MIGRATION_FRAGMENT}
${renderMigrationFragment(false)}
`

const introspectionResult: IntrospectionQuery = await this.client.request(
Expand All @@ -776,7 +709,16 @@ export class Client {
'DeployPayload',
'steps',
)
const steps = hasStepsApi ? `steps { ${STEP_FRAGMENT} }` : ''

const hasRelationManifestationApi = hasTypeWithField(
introspectionResult,
'CreateRelation',
'after',
)

const steps = hasStepsApi
? `steps { ${renderStepFragment(hasRelationManifestationApi)} }`
: ''

if (
noMigration &&
Expand Down Expand Up @@ -817,7 +759,7 @@ export class Client {
${steps}
}
}
${MIGRATION_FRAGMENT}
${renderMigrationFragment(hasRelationManifestationApi)}
`

try {
Expand Down
105 changes: 105 additions & 0 deletions cli/packages/prisma-cli-engine/src/Client/migrationFragment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const RELATION_MANIFESTATION_FRAGMENT = `
type
... on Inline {
model
column
}
... on LinkTable {
table
modelAColumn
modelBColumn
idColumn
type
}
`

/**
* Generated a graphql fragment for the step type of the management api
* @param r renderMigrationManifestations
*/
export const renderStepFragment = (r: boolean) => `
type
__typename
... on CreateEnum {
name
ce_values: values
}
... on CreateField {
model
name
cf_typeName: typeName
cf_isRequired: isRequired
cf_isList: isList
cf_isUnique: unique
cf_relation: relation
cf_defaultValue: default
cf_enum: enum
}
... on CreateModel {
name
}
... on CreateRelation {
name
leftModel
rightModel
${r ? `after { ${RELATION_MANIFESTATION_FRAGMENT} }` : ''}
}
${
r
? `
... on UpdateRelation {
ur_name: name
ur_newName: newName
before { ${RELATION_MANIFESTATION_FRAGMENT} }
ur_after: after { ${RELATION_MANIFESTATION_FRAGMENT} }
}
`
: ''
}
... on DeleteEnum {
name
}
... on DeleteField {
model
name
}
... on DeleteModel {
name
}
... on DeleteRelation {
name
}
... on UpdateEnum {
name
newName
values
}
... on UpdateField {
model
name
newName
typeName
isRequired
isList
isUnique: unique
relation
default
enum
}
... on UpdateModel {
name
um_newName: newName
}
`

export const renderMigrationFragment = (
renderRelationManifestations: boolean,
) => `
fragment MigrationFragment on Migration {
revision
steps {
${renderStepFragment(renderRelationManifestations)}
}
}
`
22 changes: 22 additions & 0 deletions cli/packages/prisma-cli-engine/src/Client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ export interface Migration {
steps: MigrationStep[]
}

export type RelationManifestation = LinkTableManifestation | InlineManifestation

export interface LinkTableManifestation {
type: 'LinkTable'
table: string
modelAColumn: string
modelBColumn: string
idColumn?: string
}

export interface InlineManifestation {
type: 'Inline'
model: string
column: string
}

export interface MigrationStep {
type: string
__typename?: string | null
Expand All @@ -58,6 +74,12 @@ export interface MigrationStep {
// createRelation
leftModel?: string | null
rightModel?: string | null
after?: RelationManifestation
// updateRelation
before?: RelationManifestation
ur_after?: RelationManifestation
ur_name?: string
ur_newName?: string
// deleteField
model?: string | null
// updateEnum
Expand Down
83 changes: 75 additions & 8 deletions cli/packages/prisma-cli-engine/src/Output/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@ import chalk from 'chalk'
import { Output } from './index'
import { makePartsEnclodesByCharacterBold } from './util'
import * as groupBy from 'lodash.groupby'
import { SchemaError, MigrationStep } from '../Client/types'
import {
SchemaError,
MigrationStep,
LinkTableManifestation,
} from '../Client/types'

const b = s => `\`${chalk.bold(s)}\``

interface StepWithName {
name: string
}

export class MigrationPrinter {
out: Output
constructor(out: Output) {
this.out = out
}
printMessages(steps: MigrationStep[]) {
const relevantSteps = steps.filter(s =>
['CreateRelation', 'UpdateRelation'].includes(s.type),
)
this.printTypes(steps)
this.printEnums(steps)
this.printRelations(steps)
Expand Down Expand Up @@ -161,12 +172,68 @@ export class MigrationPrinter {
switch (step.type) {
case 'CreateRelation':
this.printRelationName(step)
this.out.log(
`${pad}${this.getSymbol('create')} Created relation between ${
step.leftModel
} and ${step.rightModel}`,
)
if (step.after) {
const after = step.after!
if (after.type === 'LinkTable') {
this.out.log(
`${pad}${this.getSymbol('create')} Link Table ${b(
after.table,
)} between ${b(step.leftModel)} and ${b(
step.rightModel,
)} has been created`,
)
} else {
this.out.log(
`${pad}${this.getSymbol(
'create',
)} Created an inline relation between ${b(
step.leftModel,
)} and ${b(step.rightModel)} in the column ${b(
after.column,
)} of table ${b(after.model)}`,
)
}
}
break
case 'UpdateRelation':
if (step.ur_after && step.before) {
this.printRelationName({ name: step.ur_newName || step.ur_name! })

const before = step.before!
const after = step.ur_after!

if (before.type === 'LinkTable' && after.type === 'Inline') {
this.out.log(
`${pad}${this.getSymbol('delete')} Link Table ${b(
before.table,
)} has been removed`,
)
this.out.log(
`${pad}${this.getSymbol(
'create',
)} Created an inline relation in the column ${b(
after.column,
)} of table ${b(after.model)}`,
)
}

if (before.type === 'Inline' && after.type === 'LinkTable') {
this.out.log(
`${pad}${this.getSymbol(
'delete',
)} Removed an inline relation in the column ${b(
before.column,
)} of table ${b(before.model)}`,
)
this.out.log(
`${pad}${this.getSymbol('create')} Link Table ${b(
after.table,
)} has been created`,
)
}
}
break

case 'DeleteRelation':
this.printRelationName(step)
this.out.log(
Expand All @@ -179,11 +246,11 @@ export class MigrationPrinter {
})
}

printRelationName(step: MigrationStep) {
printRelationName(step: StepWithName) {
this.out.log(`\n ${chalk.bold(step.name)} (Relation)`)
}

printEnumName(step: MigrationStep) {
printEnumName(step: StepWithName) {
this.out.log(`\n ${chalk.bold(step.name)} (Enum)`)
}

Expand Down

0 comments on commit 1306da8

Please sign in to comment.