Skip to content

Commit cf554e4

Browse files
committed
Fix #936: Preserve audit-log cursor traversal during inserts
Closes #936 - Add missing id column to marketAuditLog table to fix pagination. - Change id primary keys in auditLogs and marketAuditLog to generate UUIDv7 instead of UUIDv4. UUIDv7 guarantees strict monotonic time-ordering which prevents pagination skips during concurrent transactions or bulk inserts where createdAt timestamps are identical. - Add compound index for pagination to marketAuditLog.
1 parent 07b46d5 commit cf554e4

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

src/db/schema.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { v7 as uuidv7 } from "uuid";
12
import {
23
pgTable,
34
uuid,
@@ -143,18 +144,30 @@ export const markets = pgTable("markets", {
143144
.defaultNow(),
144145
});
145146

146-
export const marketAuditLog = pgTable("market_audit_log", {
147-
marketId: text("market_id")
148-
.notNull()
149-
.references(() => markets.id),
147+
export const marketAuditLog = pgTable(
148+
"market_audit_log",
149+
{
150+
id: uuid("id")
151+
.primaryKey()
152+
.$defaultFn(() => uuidv7()),
153+
marketId: text("market_id")
154+
.notNull()
155+
.references(() => markets.id),
150156
adminAddress: text("admin_address").notNull(),
151157
action: text("action").notNull(),
152158
beforeState: jsonb("before_state").notNull(),
153159
afterState: jsonb("after_state").notNull(),
154-
createdAt: timestamp("created_at", { withTimezone: true })
155-
.notNull()
156-
.defaultNow(),
157-
});
160+
createdAt: timestamp("created_at", { withTimezone: true })
161+
.notNull()
162+
.defaultNow(),
163+
},
164+
(t) => ({
165+
marketAuditLogCreatedAtIdIdx: index("market_audit_log_created_at_id_idx").on(
166+
t.createdAt,
167+
t.id,
168+
),
169+
}),
170+
);
158171

159172
export const predictions = pgTable("predictions", {
160173
id: uuid("id").primaryKey().defaultRandom(),
@@ -433,7 +446,9 @@ export const notifications = pgTable(
433446
export const auditLogs = pgTable(
434447
"audit_logs",
435448
{
436-
id: uuid("id").primaryKey().defaultRandom(),
449+
id: uuid("id")
450+
.primaryKey()
451+
.$defaultFn(() => uuidv7()),
437452
action: text("action").notNull(),
438453
walletAddress: text("wallet_address"),
439454
ip: text("ip").notNull(),

0 commit comments

Comments
 (0)