( tableIds: string[] )
| 106 | |
| 107 | /** Latest non-export job per table for a batch of ids, via `DISTINCT ON (table_id)`. */ |
| 108 | export async function latestJobsForTables( |
| 109 | tableIds: string[] |
| 110 | ): Promise<Map<string, DerivedJobFields>> { |
| 111 | const map = new Map<string, DerivedJobFields>() |
| 112 | if (tableIds.length === 0) return map |
| 113 | const rows = await db |
| 114 | .selectDistinctOn([tableJobs.tableId], { tableId: tableJobs.tableId, ...JOB_PROJECTION }) |
| 115 | .from(tableJobs) |
| 116 | .where(and(inArray(tableJobs.tableId, tableIds), ne(tableJobs.type, 'export'))) |
| 117 | .orderBy(tableJobs.tableId, desc(tableJobs.startedAt)) |
| 118 | for (const row of rows) map.set(row.tableId, mapJobRow(row)) |
| 119 | return map |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Atomically claims a table's single background-job slot by inserting a `running` row into |
no test coverage detected