# "Remote migration versions not found in local migrations directory": when migrations applied by hand break supabase db push

supabase db push decides what to apply by comparing the versions recorded in the schema_migrations table with your migration file names. Migrations applied through an MCP tool, the SQL editor or another branch break that comparison in three distinct ways. Reproduced with the CLI's own matching logic, with the one-time repair and the rule that prevents it.

Published: 2026-12-01

`supabase db push` (and the Supabase GitHub integration) decides which migrations to run by comparing two lists: the **versions recorded** in the `schema_migrations` table (in the `supabase_migrations` schema) on the database, and the **numeric prefixes of your migration files**, such as `0017` in `0017_report_self_delete.sql`. Anything that applies SQL **without recording the same version** breaks that comparison. A migration applied through an **MCP tool** records a timestamp version the files don't have, so push fails with **"Remote migration versions not found in local migrations directory"**. A migration pasted into the **SQL editor** records nothing, so the CLI sees local files missing from the middle of the history. And **two branches that both add `0017`** collide on the version column, which is the table's primary key. I hit all three in one production project and reproduced each with the CLI's own matching code. The fix is a **one-time repair of the records**, then one rule: **production migrations go through the integration only**.

![How supabase db push compares history. Local files 0001 to 0018; the remote table records versions. Recorded by the CLI as 0001 to 0016: the plan is ok and 0017 and 0018 are pending. Two migrations applied through an MCP tool recorded timestamp versions such as 20261001195451: missing-local, push fails with remote migration versions not found in local migrations directory. Three migrations run in the SQL editor were never recorded: missing-remote, local 0010 to 0012 sit before the last applied version. Two branches both added 0017: the second is planned as pending, and recording it fails on the schema\_migrations primary key. Fix: rewrite the records to match the files once, then apply production migrations only through the integration.](https://aliahmed.si/figures/migration-history.webp)

*The database had the right tables. Its record of how they got there didn't match the files.*

## How does the CLI decide what to apply?

The CLI reads every `version` from that table, in order, parses each local file name as `<digits>_<name>.sql`, and walks both sorted lists together. This is its `findPendingMigrations`, ported from the CLI's source to JavaScript:

```js title="pending.mjs"
const PATTERN = /^([0-9]+)_(.*)\.sql$/;

export function findPendingMigrations(localFiles, remoteVersions) {
  const local = [...localFiles].sort((a, b) => a.match(PATTERN)[1].localeCompare(b.match(PATTERN)[1]));
  const remote = [...remoteVersions].sort();
  const unapplied = [],
    missing = [];
  let i = 0,
    j = 0;
  while (i < remote.length && j < local.length) {
    const v = local[j].match(PATTERN)[1];
    if (remote[i] === v) {
      i++;
      j++;
    } // recorded and present
    else if (remote[i] < v) {
      missing.push(remote[i]);
      i++;
    } // recorded, but no such file
    else {
      unapplied.push(local[j]);
      j++;
    } // file older than the last recorded version
  }
  if (j === local.length) missing.push(...remote.slice(i));
  if (missing.length) return { kind: "missing-local", versions: missing };
  if (unapplied.length) return { kind: "missing-remote", paths: unapplied };
  return { kind: "ok", pending: local.slice(remote.length) };
}
```

A version recorded on the database with **no matching file** is `missing-local`. A file **older than the last recorded version** but not itself recorded is `missing-remote`, refused unless you pass `--include-all`. Otherwise, the files past the end of the history are pending.

## What do the three failures look like?

With 18 local files, `0001` to `0018`:

```text title="node test.mjs"
1. recorded by the CLI                       {"kind":"ok","pending":["0017_m17.sql","0018_m18.sql"]}
2. two applied via MCP (timestamp versions)  {"kind":"missing-local","versions":["20261001195451","20261001195733"]}
3. 0010-0012 run in the SQL editor           {"kind":"missing-remote","paths":["0010_m10.sql","0011_m11.sql","0012_m12.sql"]}
4. two files numbered 0017, one applied      {"kind":"ok","pending":["0017_hardening.sql"]}
```

* **MCP-applied migrations.** An AI assistant's database tool applied the early migrations and recorded them under timestamp versions like `20261001195451`, with the file's name in the `name` column. No file starts with that number, so the integration stops with "Remote migration versions not found in local migrations directory".
* **SQL editor.** Three migrations were run by hand. Their tables exist, but no version was recorded. Their files now sit before `0013`, which was recorded, so they look out of order.
* **Two branches, one number.** One branch added `0017_report_self_delete.sql` and another `0017_hardening.sql`. After the first was applied, the plan calls the second "pending", but recording it means inserting version `0017` again. On PostgreSQL that fails with `duplicate key value violates unique constraint "schema_migrations_pkey"`, because `version` is the table's primary key.

In the CRM I built, all three had happened by the time the GitHub integration was switched on: MCP timestamps for the first migrations, `0010` to `0012` run in the SQL editor, one migration recorded under a different name, and one pull request's migration applied to production before it was merged.

## How do you repair it?

The database is fine; only the ledger is wrong. Repair the records, not the schema:

1. **Check that each migration's objects really exist** (tables, columns, policies, functions), one migration at a time.
2. **Rewrite `schema_migrations`** so it holds exactly the versions of your files, `0001` to `0018`, with matching names. The CLI's `supabase migration repair --status applied <version>` inserts a record without running the SQL, and `--status reverted` removes one; a single SQL statement in the editor does the same when there are many.
3. **Renumber the colliding file** before it's merged (`0017_hardening.sql` became `0018_hardening.sql`).
4. **Run the plan again.** The integration should report nothing to apply.

If your own scripts also apply migrations, make them treat a file as applied when the ledger holds **either its version or its name**, so the two tools agree during the transition. The CRM's `db-push` script does that.

## How do you keep it from happening again?

* **Production migrations go through one path**: the integration on merge to `main`, or the CLI in CI. Not the SQL editor, not an assistant's database tool, not a pre-merge test against production.
* **Number migrations at merge time, or use timestamps.** Sequential numbers chosen on branches collide; the CLI's default `<timestamp>_<name>.sql` makes collisions unlikely.
* **Treat the ledger as part of the schema.** A migration applied but not recorded is as wrong as one recorded but not applied.

## Summary

* `supabase db push` compares **recorded versions** with **file-name prefixes**, using a two-pointer walk.
* **Timestamp versions from MCP tools** cause "remote migration versions not found"; **unrecorded SQL-editor runs** look out of order; **duplicate numbers** collide on the **primary key**.
* **Repair the ledger, not the schema**: verify objects exist, then rewrite the records to match the files.
* Apply production migrations **through one path only**.

The same project's tests had their own hidden assumption about shared data: [a fake clock and a shared table](https://aliahmed.si/blog/fake-clock-shared-database-tests/), and its schema needed [indexes on foreign keys](https://aliahmed.si/blog/postgres-foreign-key-index/). Workflow files fail just as quietly: [a colon in a GitHub Actions run step](https://aliahmed.si/blog/github-actions-yaml-colon-run-step/).

---

Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore · [Original article](https://aliahmed.si/blog/supabase-migration-history-mismatch/)
