From 3c9ee5d3b497eb33179ac5366c092ea74cf36b8d Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Mon, 5 May 2025 09:11:10 -0400 Subject: [PATCH] fix(db-*): migration batch not incrementing past 1 (#12215) When `payload migrate` is run and a record with name "dev" is returned having `batch: -1`, then the `batch` is not incrementing as expected as it is stuck at 1. This change makes it so the batch is incremented from the correct latest batch, ignoring the `name: "dev"` migration. --- packages/drizzle/src/migrate.ts | 47 ++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/packages/drizzle/src/migrate.ts b/packages/drizzle/src/migrate.ts index 0256031ac6..fa422f42c6 100644 --- a/packages/drizzle/src/migrate.ts +++ b/packages/drizzle/src/migrate.ts @@ -42,33 +42,36 @@ export const migrate: DrizzleAdapter['migrate'] = async function migrate( limit: 0, sort: '-name', })) + + if (migrationsInDB.find((m) => m.batch === -1)) { + const { confirm: runMigrations } = await prompts( + { + name: 'confirm', + type: 'confirm', + initial: false, + message: + "It looks like you've run Payload in dev mode, meaning you've dynamically pushed changes to your database.\n\n" + + "If you'd like to run migrations, data loss will occur. Would you like to proceed?", + }, + { + onCancel: () => { + process.exit(0) + }, + }, + ) + + if (!runMigrations) { + process.exit(0) + } + // ignore the dev migration so that the latest batch number increments correctly + migrationsInDB = migrationsInDB.filter((m) => m.batch !== -1) + } + if (Number(migrationsInDB?.[0]?.batch) > 0) { latestBatch = Number(migrationsInDB[0]?.batch) } } - if (migrationsInDB.find((m) => m.batch === -1)) { - const { confirm: runMigrations } = await prompts( - { - name: 'confirm', - type: 'confirm', - initial: false, - message: - "It looks like you've run Payload in dev mode, meaning you've dynamically pushed changes to your database.\n\n" + - "If you'd like to run migrations, data loss will occur. Would you like to proceed?", - }, - { - onCancel: () => { - process.exit(0) - }, - }, - ) - - if (!runMigrations) { - process.exit(0) - } - } - const newBatch = latestBatch + 1 // Execute 'up' function for each migration sequentially