Skip to content

Commit

Permalink
fix(migrate): reset in the baseline case when using migrate dev (#4538)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolg42 committed Dec 8, 2020
1 parent 56c2acc commit 3968a6c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 41 deletions.
40 changes: 14 additions & 26 deletions src/packages/migrate/src/__tests__/MigrateDev.test.ts
Expand Up @@ -343,7 +343,7 @@ describe('sqlite', () => {
it('transition-db-push-migrate (prompt reset yes)', async () => {
ctx.fixture('transition-db-push-migrate')

prompt.inject(['first', 'y'])
prompt.inject(['y', 'first'])

const result = MigrateDev.new().parse(['--preview-feature'])

Expand All @@ -356,14 +356,9 @@ describe('sqlite', () => {
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
Drift detected: Your database schema is not in sync with your migration history.
The following migration was created from new schema changes:
migrations/
└─ 20201231000000_first/
└─ migration.sql
Drift detected: Your database schema is not in sync with your migration history.
The following migration(s) have been created and applied from new schema changes:
Expand All @@ -381,7 +376,7 @@ describe('sqlite', () => {
ctx.fixture('transition-db-push-migrate')
const mockExit = jest.spyOn(process, 'exit').mockImplementation()

prompt.inject(['first', new Error()])
prompt.inject([new Error(), 'first'])

const result = MigrateDev.new().parse(['--preview-feature'])

Expand All @@ -392,13 +387,6 @@ describe('sqlite', () => {
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
The following migration was created from new schema changes:
migrations/
└─ 20201231000000_first/
└─ migration.sql
Drift detected: Your database schema is not in sync with your migration history.
Expand Down Expand Up @@ -446,7 +434,7 @@ describe('sqlite', () => {
ctx.fixture('edited-and-draft')
fs.remove('prisma/migrations/20201117144659_test')

prompt.inject(['y'])
prompt.inject(['mymigration'])

const result = MigrateDev.new().parse(['--preview-feature'])

Expand Down Expand Up @@ -476,7 +464,7 @@ describe('sqlite', () => {
The following migration(s) have been created and applied from new schema changes:
migrations/
└─ 20201231000000_/
└─ 20201231000000_mymigration/
└─ migration.sql
`)
Expand Down Expand Up @@ -645,11 +633,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 @@ -688,10 +676,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 +701,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
24 changes: 9 additions & 15 deletions src/packages/migrate/src/commands/MigrateDev.ts
Expand Up @@ -40,9 +40,7 @@ export class MigrateDev implements Command {
}

private static help = format(`
${
process.platform === 'win32' ? '' : chalk.bold('🏋️ ')
}Create a migration from changes in Prisma schema, apply it to the database, trigger generators (e.g. Prisma Client)
Create a migration from changes in Prisma schema, apply it to the database, trigger generators (e.g. Prisma Client)
${chalk.bold.yellow('WARNING')} ${chalk.bold(
`Prisma's migration functionality is currently in Preview (${link(
Expand Down Expand Up @@ -149,7 +147,7 @@ ${chalk.bold('Examples')}
debug({ diagnoseResult })

let isResetNeeded = false
let isResetNeededAfterCreate = false
const isResetNeededAfterCreate = false
let migrationIdsFromDatabaseIsBehind: string[] = []
let migrationIdsFromAfterReset: string[] = []

Expand Down Expand Up @@ -238,17 +236,13 @@ ${
${diagnoseResult.drift.error.message}`,
)
} else if (diagnoseResult.drift.diagnostic === 'driftDetected') {
if (diagnoseResult.hasMigrationsTable === false) {
isResetNeededAfterCreate = true
} else {
// we could try to fix the drift in the future
console.info() // empty line
console.info(
'Drift detected: Your database schema is not in sync with your migration history.',
)
console.info() // empty line
isResetNeeded = true
}
// we could try to fix the drift in the future
console.info() // empty line
console.info(
'Drift detected: Your database schema is not in sync with your migration history.',
)
console.info() // empty line
isResetNeeded = true
}
}

Expand Down

0 comments on commit 3968a6c

Please sign in to comment.