Skip to content

Commit

Permalink
fix(migrate): better error message for non-interactive during dev / r…
Browse files Browse the repository at this point in the history
…eset (#5600)

Closes #5531
  • Loading branch information
Jolg42 committed Feb 11, 2021
1 parent f36d8a3 commit 35358c2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
28 changes: 16 additions & 12 deletions src/packages/migrate/src/__tests__/MigrateDev.test.ts
Expand Up @@ -81,9 +81,13 @@ describe('common', () => {
it('dev should error in unattended environment', async () => {
ctx.fixture('transition-db-push-migrate')
const result = MigrateDev.new().parse(['--preview-feature'])
await expect(result).rejects.toMatchInlineSnapshot(
`We detected that your environment is non-interactive. Running this command in not supported in this context.`,
)
await expect(result).rejects.toMatchInlineSnapshot(`
Prisma Migrate has detected that the environment is non-interactive, which is not supported.
\`prisma migrate dev\` is an interactive command designed to create new migrations and evolve the database in development.
To apply existing migrations in deployments, use prisma migrate deploy.
See https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-deploy
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
Expand Down Expand Up @@ -626,11 +630,11 @@ describe('sqlite', () => {

await expect(result).rejects.toMatchInlineSnapshot(`
⚠️ We found changes that cannot be executed:
⚠️ We found changes that cannot be executed:
• Step 0 Made the column \`fullname\` on table \`Blog\` required, but there are 1 existing NULL values.
• Step 0 Made the column \`fullname\` on table \`Blog\` required, but there are 1 existing NULL values.
`)
`)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Expand Down Expand Up @@ -689,10 +693,10 @@ describe('sqlite', () => {
expect(ctx.mocked['console.log'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
⚠️ There will be data loss when applying the migration:
⚠️ There will be data loss when applying the migration:
• You are about to drop the \`Blog\` table, which is not empty (2 rows).
`)
• You are about to drop the \`Blog\` table, which is not empty (2 rows).
`)
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})

Expand All @@ -713,10 +717,10 @@ describe('sqlite', () => {
expect(ctx.mocked['console.log'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
⚠️ There will be data loss when applying the migration:
⚠️ There will be data loss when applying the migration:
• You are about to drop the \`Blog\` table, which is not empty (2 rows).
`)
• You are about to drop the \`Blog\` table, which is not empty (2 rows).
`)
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})

Expand Down
9 changes: 6 additions & 3 deletions src/packages/migrate/src/__tests__/MigrateReset.test.ts
Expand Up @@ -194,9 +194,12 @@ describe('reset', () => {
it('reset should error in unattended environment', async () => {
ctx.fixture('reset')
const result = MigrateReset.new().parse(['--preview-feature'])
await expect(result).rejects.toMatchInlineSnapshot(
`We detected that your environment is non-interactive. Running this command in not supported in this context.`,
)
await expect(result).rejects.toMatchInlineSnapshot(`
Prisma Migrate has detected that the environment is non-interactive. It is recommended to run this command in an interactive environment.
Use --force to run this command without user interaction.
See https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-reset
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
Expand Down
7 changes: 5 additions & 2 deletions src/packages/migrate/src/commands/MigrateDev.ts
Expand Up @@ -23,7 +23,10 @@ import {
ExperimentalFlagWithNewMigrateError,
EarlyAccessFeatureFlagWithNewMigrateError,
} from '../utils/flagErrors'
import { NoSchemaFoundError, EnvNonInteractiveError } from '../utils/errors'
import {
NoSchemaFoundError,
MigrateDevEnvNonInteractiveError,
} from '../utils/errors'
import { printMigrationId } from '../utils/printMigrationId'
import { printFilesFromMigrationIds } from '../utils/printFiles'
import {
Expand Down Expand Up @@ -166,7 +169,7 @@ ${chalk.bold('Examples')}
if (!args['--force']) {
// We use prompts.inject() for testing in our CI
if (isCi() && Boolean((prompt as any)._injected?.length) === false) {
throw new EnvNonInteractiveError()
throw new MigrateDevEnvNonInteractiveError()
}

const dbInfo = await getDbInfo(schemaPath)
Expand Down
7 changes: 5 additions & 2 deletions src/packages/migrate/src/commands/MigrateReset.ts
Expand Up @@ -17,7 +17,10 @@ import {
ExperimentalFlagWithNewMigrateError,
EarlyAccessFeatureFlagWithNewMigrateError,
} from '../utils/flagErrors'
import { NoSchemaFoundError, EnvNonInteractiveError } from '../utils/errors'
import {
NoSchemaFoundError,
MigrateResetEnvNonInteractiveError,
} from '../utils/errors'
import { printFilesFromMigrationIds } from '../utils/printFiles'
import { throwUpgradeErrorIfOldMigrate } from '../utils/detectOldMigrate'
import { ensureDatabaseExists } from '../utils/ensureDatabaseExists'
Expand Down Expand Up @@ -130,7 +133,7 @@ ${chalk.bold('Examples')}
if (!args['--force']) {
// We use prompts.inject() for testing in our CI
if (isCi() && Boolean((prompt as any)._injected?.length) === false) {
throw new EnvNonInteractiveError()
throw new MigrateResetEnvNonInteractiveError()
}

console.info() // empty line
Expand Down
27 changes: 25 additions & 2 deletions src/packages/migrate/src/utils/errors.ts
Expand Up @@ -57,10 +57,33 @@ export class MigrateNeedsForceError extends Error {
}
}

export class EnvNonInteractiveError extends Error {
export class MigrateResetEnvNonInteractiveError extends Error {
constructor() {
super(
`We detected that your environment is non-interactive. Running this command in not supported in this context.`,
`Prisma Migrate has detected that the environment is non-interactive. It is recommended to run this command in an interactive environment.
Use ${chalk.bold.greenBright(
`--force`,
)} to run this command without user interaction.
See ${link(
'https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-reset',
)}`,
)
}
}

export class MigrateDevEnvNonInteractiveError extends Error {
constructor() {
super(
`Prisma Migrate has detected that the environment is non-interactive, which is not supported.
\`prisma migrate dev\` is an interactive command designed to create new migrations and evolve the database in development.
To apply existing migrations in deployments, use ${chalk.bold.greenBright(
`prisma migrate deploy`,
)}.
See ${link(
'https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-deploy',
)}`,
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/packages/migrate/src/utils/handleEvaluateDataloss.ts
Expand Up @@ -2,7 +2,7 @@ import chalk from 'chalk'
import prompt from 'prompts'
import { isCi } from '@prisma/sdk'
import { MigrationFeedback } from '../types'
import { EnvNonInteractiveError } from './errors'
import { MigrateDevEnvNonInteractiveError } from './errors'

export function handleUnexecutableSteps(
unexecutableSteps: MigrationFeedback[],
Expand Down Expand Up @@ -45,7 +45,7 @@ export async function handleWarnings(
if (!force) {
// We use prompts.inject() for testing in our CI
if (isCi() && Boolean((prompt as any)._injected?.length) === false) {
throw new EnvNonInteractiveError()
throw new MigrateDevEnvNonInteractiveError()
} else {
const confirmation = await prompt({
type: 'confirm',
Expand Down

0 comments on commit 35358c2

Please sign in to comment.