#17033·medusa

[Bug]: db:migrate exits 0 when a migration script fails (db:migrate:scripts exit code is discarded)

Author: kaizen-hryyCreated Sep 27, 2026Updated Sep 29, 2026
Labelsgood first issuetype: bug

Package.json file

json
{
  "dependencies": {
    "@medusajs/admin-sdk": "2.19.0",
    "@medusajs/cli": "2.19.0",
    "@medusajs/framework": "2.19.0",
    "@medusajs/medusa": "2.19.0"
  }
}

The code below is unchanged on develop at 62a5200, and the permalinks point there.

Node.js version

v22.22.2

Database and its version

PostgreSQL 17.11

Operating system name and version

Ubuntu 24.04.2 LTS

Browser name

n/a (CLI)

What happended?

medusa db:migrate exits 0 when a migration script throws. It forks db:migrate:scripts as a child process and discards the child's exit code. The same function checks the exit code of the db:migrate:search child a few lines above:

packages/medusa/src/commands/db/migrate.ts#L122-L141

typescript
    const exitCode = await runCliCommand("db:migrate:search", directory, searchArgs)
    // Reported rather than swallowed: ...
    if (exitCode !== 0) {
      return false
    }
  }

  if (!skipScripts) {
    logger.log(new Array(TERMINAL_SIZE).join("-"))
    await runCliCommand("db:migrate:scripts", directory)   // exit code discarded
  }

  return true

A migration script cannot work around this from the inside, because it runs in the child whose exit status is dropped. So any deploy or CI step that runs medusa db:migrate and trusts its exit code carries on as if every script succeeded.

That compounds two behaviours of MigrationScriptsMigrator that are otherwise reasonable:

  • On failure it deletes the script's script_migrations row, so the script silently runs again on the next migrate, against whatever state the failed attempt left behind.
  • It aborts the loop on the first failure, so every script sorted after the failed one never runs, and nothing reports that either.

Expected behavior

db:migrate exits non-zero when db:migrate:scripts does, the same way it already does for db:migrate:search:

typescript
    const exitCode = await runCliCommand("db:migrate:scripts", directory)
    if (exitCode !== 0) {
      return false
    }

Actual behavior

Reproduced on a fresh database with one extra file, src/migration-scripts/zz-deliberate-failure.ts:

typescript
export default async function deliberateFailure() {
  throw new Error("deliberate migration-script failure")
}
Command Exit Log
npx medusa db:migrate 0 error: Failed to run migration script …/zz-deliberate-failure.ts:: deliberate migration-script failure plus a stack trace
npx medusa db:migrate:scripts 1 same error
npx medusa db:migrate --skip-scripts && npx medusa db:migrate:scripts 1 same error

It is the same in a production image (NODE_ENV=production, run via docker compose run): npx medusa db:migrate exits 0 with the error in the JSON log, and npx medusa db:migrate:scripts exits 1.

Workaround for anyone hitting this: run the halves separately, medusa db:migrate --skip-scripts && medusa db:migrate:scripts.

Link to reproduction repo

None needed: any Medusa 2.x app plus the one-file script above.

Context

This is the fourth report from one project where a core operation reports success after doing less than it should: #16998 (prices silently missing past 4,000 price sets), #17003 (ordering silently lost across batches), #17030 (claim or exchange confirmed with no reservation), and this one. The fixes are unrelated, but a return false or a thrown error at those points would have caught each of them the day they happened instead of later.