Skip to content

Commit 5ad358c

Browse files
committed
address comments
1 parent 6802bf7 commit 5ad358c

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

apps/sim/app/api/table/import-csv/route.ts

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { generateRequestId } from '@/lib/core/utils/request'
55
import {
66
batchInsertRows,
77
createTable,
8+
deleteTable,
89
getWorkspaceTableLimits,
910
type TableSchema,
1011
} from '@/lib/table'
@@ -81,11 +82,11 @@ function inferSchema(headers: string[], rows: Record<string, unknown>[]): Column
8182
return headers.map((name) => {
8283
let colName = sanitizeName(name)
8384
let suffix = 2
84-
while (seen.has(colName)) {
85+
while (seen.has(colName.toLowerCase())) {
8586
colName = `${sanitizeName(name)}_${suffix}`
8687
suffix++
8788
}
88-
seen.add(colName)
89+
seen.add(colName.toLowerCase())
8990

9091
return {
9192
name: colName,
@@ -217,38 +218,43 @@ export async function POST(request: NextRequest) {
217218
requestId
218219
)
219220

220-
const coerced = coerceRows(rows, columns, headerToColumn)
221-
let inserted = 0
222-
for (let i = 0; i < coerced.length; i += MAX_BATCH_SIZE) {
223-
const batch = coerced.slice(i, i + MAX_BATCH_SIZE)
224-
const batchRequestId = crypto.randomUUID().slice(0, 8)
225-
const result = await batchInsertRows(
226-
{ tableId: table.id, rows: batch, workspaceId },
227-
table,
228-
batchRequestId
229-
)
230-
inserted += result.length
231-
}
221+
try {
222+
const coerced = coerceRows(rows, columns, headerToColumn)
223+
let inserted = 0
224+
for (let i = 0; i < coerced.length; i += MAX_BATCH_SIZE) {
225+
const batch = coerced.slice(i, i + MAX_BATCH_SIZE)
226+
const batchRequestId = crypto.randomUUID().slice(0, 8)
227+
const result = await batchInsertRows(
228+
{ tableId: table.id, rows: batch, workspaceId },
229+
table,
230+
batchRequestId
231+
)
232+
inserted += result.length
233+
}
232234

233-
logger.info(`[${requestId}] CSV imported`, {
234-
tableId: table.id,
235-
fileName: file.name,
236-
columns: columns.length,
237-
rows: inserted,
238-
})
239-
240-
return NextResponse.json({
241-
success: true,
242-
data: {
243-
table: {
244-
id: table.id,
245-
name: table.name,
246-
description: table.description,
247-
schema: normalizedSchema,
248-
rowCount: inserted,
235+
logger.info(`[${requestId}] CSV imported`, {
236+
tableId: table.id,
237+
fileName: file.name,
238+
columns: columns.length,
239+
rows: inserted,
240+
})
241+
242+
return NextResponse.json({
243+
success: true,
244+
data: {
245+
table: {
246+
id: table.id,
247+
name: table.name,
248+
description: table.description,
249+
schema: normalizedSchema,
250+
rowCount: inserted,
251+
},
249252
},
250-
},
251-
})
253+
})
254+
} catch (insertError) {
255+
await deleteTable(table.id, requestId).catch(() => {})
256+
throw insertError
257+
}
252258
} catch (error) {
253259
const message = error instanceof Error ? error.message : String(error)
254260
logger.error(`[${requestId}] CSV import failed:`, error)

apps/sim/app/workspace/[workspaceId]/tables/tables.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ export function Tables() {
182182
for (let i = 0; i < csvFiles.length; i++) {
183183
try {
184184
const result = await uploadCsv.mutateAsync({ workspaceId, file: csvFiles[i] })
185-
setUploadProgress({ completed: i + 1, total: csvFiles.length })
186185

187186
if (csvFiles.length === 1) {
188187
const tableId = result?.data?.table?.id
@@ -193,6 +192,8 @@ export function Tables() {
193192
} catch (err) {
194193
failed.push(csvFiles[i].name)
195194
logger.error('Error uploading CSV:', err)
195+
} finally {
196+
setUploadProgress({ completed: i + 1, total: csvFiles.length })
196197
}
197198
}
198199

apps/sim/hooks/queries/tables.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,13 +760,12 @@ export function useUploadCsvToTable() {
760760
body: formData,
761761
})
762762

763-
const data = await response.json()
764-
765-
if (!data.success) {
763+
if (!response.ok) {
764+
const data = await response.json().catch(() => ({}))
766765
throw new Error(data.error || 'CSV import failed')
767766
}
768767

769-
return data
768+
return response.json()
770769
},
771770
onSettled: () => {
772771
queryClient.invalidateQueries({ queryKey: tableKeys.lists() })

0 commit comments

Comments
 (0)