Skip to content

Commit

Permalink
fix(migrate): db push after reset if unexecutable step (prisma#8540)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolg42 authored and Andrew-Colman committed Aug 7, 2021
1 parent 0d7f30f commit 26bd05f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
10 changes: 10 additions & 0 deletions packages/migrate/src/__tests__/DbPush.test.ts
Expand Up @@ -193,7 +193,17 @@ describe('push', () => {
prompt.inject(['y'])

const result = DbPush.new().parse([])

const sqliteDbSizeBefore = ctx.fs.inspect('prisma/dev.db')!.size

await expect(result).resolves.toMatchInlineSnapshot(``)

const sqliteDbSizeAfter = ctx.fs.inspect('prisma/dev.db')!.size

expect(sqliteDbSizeBefore).toBeGreaterThan(10000)
expect(sqliteDbSizeAfter).toBeGreaterThan(10000)
expect(sqliteDbSizeAfter).toBeLessThan(sqliteDbSizeBefore)

expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Expand Down
32 changes: 20 additions & 12 deletions packages/migrate/src/commands/DbPush.ts
Expand Up @@ -122,7 +122,12 @@ You can now remove the ${chalk.red('--preview-feature')} flag.`)
let wasDatabaseReset = false
if (args['--force-reset']) {
console.info()
await migrate.reset()
try {
await migrate.reset()
} catch (e) {
migrate.stop()
throw e
}
if (dbInfo.dbName && dbInfo.dbLocation) {
console.info(
`The ${dbInfo.dbType} ${dbInfo.schemaWord} "${dbInfo.dbName}" from "${dbInfo.dbLocation}" was successfully reset.`,
Expand Down Expand Up @@ -187,22 +192,25 @@ ${chalk.bold.redBright('All data will be lost.')}
}

try {
// Reset first to remove all structure and data
await migrate.reset()
if (dbInfo.dbName && dbInfo.dbLocation) {
console.info(
`The ${dbInfo.dbType} ${dbInfo.schemaWord} "${dbInfo.dbName}" from "${dbInfo.dbLocation}" was successfully reset.`,
)
} else {
console.info(
`The ${dbInfo.dbType} ${dbInfo.schemaWord} was successfully reset.`,
)
}
wasDatabaseReset = true

// And now we can db push
await migrate.push({})
} catch (e) {
migrate.stop()
throw e
}

if (dbInfo.dbName && dbInfo.dbLocation) {
console.info(
`The ${dbInfo.dbType} ${dbInfo.schemaWord} "${dbInfo.dbName}" from "${dbInfo.dbLocation}" was successfully reset.`,
)
} else {
console.info(
`The ${dbInfo.dbType} ${dbInfo.schemaWord} was successfully reset.`,
)
}
wasDatabaseReset = true
}

if (migration.warnings && migration.warnings.length > 0) {
Expand Down

0 comments on commit 26bd05f

Please sign in to comment.