Squash the 13 historical migrations into one generated baseline whose journal entry reuses the original 0000 timestamp: the migrator skips it on every existing database (prod journal stuck at 0006) and applies it only on fresh ones. Parity with the old chain verified via pg_dump diff plus fresh/existing/prod-simulated drizzle-kit migrate runs. Historical files move to drizzle-history/pre-squash-2026-08/ for integration tests that exercise legacy migration behavior. Also repair db tests that already failed before this change: - raw SQL targeted users, but the table has always been "user" - membership recheck mocked legacy getAsset.albums instead of searchPhotos - audit rollback assertion walks drizzle's wrapped error cause chain - keyset index probe pinned to NULLS LAST ordering, removing a planner cost tie-break that flipped with index creation order
64 lines
2.5 KiB
TypeScript
64 lines
2.5 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
import { expect, test } from "vitest";
|
|
|
|
const migration = await readFile(
|
|
path.join(process.cwd(), "drizzle-history/pre-squash-2026-08/0002_accounting_intents.sql"),
|
|
"utf8",
|
|
);
|
|
|
|
test("0002 retains legacy Immich text and accepts case-variant historical UUIDs", () => {
|
|
expect(migration).not.toMatch(/ALTER COLUMN "immich_[^"]+" SET DATA TYPE uuid/);
|
|
expect(migration).toContain(
|
|
'ADD CONSTRAINT "album_grants_immich_album_id_uuid_check"',
|
|
);
|
|
expect(migration).toContain('CHECK ("album_grants"."immich_album_id" ~*');
|
|
expect(migration).toContain(
|
|
'ADD CONSTRAINT "usages_canonical_immich_asset_id_check"',
|
|
);
|
|
expect(migration).toContain(
|
|
'ADD CONSTRAINT "usages_canonical_immich_album_id_check"',
|
|
);
|
|
expect(migration).toContain(
|
|
'ADD CONSTRAINT "download_events_canonical_immich_asset_id_check"',
|
|
);
|
|
expect(migration.match(/NOT VALID/g)).toHaveLength(4);
|
|
});
|
|
|
|
test("0002 consolidates active case duplicates before adding their partial unique index", () => {
|
|
const consolidation = migration.indexOf('ranked_active_album_grants AS');
|
|
const activeIndex = migration.indexOf(
|
|
'CREATE UNIQUE INDEX "album_grants_editorial_active_album_unique"',
|
|
);
|
|
|
|
expect(consolidation).toBeGreaterThan(-1);
|
|
expect(migration).toContain(
|
|
'PARTITION BY "editorial_account_id", lower("immich_album_id")',
|
|
);
|
|
expect(migration).toContain(
|
|
'ORDER BY ("immich_album_id" = lower("immich_album_id")) DESC, "granted_at", "id"',
|
|
);
|
|
expect(activeIndex).toBeGreaterThan(consolidation);
|
|
expect(migration).toContain(
|
|
'ON "album_grants" ("editorial_account_id", lower("immich_album_id")) WHERE "revoked_at" IS NULL',
|
|
);
|
|
});
|
|
|
|
test("0002 uses an explicit legacy kind instead of random UUID ordering for ambiguity", () => {
|
|
expect(migration).not.toContain('ORDER BY "authorized_at", "id"');
|
|
expect(migration).toContain("ELSE 'legacy'");
|
|
expect(migration).toContain("in ('new_usage', 'repeat_download', 'legacy')");
|
|
expect(migration).toContain("case_colliding_idempotency_keys");
|
|
});
|
|
|
|
test("0002 reconstructs only already-normalized printable ASCII text", () => {
|
|
expect(migration).not.toContain("btrim(");
|
|
expect(migration).toContain(
|
|
'octet_length(usage."purpose") = char_length(usage."purpose")',
|
|
);
|
|
expect(migration).toContain("!~ '[[:cntrl:]]'");
|
|
expect(migration).toContain("left(usage.\"purpose\", 1) <> ' '");
|
|
expect(migration).toContain("right(events.\"repeat_download_note\", 1) <> ' '");
|
|
});
|