Skip to content

Commit

Permalink
fix(db pull): only output schema to stdout when using --print
Browse files Browse the repository at this point in the history
Closes #8321
  • Loading branch information
Jolg42 committed Aug 3, 2021
1 parent 0f6efec commit 7c8693b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 114 deletions.
201 changes: 90 additions & 111 deletions packages/migrate/src/__tests__/DbPull.test.ts
Expand Up @@ -17,11 +17,9 @@ describe('common/sqlite', () => {
const introspect = new DbPull()
await introspect.parse(['--print'])
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from schema.prisma
Datasource "db": SQLite database "dev.db" at "file:dev.db"
`)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
Expand All @@ -32,11 +30,9 @@ describe('common/sqlite', () => {
const introspect = new DbPull()
await introspect.parse(['--print', '--force'])
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from schema.prisma
Datasource "db": SQLite database "dev.db" at "file:dev.db"
`)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
Expand All @@ -48,11 +44,9 @@ describe('common/sqlite', () => {
const result = introspect.parse(['--print', '--url', 'file:dev.db'])
await expect(result).resolves.toBe('')
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from schema.prisma
Datasource "db": SQLite database "dev.db" at "file:dev.db"
`)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
Expand All @@ -66,11 +60,9 @@ describe('common/sqlite', () => {
`Unknown database type invalidstring:`,
)
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from schema.prisma
Datasource "db": SQLite database "dev.db" at "file:dev.db"
`)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
Expand All @@ -85,18 +77,17 @@ describe('common/sqlite', () => {
ctx.mocked['console.log'].mock.calls
.join('\n')
.replace(/\d{2,3}ms/, 'XXms'),
).toMatchInlineSnapshot(`
Introspecting based on datasource defined in schema.prisma …
✔ Introspected 3 models and wrote them into schema.prisma in XXms
Run prisma generate to generate Prisma Client.
`)
).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from schema.prisma
Datasource "db": SQLite database "dev.db" at "file:dev.db"
Introspecting based on datasource defined in schema.prisma …
✔ Introspected 3 models and wrote them into schema.prisma in XXms
Run prisma generate to generate Prisma Client.
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
Expand All @@ -112,18 +103,17 @@ describe('common/sqlite', () => {
ctx.mocked['console.log'].mock.calls
.join('\n')
.replace(/\d{2,3}ms/, 'XXms'),
).toMatchInlineSnapshot(`
Introspecting …
✔ Introspected 3 models and wrote them into schema.prisma in XXms
Run prisma generate to generate Prisma Client.
`)
).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from schema.prisma
Datasource "db": SQLite database "dev.db" at "file:dev.db"
Introspecting …
✔ Introspected 3 models and wrote them into schema.prisma in XXms
Run prisma generate to generate Prisma Client.
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
Expand Down Expand Up @@ -161,25 +151,24 @@ describe('common/sqlite', () => {
ctx.mocked['console.log'].mock.calls
.join('\n')
.replace(/\d{2,3}ms/, 'in XXms'),
).toMatchInlineSnapshot(`
Introspecting based on datasource defined in prisma/reintrospection.prisma …
✔ Introspected 3 models and wrote them into prisma/reintrospection.prisma in in XXms
*** WARNING ***
These models were enriched with \`@@map\` information taken from the previous Prisma schema.
- Model "AwesomeNewPost"
- Model "AwesomeProfile"
- Model "AwesomeUser"
Run prisma generate to generate Prisma Client.
`)
).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/reintrospection.prisma
Datasource "db": SQLite database "dev.db" at "file:dev.db"
Introspecting based on datasource defined in prisma/reintrospection.prisma …
✔ Introspected 3 models and wrote them into prisma/reintrospection.prisma in XXms
*** WARNING ***
These models were enriched with \`@@map\` information taken from the previous Prisma schema.
- Model "AwesomeNewPost"
- Model "AwesomeProfile"
- Model "AwesomeUser"
Run prisma generate to generate Prisma Client.
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
Expand Down Expand Up @@ -244,22 +233,20 @@ describe('common/sqlite', () => {
.join('\n')
.replace(/\d{2,3}ms/, 'in XXms'),
).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/reintrospection.prisma
Datasource "db": SQLite database "dev.db" at "file:dev.db"
`)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.error'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
// *** WARNING ***
//
// These models were enriched with \`@@map\` information taken from the previous Prisma schema.
// - Model "AwesomeNewPost"
// - Model "AwesomeProfile"
// - Model "AwesomeUser"
//
`)
// *** WARNING ***
//
// These models were enriched with \`@@map\` information taken from the previous Prisma schema.
// - Model "AwesomeNewPost"
// - Model "AwesomeProfile"
// - Model "AwesomeUser"
//
`)

expect(ctx.fs.read('prisma/reintrospection.prisma')).toStrictEqual(
originalSchema,
Expand All @@ -275,18 +262,17 @@ describe('common/sqlite', () => {
ctx.mocked['console.log'].mock.calls
.join('\n')
.replace(/\d{2,3}ms/, 'in XXms'),
).toMatchInlineSnapshot(`
Introspecting based on datasource defined in prisma/schema.prisma …
✔ Introspected 3 models and wrote them into prisma/schema.prisma in in XXms
Run prisma generate to generate Prisma Client.
`)
).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
Introspecting based on datasource defined in prisma/schema.prisma …
✔ Introspected 3 models and wrote them into prisma/schema.prisma in XXms
Run prisma generate to generate Prisma Client.
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
Expand All @@ -311,15 +297,15 @@ describe('common/sqlite', () => {
`)

expect(ctx.mocked['console.log'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Introspecting based on datasource defined in prisma/schema.prisma …
`)
expect(
ctx.mocked['console.log'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
Introspecting based on datasource defined in prisma/schema.prisma …
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
Expand All @@ -345,15 +331,15 @@ describe('common/sqlite', () => {
`)

expect(ctx.mocked['console.log'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Introspecting based on datasource defined in prisma/schema.prisma …
`)
expect(
ctx.mocked['console.log'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
Introspecting based on datasource defined in prisma/schema.prisma …
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
Expand Down Expand Up @@ -389,16 +375,16 @@ describe('common/sqlite', () => {
`)

expect(ctx.mocked['console.log'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Introspecting based on datasource defined in prisma/invalid.prisma …
`)
expect(
ctx.mocked['console.log'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/invalid.prisma
Datasource "db": SQLite database "dev.db" at "file:dev.db"
Introspecting based on datasource defined in prisma/invalid.prisma …
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
Expand All @@ -418,18 +404,17 @@ describe('common/sqlite', () => {
ctx.mocked['console.log'].mock.calls
.join('\n')
.replace(/\d{2,3}ms/, 'in XXms'),
).toMatchInlineSnapshot(`
Introspecting based on datasource defined in prisma/invalid.prisma …
✔ Introspected 3 models and wrote them into prisma/invalid.prisma in in XXms
Run prisma generate to generate Prisma Client.
`)
).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/invalid.prisma
Datasource "db": SQLite database "dev.db" at "file:dev.db"
Introspecting based on datasource defined in prisma/invalid.prisma …
✔ Introspected 3 models and wrote them into prisma/invalid.prisma in XXms
Run prisma generate to generate Prisma Client.
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
Expand Down Expand Up @@ -477,11 +462,9 @@ describe('postgresql', () => {
const introspect = new DbPull()
await introspect.parse(['--print'])
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from schema.prisma
Datasource "db": PostgreSQL database "tests-migrate", schema "public" at "localhost:5432"
`)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
Expand Down Expand Up @@ -542,11 +525,9 @@ describe('mysql', () => {
const introspect = new DbPull()
await introspect.parse(['--print'])
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from schema.prisma
Datasource "db": MySQL database "tests" at "localhost:5432"
`)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
Expand Down Expand Up @@ -614,11 +595,9 @@ describe('SQL Server', () => {
const introspect = new DbPull()
await introspect.parse(['--print'])
expect(ctx.mocked['console.log'].mock.calls.join('\n')).toMatchSnapshot()
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from schema.prisma
Datasource "db" - SQL Server
`)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
Expand Down
7 changes: 4 additions & 3 deletions packages/migrate/src/commands/DbPull.ts
Expand Up @@ -88,7 +88,7 @@ Instead of saving the result to the filesystem, you can also print it to stdout

const log = (...messages): void => {
if (!args['--print']) {
console.log(...messages)
console.info(...messages)
}
}

Expand Down Expand Up @@ -127,7 +127,8 @@ Instead of saving the result to the filesystem, you can also print it to stdout
const url: string | undefined = args['--url']
let schemaPath = await getSchemaPath(args['--schema'])

if (schemaPath) {
// Do not print if --print is passed to only have the schema in stdout
if (schemaPath && !args['--print']) {
console.info(
chalk.dim(
`Prisma schema loaded from ${path.relative(
Expand Down Expand Up @@ -214,7 +215,7 @@ Then you can run ${chalk.green(
}
} else if (e.code === 'P1012') {
// Schema Parsing Error
console.log() // empty line
console.info() // empty line
throw new Error(`${chalk.red(
`${e.code} Introspection failed as your current Prisma schema file is invalid`,
)}\n
Expand Down

0 comments on commit 7c8693b

Please sign in to comment.