Skip to content

Commit

Permalink
fix(migrate): run seed after apply migrations in migrate dev (#5204)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolg42 committed Jan 20, 2021
1 parent c99ecc3 commit 364290c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
31 changes: 14 additions & 17 deletions src/packages/migrate/src/__tests__/MigrateDev.test.ts
Expand Up @@ -361,7 +361,6 @@ describe('sqlite', () => {
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
The following migration(s) have been created and applied from new schema changes:
migrations/
Expand Down Expand Up @@ -410,7 +409,6 @@ describe('sqlite', () => {
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
The following migration(s) have been applied:
migrations/
Expand Down Expand Up @@ -441,7 +439,6 @@ describe('sqlite', () => {
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
The following migration(s) have been applied:
migrations/
Expand Down Expand Up @@ -598,11 +595,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 @@ -661,10 +658,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 @@ -685,10 +682,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 Expand Up @@ -733,8 +730,6 @@ describe('sqlite', () => {
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
Running \`node seed.js\` ...
The following migration(s) have been applied:
migrations/
Expand All @@ -743,6 +738,8 @@ describe('sqlite', () => {
└─ 20201231000000_draft/
└─ migration.sql
Running \`node seed.js\` ...
`)
expect(ctx.mocked['console.log'].mock.calls.join()).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls.join()).toMatchSnapshot()
Expand All @@ -765,8 +762,6 @@ describe('sqlite', () => {
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
Running \`node seed.js\` ...
The following migration(s) have been applied:
migrations/
Expand All @@ -775,6 +770,8 @@ describe('sqlite', () => {
└─ 20201231000000_draft/
└─ migration.sql
Running \`node seed.js\` ...
`)
expect(ctx.mocked['console.log'].mock.calls.join()).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls.join()).toContain(
Expand All @@ -800,7 +797,6 @@ describe('sqlite', () => {
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
The following migration(s) have been applied:
migrations/
Expand All @@ -809,6 +805,7 @@ describe('sqlite', () => {
└─ 20201231000000_draft/
└─ migration.sql
`)
expect(ctx.mocked['console.log'].mock.calls.join()).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls.join())
Expand Down
28 changes: 15 additions & 13 deletions src/packages/migrate/src/commands/MigrateDev.ts
Expand Up @@ -172,19 +172,6 @@ ${chalk.bold('Examples')}

// Do the reset
await migrate.reset()

// Run seed if 1 or more seed files are present
// And catch the error to continue execution
try {
const detected = detectSeedFiles()
if (detected.numberOfSeedFiles > 0) {
await tryToRunSeed()
}
} catch (e) {
console.error(e)
} finally {
console.info() // empty line
}
}

const { appliedMigrationNames } = await migrate.applyMigrations()
Expand All @@ -200,6 +187,21 @@ ${chalk.bold('Examples')}
)
}

// If database was reset we want to run the seed
if (devDiagnostic.action.tag === 'reset') {
// Run seed if 1 or more seed files are present
// And catch the error to continue execution
try {
const detected = detectSeedFiles()
if (detected.numberOfSeedFiles > 0) {
console.info() // empty line
await tryToRunSeed()
}
} catch (e) {
console.error(e)
}
}

const evaluateDataLossResult = await migrate.evaluateDataLoss()
debug({ evaluateDataLossResult })

Expand Down

0 comments on commit 364290c

Please sign in to comment.