{"slug": "remote-migration-versions-not-found-in-local-migrations-directory-when-applied", "title": "\"Remote migration versions not found in local migrations directory\": when migrations applied by hand break supabase db push", "summary": "A developer documented three ways that out-of-band SQL execution breaks `supabase db push` and the Supabase GitHub integration: MCP tools record timestamp versions with no matching file, SQL-editor runs record nothing, and duplicate numeric prefixes collide on the `schema_migrations` primary key. After hitting all three in one production CRM project, the developer ported the CLI's `findPendingMigrations` matching logic to JavaScript to reproduce each failure mode and recommended a one-time repair of the migration records followed by routing all production migrations through the integration only.", "body_md": "`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**.\n\n*The database had the right tables. Its record of how they got there didn't match the files.*\n\nThe 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:\n\n**pending.mjs**\n\n``` js\nconst PATTERN = /^([0-9]+)_(.*)\\.sql$/;\n\nexport function findPendingMigrations(localFiles, remoteVersions) {\n  const local = [...localFiles].sort((a, b) => a.match(PATTERN)[1].localeCompare(b.match(PATTERN)[1]));\n  const remote = [...remoteVersions].sort();\n  const unapplied = [],\n    missing = [];\n  let i = 0,\n    j = 0;\n  while (i < remote.length && j < local.length) {\n    const v = local[j].match(PATTERN)[1];\n    if (remote[i] === v) {\n      i++;\n      j++;\n    } // recorded and present\n    else if (remote[i] < v) {\n      missing.push(remote[i]);\n      i++;\n    } // recorded, but no such file\n    else {\n      unapplied.push(local[j]);\n      j++;\n    } // file older than the last recorded version\n  }\n  if (j === local.length) missing.push(...remote.slice(i));\n  if (missing.length) return { kind: \"missing-local\", versions: missing };\n  if (unapplied.length) return { kind: \"missing-remote\", paths: unapplied };\n  return { kind: \"ok\", pending: local.slice(remote.length) };\n}\n```\n\nA 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.\n\nWith 18 local files, `0001` to `0018`:\n\n**node test.mjs**\n\n```\n1. recorded by the CLI                       {\"kind\":\"ok\",\"pending\":[\"0017_m17.sql\",\"0018_m18.sql\"]}\n2. two applied via MCP (timestamp versions)  {\"kind\":\"missing-local\",\"versions\":[\"20261001195451\",\"20261001195733\"]}\n3. 0010-0012 run in the SQL editor           {\"kind\":\"missing-remote\",\"paths\":[\"0010_m10.sql\",\"0011_m11.sql\",\"0012_m12.sql\"]}\n4. two files numbered 0017, one applied      {\"kind\":\"ok\",\"pending\":[\"0017_hardening.sql\"]}\n```\n\n`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\".`0013`, which was recorded, so they look out of order.` 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.\nIn 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.\n\nThe database is fine; only the ledger is wrong. Repair the records, not the schema:\n\n`schema_migrations`` 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.`0018_hardening.sql`).\nIf 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.\n\n`main`, or the CLI in CI. Not the SQL editor, not an assistant's database tool, not a pre-merge test against production.`<timestamp>_<name>.sql` makes collisions unlikely.`supabase db push` compares 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/).\n\nWritten by Ali Ahmed (QuantumBreakz), AI engineer in Lahore · [Original article](https://aliahmed.si/blog/supabase-migration-history-mismatch/)", "url": "https://wpnews.pro/news/remote-migration-versions-not-found-in-local-migrations-directory-when-applied", "canonical_source": "https://dev.to/quantumbreakz/remote-migration-versions-not-found-in-local-migrations-directory-when-migrations-applied-by-31ml", "published_at": "2026-12-01 18:05:26+00:00", "updated_at": "2026-12-01 18:13:53.588892+00:00", "lang": "en", "topics": ["developer-tools", "mlops"], "entities": ["Supabase", "PostgreSQL", "GitHub"], "also_reported_by": [], "takeaway": "Supabase's `db push` matches local file prefixes against `schema_migrations` versions, so any SQL applied via MCP tools or the SQL editor without recording the same version produces \"missing-local\" or \"missing-remote\" errors, and duplicate prefixes like two `0017` files fail on the primary key.", "recommendation": {"advice": "Route all production migrations through the Supabase GitHub integration and repair `schema_migrations` records once, so MCP- or SQL-editor-applied changes don't desync versions and block `db push`.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/remote-migration-versions-not-found-in-local-migrations-directory-when-applied", "markdown": "https://wpnews.pro/news/remote-migration-versions-not-found-in-local-migrations-directory-when-applied.md", "text": "https://wpnews.pro/news/remote-migration-versions-not-found-in-local-migrations-directory-when-applied.txt", "jsonld": "https://wpnews.pro/news/remote-migration-versions-not-found-in-local-migrations-directory-when-applied.jsonld"}}