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
187 lines
9.9 KiB
SQL
187 lines
9.9 KiB
SQL
ALTER TABLE "download_events" ADD COLUMN "intent_kind" text;--> statement-breakpoint
|
|
ALTER TABLE "download_events" ADD COLUMN "request_fingerprint" varchar(64);--> statement-breakpoint
|
|
ALTER TABLE "download_events" ADD COLUMN "legacy_intent" boolean;--> statement-breakpoint
|
|
-- UUID-shaped legacy keys are canonicalized only when doing so is unambiguous.
|
|
-- Arbitrary and case-colliding historical keys remain byte-for-byte unchanged.
|
|
WITH case_colliding_idempotency_keys AS (
|
|
SELECT "editorial_account_id", lower("idempotency_key") AS canonical_key
|
|
FROM "download_events"
|
|
GROUP BY "editorial_account_id", lower("idempotency_key")
|
|
HAVING count(*) > 1
|
|
)
|
|
UPDATE "download_events" AS target
|
|
SET "idempotency_key" = lower(target."idempotency_key")
|
|
WHERE
|
|
target."idempotency_key" ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM case_colliding_idempotency_keys AS collision
|
|
WHERE
|
|
collision."editorial_account_id" = target."editorial_account_id"
|
|
AND collision.canonical_key = lower(target."idempotency_key")
|
|
);--> statement-breakpoint
|
|
-- A legacy new usage is reconstructible only when it has one unique earliest,
|
|
-- note-free event and every runtime fingerprint field already satisfies the new
|
|
-- normalized input contract. A repeat additionally needs a normalized non-null
|
|
-- repeat note after that provable initial event. Every other row is explicit legacy.
|
|
WITH earliest_event_times AS (
|
|
SELECT "usage_id", min("authorized_at") AS earliest_authorized_at
|
|
FROM "download_events"
|
|
GROUP BY "usage_id"
|
|
),
|
|
case_colliding_idempotency_keys AS (
|
|
SELECT "editorial_account_id", lower("idempotency_key") AS canonical_key
|
|
FROM "download_events"
|
|
GROUP BY "editorial_account_id", lower("idempotency_key")
|
|
HAVING count(*) > 1
|
|
),
|
|
legacy_event_candidates AS (
|
|
SELECT
|
|
events."id",
|
|
events."usage_id",
|
|
events."authorized_at",
|
|
earliest.earliest_authorized_at,
|
|
(
|
|
events."repeat_download_note" IS NULL
|
|
AND events."editorial_account_id" = usage."editorial_account_id"
|
|
AND lower(events."immich_asset_id") = lower(usage."immich_asset_id")
|
|
AND events."immich_asset_id" ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
|
|
AND usage."immich_asset_id" ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
|
|
AND usage."immich_album_id_snapshot" ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
|
|
AND events."idempotency_key" ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
|
|
AND octet_length(usage."purpose") = char_length(usage."purpose")
|
|
AND usage."purpose" !~ '[[:cntrl:]]'
|
|
AND left(usage."purpose", 1) <> ' '
|
|
AND right(usage."purpose", 1) <> ' '
|
|
AND char_length(usage."purpose") BETWEEN 3 AND 200
|
|
AND (
|
|
usage."internal_reference" IS NULL
|
|
OR (
|
|
octet_length(usage."internal_reference") = char_length(usage."internal_reference")
|
|
AND usage."internal_reference" !~ '[[:cntrl:]]'
|
|
AND left(usage."internal_reference", 1) <> ' '
|
|
AND right(usage."internal_reference", 1) <> ' '
|
|
AND usage."internal_reference" <> ''
|
|
AND char_length(usage."internal_reference") <= 200
|
|
)
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM case_colliding_idempotency_keys AS collision
|
|
WHERE
|
|
collision."editorial_account_id" = events."editorial_account_id"
|
|
AND collision.canonical_key = lower(events."idempotency_key")
|
|
)
|
|
) AS can_be_initial,
|
|
(
|
|
events."editorial_account_id" = usage."editorial_account_id"
|
|
AND lower(events."immich_asset_id") = lower(usage."immich_asset_id")
|
|
AND events."immich_asset_id" ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
|
|
AND events."idempotency_key" ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
|
|
AND octet_length(events."repeat_download_note") = char_length(events."repeat_download_note")
|
|
AND events."repeat_download_note" !~ '[[:cntrl:]]'
|
|
AND left(events."repeat_download_note", 1) <> ' '
|
|
AND right(events."repeat_download_note", 1) <> ' '
|
|
AND events."repeat_download_note" <> ''
|
|
AND char_length(events."repeat_download_note") <= 200
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM case_colliding_idempotency_keys AS collision
|
|
WHERE
|
|
collision."editorial_account_id" = events."editorial_account_id"
|
|
AND collision.canonical_key = lower(events."idempotency_key")
|
|
)
|
|
) AS can_be_repeat
|
|
FROM "download_events" AS events
|
|
INNER JOIN "usages" AS usage ON usage."id" = events."usage_id"
|
|
INNER JOIN earliest_event_times AS earliest ON earliest."usage_id" = events."usage_id"
|
|
),
|
|
usage_event_summary AS (
|
|
SELECT
|
|
candidate."usage_id",
|
|
candidate.earliest_authorized_at,
|
|
count(*) FILTER (
|
|
WHERE candidate."authorized_at" = candidate.earliest_authorized_at
|
|
) AS earliest_event_count,
|
|
bool_and(
|
|
CASE
|
|
WHEN candidate."authorized_at" = candidate.earliest_authorized_at
|
|
THEN candidate.can_be_initial
|
|
ELSE true
|
|
END
|
|
) AS earliest_event_is_provable
|
|
FROM legacy_event_candidates AS candidate
|
|
GROUP BY candidate."usage_id", candidate.earliest_authorized_at
|
|
)
|
|
UPDATE "download_events" AS events
|
|
SET "intent_kind" = CASE
|
|
WHEN
|
|
candidate."authorized_at" = summary.earliest_authorized_at
|
|
AND summary.earliest_event_count = 1
|
|
AND candidate.can_be_initial
|
|
THEN 'new_usage'
|
|
WHEN
|
|
candidate."authorized_at" > summary.earliest_authorized_at
|
|
AND summary.earliest_event_count = 1
|
|
AND summary.earliest_event_is_provable
|
|
AND candidate.can_be_repeat
|
|
THEN 'repeat_download'
|
|
ELSE 'legacy'
|
|
END
|
|
FROM legacy_event_candidates AS candidate, usage_event_summary AS summary
|
|
WHERE
|
|
candidate."id" = events."id"
|
|
AND summary."usage_id" = candidate."usage_id";--> statement-breakpoint
|
|
-- Runtime-compatible rows receive the exact SHA-256 used by JSON.stringify in the
|
|
-- application. Explicit legacy rows receive a deterministic non-runtime hash.
|
|
UPDATE "download_events" AS events
|
|
SET "request_fingerprint" = CASE
|
|
WHEN events."intent_kind" = 'new_usage' THEN encode(sha256(convert_to(
|
|
'{"kind":"new_usage","editorialId":' || to_json(events."editorial_account_id"::text)::text ||
|
|
',"assetId":' || to_json(lower(events."immich_asset_id"))::text ||
|
|
',"albumId":' || to_json(lower(usage."immich_album_id_snapshot"))::text ||
|
|
',"purpose":' || to_json(usage."purpose")::text ||
|
|
',"reference":' || coalesce(to_json(usage."internal_reference")::text, 'null') ||
|
|
'}',
|
|
'UTF8'
|
|
)), 'hex')
|
|
WHEN events."intent_kind" = 'repeat_download' THEN encode(sha256(convert_to(
|
|
'{"kind":"repeat_download","editorialId":' || to_json(events."editorial_account_id"::text)::text ||
|
|
',"usageId":' || to_json(events."usage_id"::text)::text ||
|
|
',"note":' || to_json(events."repeat_download_note")::text ||
|
|
'}',
|
|
'UTF8'
|
|
)), 'hex')
|
|
ELSE encode(sha256(convert_to('legacy-intent-v1:' || events."id"::text, 'UTF8')), 'hex')
|
|
END
|
|
FROM "usages" AS usage
|
|
WHERE usage."id" = events."usage_id";--> statement-breakpoint
|
|
UPDATE "download_events" SET "legacy_intent" = true;--> statement-breakpoint
|
|
ALTER TABLE "download_events" ALTER COLUMN "intent_kind" SET DEFAULT 'new_usage';--> statement-breakpoint
|
|
ALTER TABLE "download_events" ALTER COLUMN "intent_kind" SET NOT NULL;--> statement-breakpoint
|
|
ALTER TABLE "download_events" ALTER COLUMN "request_fingerprint" SET NOT NULL;--> statement-breakpoint
|
|
ALTER TABLE "download_events" ALTER COLUMN "legacy_intent" SET DEFAULT false;--> statement-breakpoint
|
|
ALTER TABLE "download_events" ALTER COLUMN "legacy_intent" SET NOT NULL;--> statement-breakpoint
|
|
ALTER TABLE "download_events" ADD CONSTRAINT "download_events_intent_kind_check" CHECK ("download_events"."intent_kind" in ('new_usage', 'repeat_download', 'legacy'));--> statement-breakpoint
|
|
ALTER TABLE "download_events" ADD CONSTRAINT "download_events_request_fingerprint_hex_check" CHECK ("download_events"."request_fingerprint" ~ '^[0-9a-f]{64}$');--> statement-breakpoint
|
|
ALTER TABLE "download_events" ADD CONSTRAINT "download_events_canonical_idempotency_key_check" CHECK ("download_events"."legacy_intent" OR "download_events"."idempotency_key" ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$');--> statement-breakpoint
|
|
WITH ranked_active_album_grants AS (
|
|
SELECT
|
|
"id",
|
|
row_number() OVER (
|
|
PARTITION BY "editorial_account_id", lower("immich_album_id")
|
|
ORDER BY ("immich_album_id" = lower("immich_album_id")) DESC, "granted_at", "id"
|
|
) AS survivor_order
|
|
FROM "album_grants"
|
|
WHERE "revoked_at" IS NULL
|
|
)
|
|
UPDATE "album_grants" AS grants
|
|
SET "revoked_at" = now()
|
|
FROM ranked_active_album_grants AS ranked
|
|
WHERE grants."id" = ranked."id" AND ranked.survivor_order > 1;--> statement-breakpoint
|
|
CREATE UNIQUE INDEX "album_grants_editorial_active_album_unique" ON "album_grants" ("editorial_account_id", lower("immich_album_id")) WHERE "revoked_at" IS NULL;--> statement-breakpoint
|
|
ALTER TABLE "album_grants" ADD CONSTRAINT "album_grants_immich_album_id_uuid_check" CHECK ("album_grants"."immich_album_id" ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') NOT VALID;--> statement-breakpoint
|
|
ALTER TABLE "download_events" ADD CONSTRAINT "download_events_canonical_immich_asset_id_check" CHECK ("download_events"."immich_asset_id" ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') NOT VALID;--> statement-breakpoint
|
|
ALTER TABLE "usages" ADD CONSTRAINT "usages_canonical_immich_asset_id_check" CHECK ("usages"."immich_asset_id" ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') NOT VALID;--> statement-breakpoint
|
|
ALTER TABLE "usages" ADD CONSTRAINT "usages_canonical_immich_album_id_check" CHECK ("usages"."immich_album_id_snapshot" ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') NOT VALID;
|