mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-16 17:09:09 +00:00
fix(db): freeze the old migration series at 241 after 240 and 241 landed
While this PR was open, merge train 2 merged 240_provider_node_custom_json_config.sql (#18353) and 241_seen_models.sql (#18359) into release/v3.8.54. With the series frozen at 239, the gate rejected both base files and the compatibility tests' synthetic 240 slot collided with the real one, so CI went red on the merge ref. A merged migration is never renamed, so the freeze moves to the highest old number on the base: LEGACY_MIGRATION_MAX is now 241, and its comment says to raise it in the same merge if another NNN_ file lands before this PR does. The tests no longer hard-code the free slot: the timestamp compatibility test takes the first old number above every real file, and the numbering tests derive the top and the number above it from LEGACY_MIGRATION_MAX. The pinned value test now pins 241. The database guide and the changelog fragment say 001-241, and the conversion recipe lists 237 to 241 as slots that always need a compatibility entry.
This commit is contained in:
@@ -1 +1 @@
|
||||
- **feat(db):** New migrations are named `YYYYMMDDHHMMSS_description.sql` (UTC, `npm run db:new-migration -- <description>`) instead of the next number, so parallel PRs no longer collide. The 236 numbered migrations stay frozen. The runner orders by the numeric value of the prefix. Against the base, the migration gate refuses deleted merged migrations and unsafe renames, and runs a commutation check on out-of-order and edited migrations. Release-Green applies every migration to an empty database and compares with the latest release tag. The order of `RENAMED_MIGRATION_COMPATIBILITY` entries no longer matters: a database whose old PR rows block each other's slots boots instead of stopping with `Cannot reconcile` ([#20169](https://github.com/cheaper-inference/OmniRoute/pull/20169))
|
||||
- **feat(db):** New migrations are named `YYYYMMDDHHMMSS_description.sql` (UTC, `npm run db:new-migration -- <description>`) instead of the next number, so parallel PRs no longer collide. The numbered migrations (`001`–`241`) stay frozen. The runner orders by the numeric value of the prefix. Against the base, the migration gate refuses deleted merged migrations and unsafe renames, and runs a commutation check on out-of-order and edited migrations. Release-Green applies every migration to an empty database and compares with the latest release tag. The order of `RENAMED_MIGRATION_COMPATIBILITY` entries no longer matters: a database whose old PR rows block each other's slots boots instead of stopping with `Cannot reconcile` ([#20169](https://github.com/cheaper-inference/OmniRoute/pull/20169))
|
||||
|
||||
@@ -192,10 +192,10 @@ OmniRoute uses **versioned, idempotent migrations** in `src/lib/db/migrations/`.
|
||||
|
||||
### Migration Naming
|
||||
|
||||
| Format | Example | Used for |
|
||||
| ------------------------------------------------- | ---------------------------------------- | --------------------------------------------------------------------------------------- |
|
||||
| `NNN_description.sql` (exactly 3 digits) | `239_call_logs_pending_detail_index.sql` | The old series `001`–`239`. Frozen on 2026-11-13: no new file gets a sequential number. |
|
||||
| `YYYYMMDDHHMMSS_description.sql` (14 digits, UTC) | `20261113120000_add_usage_index.sql` | Every migration created from 2026-11-13 on. |
|
||||
| Format | Example | Used for |
|
||||
| ------------------------------------------------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `NNN_description.sql` (exactly 3 digits) | `241_seen_models.sql` | The old series `001`–`241`. Frozen when the timestamp format landed (`240` and `241` were merged just before it): no new file gets a sequential number. |
|
||||
| `YYYYMMDDHHMMSS_description.sql` (14 digits, UTC) | `20261113120000_add_usage_index.sql` | Every migration created from 2026-11-13 on. |
|
||||
|
||||
Why the change: with sequential numbers, parallel PRs picked the same next number and all but one had to renumber before merging. A UTC timestamp gives each PR its own version.
|
||||
|
||||
@@ -204,10 +204,10 @@ Rules for the timestamp format (enforced by `npm run check:migration-numbering`,
|
||||
- The timestamp is a real UTC date and time, not earlier than `20261113000000` and not more than 10 minutes in the future.
|
||||
- The description is lowercase `snake_case` starting with a letter (`a-z`, `0-9`, single `_` between words), at most 80 characters.
|
||||
- Two files with the same timestamp are a duplicate.
|
||||
- The no-gap rule applies only to the old series: every number from `001` to `239` must exist, except the known gaps `026`, `055` and `121`. Deleting `239` (or any old file) fails the gate. `001` and `239` can never be added to the `KNOWN_GAPS` allowlist. Timestamped files never create a gap.
|
||||
- The no-gap rule applies only to the old series: every number from `001` to `241` must exist, except the known gaps `026`, `055` and `121`. Deleting `241` (or any old file) fails the gate. `001` and `241` can never be added to the `KNOWN_GAPS` allowlist. Timestamped files never create a gap.
|
||||
- Every entry in `src/lib/db/migrations/` must be a regular file with a valid lowercase `.sql` name. A directory, a symlink, a `.SQL` or `.sql.bak` file, a hidden file or a name with a space fails the gate, because the runner would skip it silently or crash at boot.
|
||||
|
||||
**Apply order.** The runner sorts by the numeric value of the prefix, not by text: the old `001`–`239` files first, then the timestamped files in timestamp order, then any namespaced directories registered with `OMNIROUTE_EXTRA_MIGRATIONS_DIRS` (versions such as `ee-001`). A pending migration is any file whose version is not in `_omniroute_migrations`, so a migration from an older branch that merges after a newer one still applies on existing databases.
|
||||
**Apply order.** The runner sorts by the numeric value of the prefix, not by text: the old `001`–`241` files first, then the timestamped files in timestamp order, then any namespaced directories registered with `OMNIROUTE_EXTRA_MIGRATIONS_DIRS` (versions such as `ee-001`). A pending migration is any file whose version is not in `_omniroute_migrations`, so a migration from an older branch that merges after a newer one still applies on existing databases.
|
||||
|
||||
**The timestamp is creation order, not merge order.** If your migration depends on a table or column created by a migration with a **newer** timestamp (for example, one from another PR that merged first), a fresh database would run yours first and fail. While your PR is still open, regenerate the file name with `npm run db:new-migration` so it gets a newer timestamp, or make it independent. `npm run test:migrations:fresh` applies every migration to an empty database and catches this.
|
||||
|
||||
@@ -250,10 +250,10 @@ To run the same comparison locally, pass the base explicitly, for example `npm r
|
||||
|
||||
### Converting an Open PR's `NNN` Migration
|
||||
|
||||
A pull request opened before the switch may still carry a sequential file such as `240_add_widgets.sql`. Old numbers above `239` are rejected, so convert it before merging:
|
||||
A pull request opened before the switch may still carry a sequential file such as `242_add_widgets.sql`. Old numbers above `241` are rejected, so convert it before merging:
|
||||
|
||||
1. **Rename the file with the generator, keeping the description.** Run `npm run db:new-migration -- <same description>` to get the new name, move the SQL into it, and delete the old `NNN_` file. The description must stay identical: the compatibility entry below matches on it.
|
||||
2. **Add a compatibility entry if a persistent database ran the old number.** If any database that is kept (homolog, a VPS, a contributor's own install) ran a build of the PR with the `NNN_` file, add `{ fromVersion: "<NNN>", fromName: "<description>", toVersion: "<YYYYMMDDHHMMSS>", toName: "<description>" }` to `RENAMED_MIGRATION_COMPATIBILITY` in `src/lib/db/migrationRunner/constants.ts`. This is **required for `237`, `238` and `239`**: those slots now hold the canonical `237_usage_history_calculated_cost.sql`, `238_codex_connection_fences.sql` and `239_call_logs_pending_detail_index.sql`, and without the entry such a database skips the canonical migration forever and runs the converted one again. It also works for a slot that stays empty, such as `240`, when the destination is a timestamp. When in doubt, add the entry. Where you add it in the array does not matter: when an entry's target slot still holds a row that another entry moves away (for example `237` holding a PR row while an older `usage_history_calculated_cost` row waits to move into `237`), the runner lets that entry wait until the slot is free. Only a real conflict, such as a slot held by a row no entry moves, or two entries that swap slots, stops the boot with `Cannot reconcile ...`.
|
||||
2. **Add a compatibility entry if a persistent database ran the old number.** If any database that is kept (homolog, a VPS, a contributor's own install) ran a build of the PR with the `NNN_` file, add `{ fromVersion: "<NNN>", fromName: "<description>", toVersion: "<YYYYMMDDHHMMSS>", toName: "<description>" }` to `RENAMED_MIGRATION_COMPATIBILITY` in `src/lib/db/migrationRunner/constants.ts`. This is **required for `237` to `241`**: those slots now hold the canonical `237_usage_history_calculated_cost.sql`, `238_codex_connection_fences.sql`, `239_call_logs_pending_detail_index.sql`, `240_provider_node_custom_json_config.sql` and `241_seen_models.sql`, and without the entry such a database skips the canonical migration forever and runs the converted one again. It also works for a slot that stays empty, such as `242`, when the destination is a timestamp. When in doubt, add the entry. Where you add it in the array does not matter: when an entry's target slot still holds a row that another entry moves away (for example `237` holding a PR row while an older `usage_history_calculated_cost` row waits to move into `237`), the runner lets that entry wait until the slot is free. Only a real conflict, such as a slot held by a row no entry moves, or two entries that swap slots, stops the boot with `Cannot reconcile ...`.
|
||||
3. **Keep the SQL idempotent** (`CREATE TABLE IF NOT EXISTS`, `CREATE INDEX IF NOT EXISTS`, `INSERT OR IGNORE`), because the converted migration may meet a database where its schema already exists.
|
||||
4. **Move version-keyed special cases.** If the PR added a case for its old number in `isSchemaAlreadyApplied()` (`src/lib/db/migrationRunner.ts`) or in `OPTIONAL_FTS5_MIGRATION_VERSIONS` (`src/lib/db/migrationRunner/constants.ts`), change the key to the new timestamp.
|
||||
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
// (094/095 em #3365/#3371). Este gate cria uma ligação de CI entre o disco e as
|
||||
// anomalias já reconhecidas em migrationRunner.ts, falhando em QUALQUER:
|
||||
// - nome de arquivo fora dos dois formatos aceitos — antigo `NNN_*.sql` (exatamente
|
||||
// 3 dígitos, 001 até LEGACY_MIGRATION_MAX=239, congelado em 2026-11-13) ou novo
|
||||
// 3 dígitos, 001 até LEGACY_MIGRATION_MAX=241, congelado na mudança de formato) ou novo
|
||||
// `YYYYMMDDHHMMSS_descricao.sql` (carimbo UTC válido, ≥ corte, não futuro, descrição
|
||||
// snake_case minúscula) — com o motivo e a dica `npm run db:new-migration`;
|
||||
// - prefixo de versão DUPLICADO no disco (exceto duplicatas antigas já reconhecidas;
|
||||
// dois arquivos com o mesmo carimbo são sempre duplicata);
|
||||
// - NOVO gap inexplicado na faixa ANTIGA (gaps conhecidos 026/055/121 são congelados),
|
||||
// inclusive no topo: sumir o 239 (ou qualquer número até ele) também falha — e o topo
|
||||
// inclusive no topo: sumir o 241 (ou qualquer número até ele) também falha — e o topo
|
||||
// ou a base truncados NUNCA são liberados por KNOWN_GAPS;
|
||||
// - entrada do diretório que não seja arquivo regular com nome exatamente válido
|
||||
// (diretório, symlink, `.SQL`, `x.sql `, `.sql.bak`, oculto): o runner a ignoraria ou
|
||||
@@ -106,7 +106,7 @@ function pad3(n) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Função pura — entradas de KNOWN_GAPS proibidas: a primeira (001) e a última (239)
|
||||
* Função pura — entradas de KNOWN_GAPS proibidas: a primeira (001) e a última (241)
|
||||
* migration antiga. Liberá-las esconderia exatamente o incidente que o gate existe para
|
||||
* pegar (migration apagada por engano no topo ou na base da série).
|
||||
*
|
||||
@@ -161,7 +161,7 @@ export function classifyDirectoryEntries(entries) {
|
||||
*
|
||||
* Classificação de cada `.sql` (regras em scripts/check/lib/migrationNaming.mjs):
|
||||
* - antigo (3 dígitos, ≤ legacyMax): duplicata (salvo knownDuplicates) e buraco (salvo
|
||||
* knownGaps) valem SÓ nessa faixa — inclusive o topo: se `legacyMax` (239) ou qualquer
|
||||
* knownGaps) valem SÓ nessa faixa — inclusive o topo: se `legacyMax` (241) ou qualquer
|
||||
* número entre o maior antigo presente e `legacyMax` sumir, vai para `missingLegacyTop`;
|
||||
* e a base: sumir o `legacyMin` (001) ou qualquer número abaixo do menor presente vai
|
||||
* para `missingLegacyBottom`;
|
||||
@@ -170,7 +170,7 @@ export function classifyDirectoryEntries(entries) {
|
||||
* - qualquer outra coisa — inclusive nome que não termina em `.sql` minúsculo, oculto ou
|
||||
* com espaço: nome inválido (`badNames` + motivo e `code` em `invalidNames`).
|
||||
* O topo/base truncados (`missingLegacyTop`/`missingLegacyBottom`) IGNORAM `knownGaps`:
|
||||
* apagar o 001 ou o 239 nunca é liberável.
|
||||
* apagar o 001 ou o 241 nunca é liberável.
|
||||
*
|
||||
* @param {string[]} filenames nomes de arquivo (basename) em src/lib/db/migrations/
|
||||
* @param {Set<string>} knownDuplicates versões antigas com duplicata reconhecida (ex.: "041")
|
||||
|
||||
@@ -9,18 +9,21 @@
|
||||
// CRIAÇÃO, não de merge) + descrição snake_case minúscula. Cada PR tem um
|
||||
// identificador próprio, então PRs paralelos deixam de disputar o mesmo número.
|
||||
//
|
||||
// O runner ordena pelo valor numérico do prefixo: as antigas (≤ 239) vêm sempre antes
|
||||
// O runner ordena pelo valor numérico do prefixo: as antigas (≤ 241) vêm sempre antes
|
||||
// das por data (≥ 20261113000000), e os diretórios extras com namespace (`ee-NNN`)
|
||||
// continuam por último.
|
||||
|
||||
/**
|
||||
* Número antigo mais alto, CONGELADO no dia da mudança de formato (2026-11-13,
|
||||
* release/v3.8.54 tip 2f2c1957: última migration `239_call_logs_pending_detail_index.sql`).
|
||||
* Daqui em diante toda migration nova usa o carimbo de data e hora. Um arquivo antigo
|
||||
* acima deste valor é recusado pelo gate, e um buraco entre o menor número antigo e este
|
||||
* valor (fora de KNOWN_GAPS) também — inclusive o próprio 239 sumindo.
|
||||
* Número antigo mais alto, CONGELADO na mudança de formato. A PR da mudança foi aberta com
|
||||
* a release/v3.8.54 em 239; antes de ela entrar, o merge train 2 mesclou
|
||||
* `240_provider_node_custom_json_config.sql` (#18353) e `241_seen_models.sql` (#18359).
|
||||
* Migration mesclada nunca é renomeada, então o congelamento é no maior número que já está
|
||||
* na base: se outra `NNN_` entrar antes desta PR, suba este valor no mesmo merge. Daqui em
|
||||
* diante toda migration nova usa o carimbo de data e hora. Um arquivo antigo acima deste
|
||||
* valor é recusado pelo gate, e um buraco entre o menor número antigo e este valor (fora de
|
||||
* KNOWN_GAPS) também — inclusive o próprio topo sumindo.
|
||||
*/
|
||||
export const LEGACY_MIGRATION_MAX = 239;
|
||||
export const LEGACY_MIGRATION_MAX = 241;
|
||||
|
||||
/**
|
||||
* Número antigo mais baixo (`001_initial_schema.sql`). Fixo pelo mesmo motivo do topo:
|
||||
|
||||
@@ -42,7 +42,7 @@ const MAIN_VERSION_PATTERN = /^\d+$/;
|
||||
const NAMESPACED_VERSION_PATTERN = /^([a-z][a-z0-9]*)-(\d+)$/;
|
||||
const TIMESTAMP_VERSION_PATTERN = /^\d{14}$/;
|
||||
|
||||
/** `YYYYMMDDHHMMSS` (exactly 14 digits): the version of a timestamped (post-239) migration. */
|
||||
/** `YYYYMMDDHHMMSS` (exactly 14 digits): the version of a timestamped (post-241) migration. */
|
||||
export function isTimestampVersion(version: string): boolean {
|
||||
return TIMESTAMP_VERSION_PATTERN.test(version);
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ type MigrationFile = { version: string; name: string; path: string };
|
||||
*
|
||||
* - Every entry: the old slot is now occupied by ANOTHER file (the original rule).
|
||||
* - Only when `toVersion` is a 14-digit timestamp: no file on disk is
|
||||
* `(fromVersion, fromName)` any more. The legacy set is frozen at 239, so a pull request
|
||||
* that ran as `240_x` and was merged as `<stamp>_x` leaves slot 240 empty for good; the
|
||||
* `(fromVersion, fromName)` any more. The legacy set is frozen at 241, so a pull request
|
||||
* that ran as `242_x` and was merged as `<stamp>_x` leaves slot 242 empty for good; the
|
||||
* original rule could never fire for it and the migration would run a second time.
|
||||
*
|
||||
* Entries with a three-digit destination keep exactly the original rule.
|
||||
|
||||
@@ -220,9 +220,14 @@ const MIGRATIONS_DIR = path.resolve(
|
||||
);
|
||||
const realFilenames = () => fs.readdirSync(MIGRATIONS_DIR).filter((f) => f.endsWith(".sql"));
|
||||
const realTimestampedCount = () => realFilenames().filter((f) => /^\d{14}_/.test(f)).length;
|
||||
// The frozen top of the old series and the first number above it.
|
||||
const LEGACY_TOP = String(LEGACY_MIGRATION_MAX).padStart(3, "0");
|
||||
const ABOVE_TOP = String(LEGACY_MIGRATION_MAX + 1).padStart(3, "0");
|
||||
|
||||
test("LEGACY_MIGRATION_MAX is frozen at 239 and equals the highest old number on disk", () => {
|
||||
assert.equal(LEGACY_MIGRATION_MAX, 239);
|
||||
test("LEGACY_MIGRATION_MAX is frozen at 241 and equals the highest old number on disk", () => {
|
||||
// 239 when the format changed; 240 and 241 were merged before the change landed, and a merged
|
||||
// migration is never renamed.
|
||||
assert.equal(LEGACY_MIGRATION_MAX, 241);
|
||||
const legacy = realFilenames()
|
||||
.map((f) => f.match(/^(\d{3})_/))
|
||||
.filter((m): m is RegExpMatchArray => m !== null)
|
||||
@@ -231,7 +236,13 @@ test("LEGACY_MIGRATION_MAX is frozen at 239 and equals the highest old number on
|
||||
});
|
||||
|
||||
test("date-and-time migration names are accepted and counted separately", () => {
|
||||
const files = ["238_a.sql", "239_b.sql", "20261113120000_add_x.sql", "20261114093015_y2.sql"];
|
||||
const belowTop = String(LEGACY_MIGRATION_MAX - 1).padStart(3, "0");
|
||||
const files = [
|
||||
`${belowTop}_a.sql`,
|
||||
`${LEGACY_TOP}_b.sql`,
|
||||
"20261113120000_add_x.sql",
|
||||
"20261114093015_y2.sql",
|
||||
];
|
||||
const r = find(files, EMPTY, EMPTY, { now: NOW });
|
||||
assert.deepEqual(r.badNames, []);
|
||||
assert.deepEqual(r.duplicates, []);
|
||||
@@ -354,8 +365,8 @@ test("an old-range gap is still detected next to date-named migrations", () => {
|
||||
assert.deepEqual(r.gaps, ["002"]);
|
||||
});
|
||||
|
||||
test("removing the top old migration (239) is detected", () => {
|
||||
const withoutTop = realFilenames().filter((f) => !f.startsWith("239_"));
|
||||
test("removing the top old migration (LEGACY_MIGRATION_MAX) is detected", () => {
|
||||
const withoutTop = realFilenames().filter((f) => !f.startsWith(`${LEGACY_TOP}_`));
|
||||
assert.equal(withoutTop.length, realFilenames().length - 1);
|
||||
const r = find(
|
||||
[...withoutTop, "20261113120000_x.sql"],
|
||||
@@ -364,7 +375,7 @@ test("removing the top old migration (239) is detected", () => {
|
||||
{ now: NOW }
|
||||
);
|
||||
assert.deepEqual(r.gaps, []);
|
||||
assert.deepEqual(r.missingLegacyTop, ["239"]);
|
||||
assert.deepEqual(r.missingLegacyTop, [LEGACY_TOP]);
|
||||
});
|
||||
|
||||
test("removing the last three old migrations reports each missing number", () => {
|
||||
@@ -426,7 +437,7 @@ test("the gate script exits 0 on the real tree and prints both counts", async ()
|
||||
env: gateEnv(),
|
||||
});
|
||||
assert.equal(run.status, 0, run.stderr);
|
||||
assert.match(run.stdout, /antiga\(s\) 001–239, \d+ por data/);
|
||||
assert.match(run.stdout, new RegExp(`antiga\\(s\\) 001–${LEGACY_TOP}, \\d+ por data`));
|
||||
});
|
||||
|
||||
// --- End-to-end: the gate's main() against a scratch copy of the migrations dir ---------
|
||||
@@ -469,18 +480,21 @@ test("gate main() end-to-end: accepts a new timestamp file, rejects bad names an
|
||||
assert.equal(r.status, 0, r.stderr);
|
||||
assert.match(r.stdout, new RegExp(`, ${realTimestampedCount() + 1} por data;`));
|
||||
|
||||
fs.writeFileSync(path.join(dir, "240_too_late.sql"), "-- bad\n");
|
||||
fs.writeFileSync(path.join(dir, `${ABOVE_TOP}_too_late.sql`), "-- bad\n");
|
||||
r = gate();
|
||||
assert.equal(r.status, 1);
|
||||
assert.match(r.stderr, /240_too_late\.sql — número antigo 240 depois do corte/);
|
||||
assert.match(
|
||||
r.stderr,
|
||||
new RegExp(`${ABOVE_TOP}_too_late\\.sql — número antigo ${ABOVE_TOP} depois do corte`)
|
||||
);
|
||||
assert.match(r.stderr, /npm run db:new-migration -- <descricao>/);
|
||||
fs.rmSync(path.join(dir, "240_too_late.sql"));
|
||||
fs.rmSync(path.join(dir, `${ABOVE_TOP}_too_late.sql`));
|
||||
|
||||
const top = realFilenames().find((f) => f.startsWith("239_"))!;
|
||||
const top = realFilenames().find((f) => f.startsWith(`${LEGACY_TOP}_`))!;
|
||||
fs.rmSync(path.join(dir, top));
|
||||
r = gate();
|
||||
assert.equal(r.status, 1);
|
||||
assert.match(r.stderr, /faixa antiga truncada: faltando 239/);
|
||||
assert.match(r.stderr, new RegExp(`faixa antiga truncada: faltando ${LEGACY_TOP}`));
|
||||
} finally {
|
||||
fs.rmSync(tmp, { recursive: true, force: true });
|
||||
}
|
||||
@@ -584,7 +598,10 @@ test("KNOWN_GAPS may never contain the first or the last old number", async () =
|
||||
findForbiddenKnownGaps?: (gaps: Set<string>) => string[];
|
||||
};
|
||||
assert.equal(typeof mod.findForbiddenKnownGaps, "function");
|
||||
assert.deepEqual(mod.findForbiddenKnownGaps!(new Set(["026", "001", "239"])), ["001", "239"]);
|
||||
assert.deepEqual(mod.findForbiddenKnownGaps!(new Set(["026", "001", LEGACY_TOP])), [
|
||||
"001",
|
||||
LEGACY_TOP,
|
||||
]);
|
||||
assert.deepEqual(mod.findForbiddenKnownGaps!(KNOWN_GAPS as Set<string>), []);
|
||||
});
|
||||
|
||||
@@ -603,11 +620,14 @@ test("gate hints: truncated range says restore (never allowlist); future stamp s
|
||||
const gate = () =>
|
||||
spawnSync(process.execPath, [script], { cwd: tmp, encoding: "utf-8", env: gateEnv() });
|
||||
|
||||
const top = realFilenames().find((f) => f.startsWith("239_"))!;
|
||||
const top = realFilenames().find((f) => f.startsWith(`${LEGACY_TOP}_`))!;
|
||||
fs.rmSync(path.join(dir, top));
|
||||
let r = gate();
|
||||
assert.equal(r.status, 1);
|
||||
assert.match(r.stderr, /git checkout <base> -- src\/lib\/db\/migrations\/239_\*/);
|
||||
assert.match(
|
||||
r.stderr,
|
||||
new RegExp(`git checkout <base> -- src/lib/db/migrations/${LEGACY_TOP}_\\*`)
|
||||
);
|
||||
assert.match(r.stderr, /nunca libere na allowlist/);
|
||||
assert.doesNotMatch(r.stderr, /adicione o número às allowlists/);
|
||||
fs.writeFileSync(path.join(dir, top), "-- copy\n");
|
||||
|
||||
@@ -4,21 +4,22 @@
|
||||
/**
|
||||
* RENAMED_MIGRATION_COMPATIBILITY with a TIMESTAMPED destination (`NNN_x` → `YYYYMMDDHHMMSS_x`).
|
||||
*
|
||||
* The legacy set is frozen at 239, so an open pull request that ran with `240_x` (or any
|
||||
* slot above 239) gets its file renamed to a timestamp before merging, and slot 240 is never
|
||||
* reoccupied. A database that already ran that PR build carries a `240 x` ledger row. Until
|
||||
* now a compatibility entry only moved the row when ANOTHER file sat in the old slot, so an
|
||||
* entry `240 x → <stamp> x` did nothing and the migration ran a second time (non-idempotent
|
||||
* The legacy set is frozen (LEGACY_MIGRATION_MAX), so an open pull request that ran with an
|
||||
* old number above it (`PR_SLOT_x` below) gets its file renamed to a timestamp before merging,
|
||||
* and that slot is never reoccupied. A database that already ran that PR build carries a
|
||||
* `PR_SLOT x` ledger row. Until now a compatibility entry only moved the row when ANOTHER file
|
||||
* sat in the old slot, so an entry `PR_SLOT x → <stamp> x` did nothing and the migration ran a
|
||||
* second time (non-idempotent
|
||||
* SQL stopped the boot). The rule is now: eligible as before, OR — only when `toVersion` is a
|
||||
* 14-digit stamp — when no file on disk is `(fromVersion, fromName)` any more. Entries with a
|
||||
* three-digit destination keep the old rule.
|
||||
*
|
||||
* Covered, with real SQL on top of a real clean install:
|
||||
* (a) 240_x in the ledger, file now <stamp>_x, entry present → the row moves, nothing is
|
||||
* (a) PR_SLOT_x in the ledger, file now <stamp>_x, entry present → the row moves, nothing is
|
||||
* applied, the non-idempotent SQL is not executed again;
|
||||
* (b) slot 238 reoccupied by the canonical 238_codex_connection_fences: the canonical
|
||||
* applies, the converted PR migration does not run again;
|
||||
* (c) no entry → today's behaviour: the migration runs again, the 240 row stays orphaned;
|
||||
* (c) no entry → today's behaviour: the migration runs again, the PR_SLOT row stays orphaned;
|
||||
* (d) entry with a three-digit destination and the old slot vacant → not reconciled.
|
||||
*
|
||||
* INJECTION TECHNIQUE: the entries are pushed at runtime into the exported
|
||||
@@ -47,6 +48,19 @@ const REAL_MIGRATIONS_DIR =
|
||||
process.env.OMNIROUTE_TEST_MIGRATIONS_SOURCE_DIR ||
|
||||
path.join(REPO_ROOT, "src", "lib", "db", "migrations");
|
||||
|
||||
// The slot an open PR used before converting to a timestamp: the first old-format number above
|
||||
// every real one, so it never collides with a real file (240 and 241 were merged before this
|
||||
// rule landed; a hard-coded slot broke as soon as they did).
|
||||
const REAL_LEGACY_MAX = Math.max(
|
||||
...fs
|
||||
.readdirSync(REAL_MIGRATIONS_DIR)
|
||||
.map((name) => /^(\d{3})_.+\.sql$/.exec(name))
|
||||
.filter((match): match is RegExpExecArray => match !== null)
|
||||
.map((match) => Number(match[1]))
|
||||
);
|
||||
const PR_SLOT = String(REAL_LEGACY_MAX + 1).padStart(3, "0");
|
||||
const NEXT_SLOT = String(REAL_LEGACY_MAX + 2).padStart(3, "0");
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
function mkTempDir(prefix: string): string {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
||||
@@ -219,21 +233,21 @@ function migrationFiles() {
|
||||
|
||||
// ── scenarios ───────────────────────────────────────────────────────────────────
|
||||
|
||||
test("(a) 240_x converted to a timestamp with an entry → row moved, nothing re-applied", () => {
|
||||
test("(a) an old-numbered PR file converted to a timestamp with an entry → row moved, nothing re-applied", () => {
|
||||
const db = applyRealBaseline();
|
||||
try {
|
||||
// The database ran the PR build that still numbered the migration 240.
|
||||
addCoreMigration("240_compat_probe.sql", nonIdempotentSql("compat_probe"));
|
||||
// The database ran the PR build that still used the old number PR_SLOT.
|
||||
addCoreMigration(`${PR_SLOT}_compat_probe.sql`, nonIdempotentSql("compat_probe"));
|
||||
assert.equal(runner.runMigrations(db), 1);
|
||||
assert.deepEqual(ledgerRows(db, "240"), [{ name: "compat_probe" }]);
|
||||
assert.deepEqual(ledgerRows(db, PR_SLOT), [{ name: "compat_probe" }]);
|
||||
|
||||
// The PR is merged with the file renamed to a timestamp; slot 240 stays empty for good.
|
||||
removeCoreMigration("240_compat_probe.sql");
|
||||
// The PR is merged with the file renamed to a timestamp; slot PR_SLOT stays empty for good.
|
||||
removeCoreMigration(`${PR_SLOT}_compat_probe.sql`);
|
||||
addCoreMigration("20990101000000_compat_probe.sql", nonIdempotentSql("compat_probe"));
|
||||
|
||||
withCompatibilityEntry(
|
||||
{
|
||||
fromVersion: "240",
|
||||
fromVersion: PR_SLOT,
|
||||
fromName: "compat_probe",
|
||||
toVersion: "20990101000000",
|
||||
toName: "compat_probe",
|
||||
@@ -242,11 +256,15 @@ test("(a) 240_x converted to a timestamp with an entry → row moved, nothing re
|
||||
assert.equal(
|
||||
schemaState.hasLedgerRepairCandidates(db, migrationFiles()),
|
||||
true,
|
||||
"the 240 row is a repair candidate"
|
||||
`the ${PR_SLOT} row is a repair candidate`
|
||||
);
|
||||
// Re-running the CREATE TABLE would throw; 0 means the SQL never ran again.
|
||||
assert.equal(runner.runMigrations(db), 0, "the converted migration must not re-apply");
|
||||
assert.deepEqual(ledgerRows(db, "240"), [], "the 240 row is moved, not left orphaned");
|
||||
assert.deepEqual(
|
||||
ledgerRows(db, PR_SLOT),
|
||||
[],
|
||||
`the ${PR_SLOT} row is moved, not left orphaned`
|
||||
);
|
||||
assert.deepEqual(ledgerRows(db, "20990101000000"), [{ name: "compat_probe" }]);
|
||||
assert.equal(tableRows(db, "compat_probe"), 1);
|
||||
assert.equal(schemaState.hasLedgerRepairCandidates(db, migrationFiles()), false);
|
||||
@@ -296,14 +314,14 @@ test("(b) slot 238 reoccupied by the canonical migration → canonical applies,
|
||||
test("(c) no entry → today's behaviour: the converted migration runs again", () => {
|
||||
const db = applyRealBaseline();
|
||||
try {
|
||||
addCoreMigration("240_compat_rerun.sql", countingSql("compat_rerun"));
|
||||
addCoreMigration(`${PR_SLOT}_compat_rerun.sql`, countingSql("compat_rerun"));
|
||||
assert.equal(runner.runMigrations(db), 1);
|
||||
removeCoreMigration("240_compat_rerun.sql");
|
||||
removeCoreMigration(`${PR_SLOT}_compat_rerun.sql`);
|
||||
addCoreMigration("20990103000000_compat_rerun.sql", countingSql("compat_rerun"));
|
||||
|
||||
assert.equal(schemaState.hasLedgerRepairCandidates(db, migrationFiles()), false);
|
||||
assert.equal(runner.runMigrations(db), 1, "without an entry the migration applies again");
|
||||
assert.deepEqual(ledgerRows(db, "240"), [{ name: "compat_rerun" }], "orphan row stays");
|
||||
assert.deepEqual(ledgerRows(db, PR_SLOT), [{ name: "compat_rerun" }], "orphan row stays");
|
||||
assert.deepEqual(ledgerRows(db, "20990103000000"), [{ name: "compat_rerun" }]);
|
||||
assert.equal(tableRows(db, "compat_rerun"), 2, "the SQL ran a second time");
|
||||
} finally {
|
||||
@@ -314,23 +332,27 @@ test("(c) no entry → today's behaviour: the converted migration runs again", (
|
||||
test("(d) three-digit destination with the old slot vacant → old rule, not reconciled", () => {
|
||||
const db = applyRealBaseline();
|
||||
try {
|
||||
addCoreMigration("240_compat_legacy.sql", countingSql("compat_legacy"));
|
||||
addCoreMigration(`${PR_SLOT}_compat_legacy.sql`, countingSql("compat_legacy"));
|
||||
assert.equal(runner.runMigrations(db), 1);
|
||||
removeCoreMigration("240_compat_legacy.sql");
|
||||
addCoreMigration("241_compat_legacy.sql", countingSql("compat_legacy"));
|
||||
removeCoreMigration(`${PR_SLOT}_compat_legacy.sql`);
|
||||
addCoreMigration(`${NEXT_SLOT}_compat_legacy.sql`, countingSql("compat_legacy"));
|
||||
|
||||
withCompatibilityEntry(
|
||||
{
|
||||
fromVersion: "240",
|
||||
fromVersion: PR_SLOT,
|
||||
fromName: "compat_legacy",
|
||||
toVersion: "241",
|
||||
toVersion: NEXT_SLOT,
|
||||
toName: "compat_legacy",
|
||||
},
|
||||
() => {
|
||||
assert.equal(schemaState.hasLedgerRepairCandidates(db, migrationFiles()), false);
|
||||
assert.equal(runner.runMigrations(db), 1, "241 applies: the entry is not eligible");
|
||||
assert.deepEqual(ledgerRows(db, "240"), [{ name: "compat_legacy" }]);
|
||||
assert.deepEqual(ledgerRows(db, "241"), [{ name: "compat_legacy" }]);
|
||||
assert.equal(
|
||||
runner.runMigrations(db),
|
||||
1,
|
||||
`${NEXT_SLOT} applies: the entry is not eligible`
|
||||
);
|
||||
assert.deepEqual(ledgerRows(db, PR_SLOT), [{ name: "compat_legacy" }]);
|
||||
assert.deepEqual(ledgerRows(db, NEXT_SLOT), [{ name: "compat_legacy" }]);
|
||||
assert.equal(tableRows(db, "compat_legacy"), 2);
|
||||
}
|
||||
);
|
||||
@@ -342,15 +364,15 @@ test("(d) three-digit destination with the old slot vacant → old rule, not rec
|
||||
test("(e) a timestamped destination is NOT eligible while the old file is still on disk", () => {
|
||||
const db = applyRealBaseline();
|
||||
try {
|
||||
// Both files present (e.g. the rename was not finished): the 240 row belongs to a file
|
||||
// Both files present (e.g. the rename was not finished): the PR_SLOT row belongs to a file
|
||||
// that still exists, so it must not be moved away from it.
|
||||
addCoreMigration("240_compat_both.sql", countingSql("compat_both"));
|
||||
addCoreMigration(`${PR_SLOT}_compat_both.sql`, countingSql("compat_both"));
|
||||
assert.equal(runner.runMigrations(db), 1);
|
||||
addCoreMigration("20990104000000_compat_both.sql", countingSql("compat_both"));
|
||||
|
||||
withCompatibilityEntry(
|
||||
{
|
||||
fromVersion: "240",
|
||||
fromVersion: PR_SLOT,
|
||||
fromName: "compat_both",
|
||||
toVersion: "20990104000000",
|
||||
toName: "compat_both",
|
||||
@@ -358,7 +380,7 @@ test("(e) a timestamped destination is NOT eligible while the old file is still
|
||||
() => {
|
||||
assert.equal(schemaState.hasLedgerRepairCandidates(db, migrationFiles()), false);
|
||||
assert.equal(runner.runMigrations(db), 1);
|
||||
assert.deepEqual(ledgerRows(db, "240"), [{ name: "compat_both" }]);
|
||||
assert.deepEqual(ledgerRows(db, PR_SLOT), [{ name: "compat_both" }]);
|
||||
assert.deepEqual(ledgerRows(db, "20990104000000"), [{ name: "compat_both" }]);
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user