@@ -207,35 +207,35 @@ export async function getJobProgress(tableId: string, jobId: string): Promise<nu
207207}
208208
209209/**
210- * One keyset page of rows for the export worker, ordered by `(position , id)`. Keyset (not
211- * OFFSET) keeps each page O(page) — offset paging re-scans every prior row per page, which is
212- * O(N²) across a large export. `(position, id)` is total (position exists on every row; id breaks
213- * ties) and served by the `(table_id, position)` index; under fractional ordering a manually
214- * reordered table may export in near-grid rather than exact grid order — the right trade for a
215- * bulk dump. The delete-job visibility mask applies, like every user-facing read.
210+ * One keyset page of rows for the export worker, ordered by `(order_key , id)` — the same
211+ * authoritative visual order the grid (`queryRows`) uses, so exports and snapshots match what the
212+ * user sees even after manual reorders. Keyset (not OFFSET) keeps each page O(page); `order_key` is
213+ * present on every row (always assigned on insert, backfilled for legacy rows) with `id` as the
214+ * tiebreaker, and the `(table_id, order_key, id)` index serves it. The delete-job visibility mask
215+ * applies, like every user-facing read.
216216 */
217217export async function selectExportRowPage (
218218 table : TableDefinition ,
219- after : { position : number ; id : string } | null ,
219+ after : { orderKey : string ; id : string } | null ,
220220 limit : number
221- ) : Promise < Array < { id : string ; data : RowData ; position : number } > > {
221+ ) : Promise < Array < { id : string ; data : RowData ; orderKey : string } > > {
222222 const deleteMask = await pendingDeleteMask ( table )
223223 const rows = await db
224- . select ( { id : userTableRows . id , data : userTableRows . data , position : userTableRows . position } )
224+ . select ( { id : userTableRows . id , data : userTableRows . data , orderKey : userTableRows . orderKey } )
225225 . from ( userTableRows )
226226 . where (
227227 and (
228228 eq ( userTableRows . tableId , table . id ) ,
229229 eq ( userTableRows . workspaceId , table . workspaceId ) ,
230230 deleteMask ,
231231 after
232- ? sql `(${ userTableRows . position } , ${ userTableRows . id } ) > (${ after . position } , ${ after . id } )`
232+ ? sql `(${ userTableRows . orderKey } , ${ userTableRows . id } ) > (${ after . orderKey } , ${ after . id } )`
233233 : undefined
234234 )
235235 )
236- . orderBy ( asc ( userTableRows . position ) , asc ( userTableRows . id ) )
236+ . orderBy ( asc ( userTableRows . orderKey ) , asc ( userTableRows . id ) )
237237 . limit ( limit )
238- return rows as Array < { id : string ; data : RowData ; position : number } >
238+ return rows as Array < { id : string ; data : RowData ; orderKey : string } >
239239}
240240
241241/** How long a terminal export stays listable (and re-downloadable from the tray). */
0 commit comments