mirror of
https://github.com/bitbaum/orangecat.git
synced 2026-12-04 22:27:36 +01:00
main is red and CD has stopped deploying for the whole repo. Migration
Replay aborts with:
ERROR: duplicate key value violates unique constraint
"schema_migrations_pkey"
Key (version)=(20260826180000) already exists.
Supabase keys its ledger on the numeric PREFIX of the filename
(supabase_migrations.schema_migrations.version, a PRIMARY KEY), not on
the whole name. Two sessions working in parallel each timestamped a
migration 20260826180000 — mention_queue_all_mentions and
resolve_username_history_rpc — so to the ledger they are one migration,
and replaying them dies.
git cannot catch this. Neither file conflicts with the other: different
names, different contents, no overlapping lines. Only the ledger notices,
and by then it is on main, where it blocks the deploy carrying the fix
each session was waiting on.
Renamed mine to 20260826181500. Nothing else about it changes; the
collision is entirely in the filename.
Then closed the class, because "pick a unique timestamp" is exactly the
kind of instruction that works until two people are typing at once:
scripts/check-migration-versions.mjs fails when any two migrations share
a version prefix, and it runs inside `verify`, so a collision costs one
red PR check instead of a blocked pipeline. Verified by reintroducing a
duplicate and watching it name both files, then passing on a clean tree.
npm run verify green; 2385 tests pass.
Co-authored-by: Georgy Butaev <41178744+g-but@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
58 lines
2.3 KiB
JavaScript
58 lines
2.3 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* One migration per version number.
|
|
*
|
|
* Supabase keys its ledger on the numeric prefix of the filename
|
|
* (supabase_migrations.schema_migrations.version, a PRIMARY KEY), not on the
|
|
* whole name. So two files that merely SHARE a prefix are the same migration as
|
|
* far as the ledger is concerned, and replaying them dies on:
|
|
*
|
|
* ERROR: duplicate key value violates unique constraint "schema_migrations_pkey"
|
|
* Key (version)=(20260826180000) already exists.
|
|
*
|
|
* That is not a theoretical clash. It happened on 2026-08-26: two sessions
|
|
* working in parallel each timestamped a migration 20260826180000
|
|
* (mention_queue_all_mentions, resolve_username_history_rpc), main went red,
|
|
* and CD stopped deploying for the whole repo — including the deploy carrying
|
|
* the fix each of them was waiting on.
|
|
*
|
|
* git does not catch it, because neither file conflicts with the other: they
|
|
* have different names and different contents. Only the ledger notices, and by
|
|
* then it is on main. So this runs in CI, where a collision costs one red PR
|
|
* check instead of a blocked pipeline.
|
|
*
|
|
* Run: node scripts/check-migration-versions.mjs
|
|
*/
|
|
|
|
import { readdirSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
|
|
const DIR = join(process.cwd(), 'supabase', 'migrations');
|
|
|
|
const byVersion = new Map();
|
|
for (const name of readdirSync(DIR)) {
|
|
if (!name.endsWith('.sql')) continue;
|
|
const version = name.match(/^(\d+)/)?.[1];
|
|
if (!version) {
|
|
console.error(`✗ ${name} has no leading version number — supabase cannot record it`);
|
|
process.exit(1);
|
|
}
|
|
if (!byVersion.has(version)) byVersion.set(version, []);
|
|
byVersion.get(version).push(name);
|
|
}
|
|
|
|
const collisions = [...byVersion.entries()].filter(([, files]) => files.length > 1);
|
|
|
|
if (collisions.length > 0) {
|
|
console.error('✗ migration version collision — supabase_migrations.schema_migrations.version is a PRIMARY KEY,');
|
|
console.error(' so these files are the same migration to the ledger and a replay will abort:\n');
|
|
for (const [version, files] of collisions) {
|
|
console.error(` ${version}:`);
|
|
for (const f of files) console.error(` ${f}`);
|
|
}
|
|
console.error('\n Fix: bump one filename to an unused timestamp. Nothing else needs to change.');
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`✓ migration versions unique (${byVersion.size} migrations)`);
|