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.
The database had the right tables. Its record of how they got there didn't match the files.
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:
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.
With 18 local files, 0001 to 0018:
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"]}
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.
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.
The database is fine; only the ledger is wrong. Repair the records, not the schema:
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).
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.
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, and its schema needed indexes on foreign keys. Workflow files fail just as quietly: a colon in a GitHub Actions run step.
Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore · Original article