(db: DrizzleDb, prev: string | null, next: string)
| 995 | // LOCKS the cursor row until commit: a concurrent run's CAS waits here, then |
| 996 | // re-reads the moved cursor and matches nothing. |
| 997 | const advanceCursor = async (db: DrizzleDb, prev: string | null, next: string) => { |
| 998 | const now = new Date(); |
| 999 | if (prev === null) { |
| 1000 | // First advance: mint the row, or claim an existing row that still has |
| 1001 | // no cursor. A row that already carries one belongs to another run and |
| 1002 | // is left alone. |
| 1003 | const written = await db |
| 1004 | .insert(workosSync) |
| 1005 | .values({ id: WORKOS_EVENTS_STREAM_ID, cursor: next, updatedAt: now }) |
| 1006 | .onConflictDoUpdate({ |
| 1007 | target: workosSync.id, |
| 1008 | set: { cursor: next, updatedAt: now }, |
| 1009 | setWhere: isNull(workosSync.cursor), |
| 1010 | }) |
| 1011 | .returning({ id: workosSync.id }); |
| 1012 | return written.length > 0; |
| 1013 | } |
| 1014 | const written = await db |
| 1015 | .update(workosSync) |
| 1016 | .set({ cursor: next, updatedAt: now }) |
| 1017 | .where(and(eq(workosSync.id, WORKOS_EVENTS_STREAM_ID), eq(workosSync.cursor, prev))) |
| 1018 | .returning({ id: workosSync.id }); |
| 1019 | return written.length > 0; |
| 1020 | }; |
| 1021 | |
| 1022 | // Claim the organization for a scan listed at `listedAt`: move its |
| 1023 | // `backfilled_at` forward to `listedAt` if the recorded mark is older (or |
no test coverage detected