Skip to content

Commit

Permalink
fix(cli): fix doctor
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jul 6, 2020
1 parent a48aaec commit 3e811ed
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"test:generate": "./node_modules/.bin/mocha src/__tests__/integrate.generate.test.ts --require ts-mocha src/__tests__/integrate.generate.test.ts --timeout 10s",
"test:dotenv": "./node_modules/.bin/mocha src/__tests__/dotenv.test.ts --require ts-mocha src/__tests__/dotenv.test.ts --timeout 10s",
"test:format": "./node_modules/.bin/mocha src/__tests__/format.test.ts --require ts-mocha src/__tests__/format.test.ts --timeout 5s",
"test:doctor": "./node_modules/.bin/mocha src/__tests__/doctor.test.ts --require ts-mocha src/__tests__/doctor.test.ts --timeout 5s",
"test:sqlite": "./node_modules/.bin/mocha src/__tests__/integrate.sqlite.test.ts --require ts-mocha src/__tests__/integrate.sqlite.test.ts --timeout 10s",
"test:mariadb": "./node_modules/.bin/mocha src/__tests__/integrate.mariadb.test.ts --require ts-mocha src/__tests__/integrate.mariadb.test.ts --timeout 10s",
"test:mysql": "./node_modules/.bin/mocha src/__tests__/integrate.mysql.test.ts --require ts-mocha src/__tests__/integrate.mysql.test.ts --timeout 10s",
Expand Down
16 changes: 12 additions & 4 deletions src/packages/cli/src/Doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ export class Doctor implements Command {
cwd: path.dirname(schemaPath),
})

const { datamodel } = await engine.introspect(schema)
let datamodel
try {
const result = await engine.introspect(schema, true)
datamodel = result.datamodel
} finally {
engine.stop()
}

const remoteDmmf = await getDMMF({ datamodel })

const remoteModels = keyBy(
Expand Down Expand Up @@ -158,10 +165,11 @@ function printModelMessage({
}

for (const { localField, remoteField } of incorrectFieldType) {
const printField = (f: DMMF.Field) => f.name + (f.isList ? '[]' : '')
const printFieldType = (f: DMMF.Field) => f.type + (f.isList ? '[]' : '')

msg += `↪ Field ${localField.name} has type ${chalk.greenBright(
localField.type + printField(localField),
)} locally, but ${chalk.redBright(printField(remoteField))} remote`
printFieldType(localField),
)} locally, but ${chalk.redBright(printFieldType(remoteField))} remote\n`
}

return msg
Expand Down
74 changes: 74 additions & 0 deletions src/packages/cli/src/__tests__/doctor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import tempy from 'tempy'
import path from 'path'
import { Doctor } from '../Doctor'
import copy from '@apexearth/copy'
import assert from 'assert'
import stripAnsi from 'strip-ansi'

describe('doctor', () => {
it('doctor should succeed when schema and db match', async () => {
const tmpDir = tempy.directory()
await copy({
from: path.join(__dirname, 'fixtures/example-project/prisma'),
to: tmpDir,
recursive: true,
})
const cwd = process.cwd()
process.chdir(tmpDir)

const doctor = Doctor.new()
const errLog = []
const oldConsoleError = console.error
console.error = (...args) => {
errLog.push(...args)
}
const result = await doctor.parse([])
console.error = oldConsoleError

assert.deepEqual(errLog, [`👩‍⚕️🏥 Prisma Doctor checking the database...`])
assert.equal(result, 'Everything in sync 🔄')

process.chdir(cwd)
})

it('should fail when schema and db dont match', async () => {
const tmpDir = tempy.directory()
await copy({
from: path.join(__dirname, 'fixtures/schema-db-out-of-sync'),
to: tmpDir,
recursive: true,
})
const cwd = process.cwd()
process.chdir(tmpDir)

const doctor = Doctor.new()
const errLog = []
const oldConsoleError = console.error
console.error = (...args) => {
errLog.push(...args)
}
let err
try {
const result = await doctor.parse([])
} catch (e) {
err = e
}
console.error = oldConsoleError

assert.equal(
stripAnsi(err.message),
`
Post
↪ Model is missing in database
User
↪ Field name is missing in database
↪ Field posts is missing in database
`,
)

process.chdir(cwd)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
generator client {
provider = "prisma-client-js"
output = "../generated/client"
}

datasource db {
provider = "sqlite"
url = "file:dev.db"
}

model Post {
id Int @default(autoincrement()) @id
createdAt DateTime @default(now())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}

model Profile {
id Int @default(autoincrement()) @id
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
}

model User {
id Int @default(autoincrement()) @id
email String @unique
name String?
posts Post[]
profile Profile?
}

0 comments on commit 3e811ed

Please sign in to comment.