@@ -5,6 +5,7 @@ import { generateRequestId } from '@/lib/core/utils/request'
55import {
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 )
0 commit comments