From 7c8693ba2cdd4216aabecb1d57b1c3553b5eb8dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=CC=88l?= Date: Tue, 3 Aug 2021 12:38:21 +0200 Subject: [PATCH] fix(db pull): only output schema to stdout when using --print Closes #8321 --- packages/migrate/src/__tests__/DbPull.test.ts | 201 ++++++++---------- packages/migrate/src/commands/DbPull.ts | 7 +- 2 files changed, 94 insertions(+), 114 deletions(-) diff --git a/packages/migrate/src/__tests__/DbPull.test.ts b/packages/migrate/src/__tests__/DbPull.test.ts index db0a727445e0..c5d793c10177 100644 --- a/packages/migrate/src/__tests__/DbPull.test.ts +++ b/packages/migrate/src/__tests__/DbPull.test.ts @@ -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(``) @@ -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(``) @@ -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(``) @@ -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(``) @@ -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'), @@ -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'), @@ -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'), @@ -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, @@ -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'), @@ -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'), @@ -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'), @@ -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'), @@ -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'), @@ -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(``) @@ -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(``) @@ -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(``) diff --git a/packages/migrate/src/commands/DbPull.ts b/packages/migrate/src/commands/DbPull.ts index 37fbbafbb9f2..4e2a54ddd814 100644 --- a/packages/migrate/src/commands/DbPull.ts +++ b/packages/migrate/src/commands/DbPull.ts @@ -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) } } @@ -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( @@ -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