Skip to content

Commit 6b79524

Browse files
committed
fix(table): address PR bot review
- Move the `isAppendingRowRef` reset into the create-row mutation's `onSettled` callback. The previous `try/finally` cleared the guard immediately after `mutate()` returned, before the request completed, so a rapid second click on "New row" could fire a duplicate create. - Drop the unused `addTableColumns` wrapper from `lib/table/service`. The CSV import flow only ever uses the transaction-bound `addTableColumnsWithTx`; the standalone wrapper was dead code.
1 parent 9885bf7 commit 6b79524

2 files changed

Lines changed: 31 additions & 55 deletions

File tree

  • apps/sim

apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table/table.tsx

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -603,35 +603,35 @@ export function Table({
603603
if (isAppendingRowRef.current) return
604604
isAppendingRowRef.current = true
605605
try {
606-
try {
607-
while (hasNextPageRef.current) {
608-
const result = await fetchNextPageRef.current()
609-
if (!result.hasNextPage) break
610-
}
611-
} catch (error) {
612-
logger.error('Failed to load remaining rows before appending', { error })
613-
toast.error('Failed to load all rows. Try again.', { duration: 5000 })
614-
return
606+
while (hasNextPageRef.current) {
607+
const result = await fetchNextPageRef.current()
608+
if (!result.hasNextPage) break
615609
}
616-
617-
createRef.current(
618-
{ data: {} },
619-
{
620-
onSuccess: (response: Record<string, unknown>) => {
621-
const newRowId = extractCreatedRowId(response)
622-
if (newRowId) {
623-
pushUndoRef.current({
624-
type: 'create-row',
625-
rowId: newRowId,
626-
position: maxPositionRef.current + 1,
627-
})
628-
}
629-
},
630-
}
631-
)
632-
} finally {
610+
} catch (error) {
633611
isAppendingRowRef.current = false
612+
logger.error('Failed to load remaining rows before appending', { error })
613+
toast.error('Failed to load all rows. Try again.', { duration: 5000 })
614+
return
634615
}
616+
617+
createRef.current(
618+
{ data: {} },
619+
{
620+
onSuccess: (response: Record<string, unknown>) => {
621+
const newRowId = extractCreatedRowId(response)
622+
if (newRowId) {
623+
pushUndoRef.current({
624+
type: 'create-row',
625+
rowId: newRowId,
626+
position: maxPositionRef.current + 1,
627+
})
628+
}
629+
},
630+
onSettled: () => {
631+
isAppendingRowRef.current = false
632+
},
633+
}
634+
)
635635
}, [])
636636

637637
const handleRowContextMenu = useCallback(

apps/sim/lib/table/service.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -470,35 +470,11 @@ export async function addTableColumn(
470470
}
471471

472472
/**
473-
* Adds multiple columns to an existing table in a single schema update. This is
474-
* atomic: either all columns are added or none are. Validates each column the
475-
* same way `addTableColumn` does and rejects if any name collides with an
476-
* existing column or another entry in `columns`.
477-
*
478-
* @param tableId - Table ID to update
479-
* @param columns - Column definitions to add (appended in order)
480-
* @param requestId - Request ID for logging
481-
* @returns Updated table definition
482-
* @throws Error if table not found, column invalid, or name collides
483-
*/
484-
export async function addTableColumns(
485-
tableId: string,
486-
columns: { name: string; type: string; required?: boolean; unique?: boolean }[],
487-
requestId: string
488-
): Promise<TableDefinition> {
489-
const table = await getTableById(tableId)
490-
if (!table) {
491-
throw new Error('Table not found')
492-
}
493-
if (columns.length === 0) return table
494-
495-
return db.transaction((trx) => addTableColumnsWithTx(trx, table, columns, requestId))
496-
}
497-
498-
/**
499-
* Transaction-bound variant of `addTableColumns`. Caller passes the current
500-
* `table` definition (read by caller) and an open `trx`; this function performs
501-
* the schema UPDATE inside that transaction and returns the new definition.
473+
* Adds multiple columns to an existing table inside a caller-provided
474+
* transaction. This is atomic with respect to the surrounding `trx`: either
475+
* all columns are added or none are. Validates each column the same way
476+
* `addTableColumn` does and rejects if any name collides with an existing
477+
* column or another entry in `columns`.
502478
*
503479
* Use this when composing a column addition with other writes (e.g., row
504480
* inserts) that must succeed or roll back together.

0 commit comments

Comments
 (0)