Skip to content

Maintenance, Support & Optimisation

Drizzle migrations not applied in production

Why Drizzle migrations are skipped or fail in production, read from Drizzle's own code, with an ERP whose every login failed and the fix that stopped it.

Ridam Agrawal

Founder, Easinvy

Published
Read
6 min
On this page

In short

Why are my Drizzle migrations not applied in production?

Drizzle's migrator reads only the newest row of drizzle.__drizzle_migrations and runs each journal entry whose when timestamp is later. It never compares hashes. So a regenerated migration, a database with no history, or a history row stamped too late can leave production behind its code, failing on "already exists" or skipping silently.

On the morning of 30 October 2026, an ERP we built and run for a manufacturer stopped letting anyone sign in. The new code expected a table the production database did not have yet, because the migrations that create it had not run. This note reads why that can happen from Drizzle's own source, lists the three ways a database falls behind its migrations, and shows the fix that now stops the ERP from ever starting on an older schema. Everything below is drizzle-orm 0.45.4, the version npm marks as latest on 16 November 2026.

What happened to the ERP on 30 October 2026

The ERP's release of 30 October 2026 added role-based access control. It came with a migration, 0001_roles_rbac, that creates a roles table and gives each user a role, and a login query that joins the two. Production served the new code against a database still at the previous schema. In the words of the commit that fixed it: "login selects users.role_id and joins roles, neither existed, and every sign-in returned the generic 500." The fix was committed at 12:10 IST the same day. The record holds commit times, not the time of the first failed sign-in, so this note does not say how long it lasted.

How Drizzle decides which migrations to run

Drizzle keeps its history in a table, drizzle.__drizzle_migrations, one row per applied migration: the file's hash and the migration's timestamp, its when in meta/_journal.json. The migrate method reads one row from that table, the newest, and compares every migration in the journal with it. Abridged:

const dbMigrations = await session.all(
  sql`select id, hash, created_at from drizzle.__drizzle_migrations order by created_at desc limit 1`
)
const lastDbMigration = dbMigrations[0]
await session.transaction(async (tx) => {
  for await (const migration of migrations) {
    if (!lastDbMigration || Number(lastDbMigration.created_at) < migration.folderMillis) {
      // run the migration's statements, then insert its hash and folderMillis
    }
  }
})

folderMillis is the journal's when. Three things follow. Only timestamps decide what runs: the hash is written and never read back. Every pending migration runs in one transaction, so one failure rolls all of them back. And nothing takes a lock, so two processes can run the same migration at once. The rule is the same whichever way the migrator is called: drizzle-kit migrate (0.31.11) imports this migrator from drizzle-orm, and the ERP calls its migrate() function from a script of its own. We use Drizzle the same way on our other products, as in Multi-tenancy in Drizzle, from a shipped product.

Three ways a database falls behind its migrations

How the database got thereWhat the migrator doesWhat you see
A migration it already applied was regenerated, and got a later whenRuns that migration againrelation "…" already exists, and the whole batch rolls back
It has no history: built with drizzle-kit push, or copied without the drizzle schemaRuns everything from the first migrationThe same error, on the first CREATE
Its newest history row is later than the pending migrations' when (a row written by hand with the current time, or a merge that reordered timestamps)Skips every migration older than that rowSuccess, exit code 0, nothing applied

The third row is the one reported as drizzle-orm issue #5769, open since May 2026. The ERP's journal shows how easily the first one happens: its first migration, 0000_init, was regenerated twice in one night on 13 October 2026, its when moving from 13:00 to 20:53 to 21:46 UTC. A database set up from either earlier version holds a history row older than the final 0000_init, so Drizzle tries to create its tables a second time.

Why the pre-deploy step did not stop it

The ERP runs on Railway, and its railway.json has run npm run db:migrate as a pre-deploy command since its first commit. Railway's pre-deploy documentation says a pre-deploy command runs between building and deploying, in a separate container, and that "If your command fails, it will not be retried and the deployment will not proceed." A migrate that failed on "already exists" should therefore have stopped the release. It did not, and the record does not say why. Railway's documented behaviour leaves two possibilities: the command exited 0 without applying the new migrations, which is the third row of the table above, or it did not run for that deployment. We do not know which. The fix does not depend on knowing.

Migrations in the start command, as well as before it

The fix changed one line of package.json:

"start": "tsx src/db/migrate.ts && tsx src/server.ts"

The && means the server starts only after every migration has applied. A database behind its code now stops the new version from starting at all, and Railway makes a new deployment active only once its health check returns a 2xx, so the release is marked failed instead of serving 500s. Drizzle's own Node.js and Railway tutorial applies migrations on application startup by default, and offers the pre-deploy step for zero-downtime deploys. The ERP keeps both: the pre-deploy step runs first, and the start command makes sure. A health check that asks the database has its own cost on a serverless database, which Neon free tier compute hours and a health check measures. One caution applies to any service with more than one replica: since the migrator takes no lock, replicas starting together can each try to apply the same migration, so run it in one place there.

Recording an existing schema without skipping anything

The start command alone would still fail on a database already in the first two rows of the table. So the migration script first checks for that case, and only that case. Abridged:

// only when the database really has the existing schema's tables
if (!schema.rows[0]?.v2) return
const last = await db.execute(sql`select max(created_at)::text as at from drizzle.__drizzle_migrations`)
const at = last.rows[0]?.at
if (at !== null && at !== undefined && Number(at) >= init.when) return
const hash = createHash("sha256").update(readFileSync(path.join(FOLDER, `${init.tag}.sql`), "utf8")).digest("hex")
await db.execute(sql`insert into drizzle.__drizzle_migrations (hash, created_at) values (${hash}, ${init.when})`)

The check looks for a table and a column that only the existing schema has, so a database in any other state still fails loudly. The row it writes is 0000_init's own hash and its own when from the journal, not the time of the repair, so the migrator then applies 0001 onwards as it should. A row stamped with the current time would sit after every later migration's when, and the migrator would skip them all and report success: the third row of the table, written by the repair.

Five databases, checked before the fix shipped

The fix's commit records starting the server and signing in against five databases: the previous schema with its history, without history, with a stale history row, an empty database, and an up-to-date one. It also records the stock and API test suites passing, 24 and 19 tests. We cite the commit as written; the checks were not re-run for this note. The repository has added four migrations since, 0004 to 0007.

A checklist for Drizzle migrations in production

  1. Never edit or regenerate a migration that any database has applied. Generate a new one: an edited file will not run again, and a regenerated one will.
  2. Keep when increasing in _journal.json. After merging branches, check that no new migration is older than one already applied somewhere.
  3. Do not build a production database with drizzle-kit push and migrate it later. Record its schema first, as above.
  4. If you write a history row by hand, stamp it with the journal's when, never the current time.
  5. Run the migrator in the start command, joined to the server with &&, so new code never starts on an older schema. Keep a pre-deploy step as well if you want the failure before traffic.
  6. With several replicas, run migrations in one place: the migrator takes no lock.
  7. After every deploy, compare select max(created_at) from drizzle.__drizzle_migrations with the last when in _journal.json.

Easinvy runs maintenance and support for the systems it builds, and fixes them the day they break.

Questions people ask

Why does drizzle-kit migrate say it succeeded when nothing changed?
Because the newest created_at in drizzle.__drizzle_migrations is later than the pending migrations' when timestamps, so the migrator skips them and exits cleanly. It usually comes from a history row written by hand with the current time, or a merge that reordered timestamps.
Can I edit or regenerate a Drizzle migration that has already run?
Not safely. The migrator stores each file's hash but never compares it, so an edited migration will not run again anywhere it already ran. A regenerated one gets a newer timestamp, so it runs again and fails on tables that already exist.
What is the difference between drizzle-kit push and migrate?
push compares your schema with the database and changes the database directly, writing no row to drizzle.__drizzle_migrations. migrate applies the SQL files from drizzle-kit generate and records each one. So a database built with push and migrated later has no history, and migrate tries to create every table again.
Should Drizzle migrations run before deploy or at startup?
Either works, and both together is safer. A pre-deploy step stops a bad deploy before it takes traffic. Running the migrator in the start command, joined with &&, means new code can never start against an older schema, whatever the pre-deploy step did.
How do I see which migrations a production database has?
Read drizzle.__drizzle_migrations. Each applied migration is a row whose created_at equals that migration's when in meta/_journal.json, so the newest created_at should equal the journal's last when. If it is older, migrations are pending; if it is newer, they will be skipped.

Sources

Ridam Agrawal

Founder and engineer at Easinvy. Writes up the parts of building and running a multi-tenant product that were expensive to get wrong. More about the studio →

Keep reading