Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address comments
  • Loading branch information
icecrasher321 committed Mar 16, 2026
commit 5ad358c2341fea42a1a2804a268b601fc84480d4
70 changes: 38 additions & 32 deletions apps/sim/app/api/table/import-csv/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { generateRequestId } from '@/lib/core/utils/request'
import {
batchInsertRows,
createTable,
deleteTable,
getWorkspaceTableLimits,
type TableSchema,
} from '@/lib/table'
Expand Down Expand Up @@ -81,11 +82,11 @@ function inferSchema(headers: string[], rows: Record<string, unknown>[]): Column
return headers.map((name) => {
let colName = sanitizeName(name)
let suffix = 2
while (seen.has(colName)) {
while (seen.has(colName.toLowerCase())) {
colName = `${sanitizeName(name)}_${suffix}`
suffix++
}
seen.add(colName)
seen.add(colName.toLowerCase())

return {
name: colName,
Expand Down Expand Up @@ -217,38 +218,43 @@ export async function POST(request: NextRequest) {
requestId
)

const coerced = coerceRows(rows, columns, headerToColumn)
let inserted = 0
for (let i = 0; i < coerced.length; i += MAX_BATCH_SIZE) {
const batch = coerced.slice(i, i + MAX_BATCH_SIZE)
const batchRequestId = crypto.randomUUID().slice(0, 8)
const result = await batchInsertRows(
{ tableId: table.id, rows: batch, workspaceId },
table,
batchRequestId
)
inserted += result.length
}
try {
const coerced = coerceRows(rows, columns, headerToColumn)
let inserted = 0
for (let i = 0; i < coerced.length; i += MAX_BATCH_SIZE) {
const batch = coerced.slice(i, i + MAX_BATCH_SIZE)
const batchRequestId = crypto.randomUUID().slice(0, 8)
const result = await batchInsertRows(
{ tableId: table.id, rows: batch, workspaceId },
table,
batchRequestId
)
Comment thread
icecrasher321 marked this conversation as resolved.
inserted += result.length
}

logger.info(`[${requestId}] CSV imported`, {
tableId: table.id,
fileName: file.name,
columns: columns.length,
rows: inserted,
})

return NextResponse.json({
success: true,
data: {
table: {
id: table.id,
name: table.name,
description: table.description,
schema: normalizedSchema,
rowCount: inserted,
logger.info(`[${requestId}] CSV imported`, {
tableId: table.id,
fileName: file.name,
columns: columns.length,
rows: inserted,
})

return NextResponse.json({
success: true,
data: {
table: {
id: table.id,
name: table.name,
description: table.description,
schema: normalizedSchema,
rowCount: inserted,
},
},
},
})
})
} catch (insertError) {
await deleteTable(table.id, requestId).catch(() => {})
throw insertError
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
logger.error(`[${requestId}] CSV import failed:`, error)
Expand Down
3 changes: 2 additions & 1 deletion apps/sim/app/workspace/[workspaceId]/tables/tables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ export function Tables() {
for (let i = 0; i < csvFiles.length; i++) {
try {
const result = await uploadCsv.mutateAsync({ workspaceId, file: csvFiles[i] })
setUploadProgress({ completed: i + 1, total: csvFiles.length })

if (csvFiles.length === 1) {
const tableId = result?.data?.table?.id
Expand All @@ -193,6 +192,8 @@ export function Tables() {
} catch (err) {
failed.push(csvFiles[i].name)
logger.error('Error uploading CSV:', err)
} finally {
setUploadProgress({ completed: i + 1, total: csvFiles.length })
}
Comment thread
icecrasher321 marked this conversation as resolved.
Comment thread
icecrasher321 marked this conversation as resolved.
}
Comment thread
icecrasher321 marked this conversation as resolved.

Expand Down
7 changes: 3 additions & 4 deletions apps/sim/hooks/queries/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,13 +760,12 @@ export function useUploadCsvToTable() {
body: formData,
})

const data = await response.json()

if (!data.success) {
if (!response.ok) {
const data = await response.json().catch(() => ({}))
throw new Error(data.error || 'CSV import failed')
}

return data
return response.json()
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: tableKeys.lists() })
Expand Down
Loading