fix: harden usage accounting idempotency

This commit is contained in:
Leon Tonhäuser committed 2026-08-10 10:59:12 +02:00
1 parent 22fe6bf659
commit 3de1530074
14 files changed
+708 -107

No files matched your search

+79 -4
View File
@@ -1,7 +1,82 @@
ALTER TABLE "download_events" ADD COLUMN "intent_kind" text DEFAULT 'new_usage' NOT NULL;--> statement-breakpoint
ALTER TABLE "album_grants" ALTER COLUMN "immich_album_id" SET DATA TYPE uuid USING lower("immich_album_id")::uuid;--> statement-breakpoint
ALTER TABLE "download_events" ALTER COLUMN "immich_asset_id" SET DATA TYPE uuid USING lower("immich_asset_id")::uuid;--> statement-breakpoint
ALTER TABLE "usages" ALTER COLUMN "immich_asset_id" SET DATA TYPE uuid USING lower("immich_asset_id")::uuid;--> statement-breakpoint
ALTER TABLE "usages" ALTER COLUMN "immich_album_id_snapshot" SET DATA TYPE uuid USING lower("immich_album_id_snapshot")::uuid;--> statement-breakpoint
ALTER TABLE "download_events" ADD COLUMN "intent_kind" text;--> statement-breakpoint
ALTER TABLE "download_events" ADD COLUMN "request_fingerprint" varchar(64);--> statement-breakpoint
UPDATE "download_events"
SET "request_fingerprint" = md5("editorial_account_id"::text || ':' || "id"::text) || md5("id"::text || ':' || "editorial_account_id"::text);--> statement-breakpoint
ALTER TABLE "download_events" ADD COLUMN "legacy_intent" boolean;--> statement-breakpoint
-- Preserve arbitrary legacy keys, while canonicalizing every legacy UUID key that
-- can be retried through the new UUID-only API. New rows are protected below.
UPDATE "download_events" AS target
SET "idempotency_key" = lower("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 "download_events" AS sibling
WHERE
sibling."editorial_account_id" = target."editorial_account_id"
AND sibling."id" <> target."id"
AND lower(sibling."idempotency_key") = lower(target."idempotency_key")
);--> statement-breakpoint
-- Legacy accounting deterministically treats the earliest event for each usage as
-- its billable creation intent. Later events are repeat downloads. These are the
-- only intent shapes whose original request can be reconstructed from stored data.
WITH ranked_legacy_events AS (
SELECT
"id",
row_number() OVER (
PARTITION BY "usage_id"
ORDER BY "authorized_at", "id"
) AS usage_event_number
FROM "download_events"
)
UPDATE "download_events" AS events
SET "intent_kind" = CASE
WHEN ranked.usage_event_number = 1 THEN 'new_usage'
ELSE 'repeat_download'
END
FROM ranked_legacy_events AS ranked
WHERE ranked."id" = events."id";--> statement-breakpoint
-- The string assembled here deliberately matches JSON.stringify's runtime field
-- order. Valid legacy rows therefore receive the same SHA-256 as an equivalent
-- retry; inconsistent rows receive a deterministic fingerprint no request can use.
UPDATE "download_events" AS events
SET "request_fingerprint" = CASE
WHEN
events."editorial_account_id" = usage."editorial_account_id"
AND events."immich_asset_id" = usage."immich_asset_id"
AND 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(events."immich_asset_id"::text)::text ||
',"albumId":' || to_json(usage."immich_album_id_snapshot"::text)::text ||
',"purpose":' || to_json(btrim(usage."purpose"))::text ||
',"reference":' || coalesce(to_json(nullif(btrim(usage."internal_reference"), ''))::text, 'null') ||
'}',
'UTF8'
)), 'hex')
WHEN
events."editorial_account_id" = usage."editorial_account_id"
AND events."immich_asset_id" = usage."immich_asset_id"
AND 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":' || coalesce(to_json(nullif(btrim(events."repeat_download_note"), ''))::text, 'null') ||
'}',
'UTF8'
)), 'hex')
ELSE encode(sha256(convert_to('unprovable-legacy-intent:' || 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'));--> statement-breakpoint
ALTER TABLE "download_events" ADD CONSTRAINT "download_events_request_fingerprint_hex_check" CHECK ("download_events"."request_fingerprint" ~ '^[0-9a-f]{64}$');
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}$');
+16 -5
View File
@@ -1,5 +1,5 @@
{
"id": "0cdb7b32-1eb5-4dcf-9949-3862dbbad2b4",
"id": "f650c8ba-ebde-411d-92d0-caf0f9885c44",
"prevId": "cfd5f4d1-55ac-46d9-9667-cd156f26e070",
"version": "7",
"dialect": "postgresql",
@@ -23,7 +23,7 @@
},
"immich_album_id": {
"name": "immich_album_id",
"type": "text",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
@@ -1157,7 +1157,7 @@
},
"immich_asset_id": {
"name": "immich_asset_id",
"type": "text",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
@@ -1224,6 +1224,13 @@
"type": "varchar(64)",
"primaryKey": false,
"notNull": true
},
"legacy_intent": {
"name": "legacy_intent",
"type": "boolean",
"primaryKey": false,
"notNull": true,
"default": false
}
},
"indexes": {
@@ -1288,6 +1295,10 @@
"download_events_request_fingerprint_hex_check": {
"name": "download_events_request_fingerprint_hex_check",
"value": "\"download_events\".\"request_fingerprint\" ~ '^[0-9a-f]{64}$'"
},
"download_events_canonical_idempotency_key_check": {
"name": "download_events_canonical_idempotency_key_check",
"value": "\"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}$'"
}
},
"isRLSEnabled": false
@@ -1311,7 +1322,7 @@
},
"immich_asset_id": {
"name": "immich_asset_id",
"type": "text",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
@@ -1323,7 +1334,7 @@
},
"immich_album_id_snapshot": {
"name": "immich_album_id_snapshot",
"type": "text",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
+1 -1
View File
@@ -19,7 +19,7 @@
{
"idx": 2,
"version": "7",
"when": 1786308409516,
"when": 1786352073526,
"tag": "0002_accounting_intents",
"breakpoints": true
}