Something went wrong. Try again.
Exosphere is a set of small, modular, self-hostable community tools built on the AT Protocol. app.exosphere.site
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576/** * Ensure `drizzle/meta/_journal.json` has strictly-monotonic `when` values. * * Why: drizzle's sqlite migrator skips any migration whose `when` is ≤ * `max(__drizzle_migrations.created_at)` on the target DB. If a newly-generated * migration's `when` (wall-clock `Date.now()` at generate-time) is smaller than * an earlier entry's — which happens here because a few older entries were * manually bumped into the future — the migrator silently skips the new one. * * This script walks the journal in `idx` order. Any entry whose `when` is not * strictly greater than the running max is rewritten to `max + 1`, so every * subsequent migration is guaranteed to be picked up. * * Runs idempotently — no-op if the journal is already monotonic. * * Usage: `bun run scripts/fix-journal-ordering.ts` (wired into `db:generate`). */
import { readFileSync, writeFileSync } from "node:fs";import { join } from "node:path";
const ROOT = join(import.meta.dir, "..");const JOURNAL_PATH = join(ROOT, "drizzle/meta/_journal.json");
interface JournalEntry { idx: number; version: string; when: number; tag: string; breakpoints: boolean;}
interface Journal { version: string; dialect: string; entries: JournalEntry[];}
const raw = readFileSync(JOURNAL_PATH, "utf-8");const journal: Journal = JSON.parse(raw);
// Sort by idx to be defensive — the file is already idx-ordered but nothing// enforces that.const entries = [...journal.entries].sort((a, b) => a.idx - b.idx);
const bumps: { tag: string; from: number; to: number }[] = [];let maxWhen = Number.NEGATIVE_INFINITY;for (const entry of entries) { if (entry.when > maxWhen) { maxWhen = entry.when; continue; } // Non-monotonic: bump to max + 1 so it's strictly greater than every prior. const next = maxWhen + 1; bumps.push({ tag: entry.tag, from: entry.when, to: next }); entry.when = next; maxWhen = next;}
if (bumps.length === 0) { console.log("[fix-journal-ordering] journal already monotonic — nothing to do"); process.exit(0);}
journal.entries = entries;// Preserve trailing newline — drizzle-kit writes one, and removing it churns diffs.const serialized = `${JSON.stringify(journal, null, 2)}\n`;writeFileSync(JOURNAL_PATH, serialized);
console.log( `[fix-journal-ordering] bumped ${bumps.length} entr${bumps.length === 1 ? "y" : "ies"}:`,);for (const b of bumps) { console.log(` ${b.tag}: ${b.from} → ${b.to}`);}