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

+9 -3
View File
@@ -1,6 +1,7 @@
import { sql } from "drizzle-orm";
import {
bigint,
boolean,
check,
integer,
pgEnum,
@@ -32,9 +33,9 @@ export const usages = pgTable("usages", {
editorialAccountId: uuid("editorial_account_id")
.notNull()
.references(() => editorialAccounts.id, { onDelete: "restrict" }),
immichAssetId: text("immich_asset_id").notNull(),
immichAssetId: uuid("immich_asset_id").notNull(),
filenameSnapshot: text("filename_snapshot").notNull(),
immichAlbumIdSnapshot: text("immich_album_id_snapshot").notNull(),
immichAlbumIdSnapshot: uuid("immich_album_id_snapshot").notNull(),
albumNameSnapshot: text("album_name_snapshot").notNull(),
purpose: text("purpose").notNull(),
internalReference: text("internal_reference"),
@@ -56,7 +57,7 @@ export const downloadEvents = pgTable(
usageId: uuid("usage_id")
.notNull()
.references(() => usages.id, { onDelete: "restrict" }),
immichAssetId: text("immich_asset_id").notNull(),
immichAssetId: uuid("immich_asset_id").notNull(),
authorizedAt: timestamp("authorized_at", { withTimezone: true }).notNull().defaultNow(),
startedAt: timestamp("started_at", { withTimezone: true }),
completedAt: timestamp("completed_at", { withTimezone: true }),
@@ -67,6 +68,7 @@ export const downloadEvents = pgTable(
idempotencyKey: text("idempotency_key").notNull(),
intentKind: text("intent_kind").notNull().default("new_usage"),
requestFingerprint: varchar("request_fingerprint", { length: 64 }).notNull(),
legacyIntent: boolean("legacy_intent").notNull().default(false),
},
(table) => [
uniqueIndex("download_events_editorial_idempotency_key_unique").on(
@@ -81,5 +83,9 @@ export const downloadEvents = pgTable(
"download_events_request_fingerprint_hex_check",
sql`${table.requestFingerprint} ~ '^[0-9a-f]{64}$'`,
),
check(
"download_events_canonical_idempotency_key_check",
sql`${table.legacyIntent} OR ${table.idempotencyKey} ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'`,
),
],
);