-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a48aaec
commit 3e811ed
Showing
4 changed files
with
121 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
34 changes: 34 additions & 0 deletions
34
src/packages/cli/src/__tests__/fixtures/schema-db-out-of-sync/schema.prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
} |