-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathroute.ts
More file actions
403 lines (366 loc) · 14 KB
/
Copy pathroute.ts
File metadata and controls
403 lines (366 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import type { Readable } from 'node:stream'
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import { generateId } from '@sim/utils/id'
import { type NextRequest, NextResponse } from 'next/server'
import {
csvExtensionSchema,
csvImportCreateColumnsSchema,
csvImportFormSchema,
csvImportMappingSchema,
csvImportModeSchema,
tableIdParamsSchema,
} from '@/lib/api/contracts/tables'
import { getValidationErrorMessage } from '@/lib/api/server'
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
import { isMultipartError, readMultipart } from '@/lib/core/utils/multipart'
import { generateRequestId } from '@/lib/core/utils/request'
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
import {
buildAutoMapping,
CSV_MAX_FILE_SIZE_BYTES,
type CsvHeaderMapping,
CsvImportValidationError,
coerceRowsForTable,
createCsvParser,
dispatchAfterBatchInsert,
generateColumnId,
inferColumnType,
markTableJobRunning,
releaseJobClaim,
sanitizeName,
type TableDefinition,
type TableSchema,
validateMapping,
} from '@/lib/table'
import { importAppendRows, importReplaceRows } from '@/lib/table/import-data'
import {
accessError,
checkAccess,
csvProxyBodyCapResponse,
multipartErrorResponse,
} from '@/app/api/table/utils'
const logger = createLogger('TableImportCSVExisting')
export const runtime = 'nodejs'
export const dynamic = 'force-dynamic'
export const maxDuration = 300
interface RouteParams {
params: Promise<{ tableId: string }>
}
export const POST = withRouteHandler(async (request: NextRequest, { params }: RouteParams) => {
const requestId = generateRequestId()
const { tableId } = tableIdParamsSchema.parse(await params)
let fileStream: Readable | undefined
let claimedImportId: string | null = null
try {
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
if (!authResult.success || !authResult.userId) {
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
}
const oversize = csvProxyBodyCapResponse(request)
if (oversize) return oversize
let parsed: Awaited<ReturnType<typeof readMultipart>>
try {
parsed = await readMultipart(request, {
maxFileBytes: CSV_MAX_FILE_SIZE_BYTES,
requiredFieldsBeforeFile: ['workspaceId'],
signal: request.signal,
})
} catch (err) {
if (isMultipartError(err)) return multipartErrorResponse(err)
throw err
}
const { fields, file } = parsed
if (!file) {
return NextResponse.json({ error: 'CSV file is required' }, { status: 400 })
}
fileStream = file.stream
const workspaceIdResult = csvImportFormSchema.shape.workspaceId.safeParse(fields.workspaceId)
if (!workspaceIdResult.success) {
return NextResponse.json(
{ error: getValidationErrorMessage(workspaceIdResult.error) },
{ status: 400 }
)
}
const workspaceId = workspaceIdResult.data
const rawMode = fields.mode ?? 'append'
const modeValidation = csvImportModeSchema.safeParse(rawMode)
if (!modeValidation.success) {
return NextResponse.json(
{ error: `Invalid mode "${String(rawMode)}". Must be "append" or "replace".` },
{ status: 400 }
)
}
const mode = modeValidation.data
const ext = file.filename.split('.').pop()?.toLowerCase()
const extensionValidation = csvExtensionSchema.safeParse(ext)
if (!extensionValidation.success) {
return NextResponse.json(
{ error: getValidationErrorMessage(extensionValidation.error) },
{ status: 400 }
)
}
const accessResult = await checkAccess(tableId, authResult.userId, 'write')
if (!accessResult.ok) return accessError(accessResult, requestId, tableId)
const { table } = accessResult
if (table.workspaceId !== workspaceId) {
logger.warn(
`[${requestId}] Workspace ID mismatch for table ${tableId}. Provided: ${workspaceId}, Actual: ${table.workspaceId}`
)
return NextResponse.json({ error: 'Invalid workspace ID' }, { status: 400 })
}
if (table.archivedAt) {
return NextResponse.json({ error: 'Cannot import into an archived table' }, { status: 400 })
}
// Don't run a sync import on top of an in-flight background job — concurrent writers
// would insert at colliding row positions.
if (table.jobStatus === 'running') {
return NextResponse.json(
{ error: 'A job is already in progress for this table' },
{ status: 409 }
)
}
let mapping: CsvHeaderMapping | undefined
if (fields.mapping) {
const mappingValidation = csvImportMappingSchema.safeParse(fields.mapping)
if (!mappingValidation.success) {
return NextResponse.json(
{ error: getValidationErrorMessage(mappingValidation.error) },
{ status: 400 }
)
}
mapping = mappingValidation.data
}
let createColumns: string[] | undefined
if (fields.createColumns) {
const createColumnsValidation = csvImportCreateColumnsSchema.safeParse(fields.createColumns)
if (!createColumnsValidation.success) {
return NextResponse.json(
{ error: getValidationErrorMessage(createColumnsValidation.error) },
{ status: 400 }
)
}
createColumns = createColumnsValidation.data
}
const delimiter = extensionValidation.data === 'tsv' ? '\t' : ','
const parser = createCsvParser(delimiter)
// `.pipe` doesn't forward source errors; forward them so the iterator throws.
file.stream.on('error', (streamErr) => parser.destroy(streamErr))
file.stream.pipe(parser)
const rows: Record<string, unknown>[] = []
for await (const record of parser as AsyncIterable<Record<string, unknown>>) {
rows.push(record)
}
if (rows.length === 0) {
return NextResponse.json({ error: 'CSV file has no data rows' }, { status: 400 })
}
const headers = Object.keys(rows[0])
let effectiveMapping = mapping ?? buildAutoMapping(headers, table.schema)
let prospectiveTable: TableDefinition = table
const additions: { id?: string; name: string; type: string }[] = []
if (createColumns && createColumns.length > 0) {
const headerSet = new Set(headers)
const unknownHeaders = createColumns.filter((h) => !headerSet.has(h))
if (unknownHeaders.length > 0) {
return NextResponse.json(
{
error: `createColumns references unknown CSV headers: ${unknownHeaders.join(', ')}`,
},
{ status: 400 }
)
}
const usedNames = new Set(table.schema.columns.map((c) => c.name.toLowerCase()))
const updatedMapping: CsvHeaderMapping = { ...effectiveMapping }
const newColumns: TableSchema['columns'] = []
for (const header of createColumns) {
const base = sanitizeName(header)
let columnName = base
let suffix = 2
while (usedNames.has(columnName.toLowerCase())) {
columnName = `${base}_${suffix}`
suffix++
}
usedNames.add(columnName.toLowerCase())
const inferredType = inferColumnType(rows.map((r) => r[header]))
// Pre-assign the id so the prospective schema (used to coerce rows) and
// the persisted column (created in importAppendRows) share the same key.
const id = generateColumnId()
additions.push({ id, name: columnName, type: inferredType })
newColumns.push({
id,
name: columnName,
type: inferredType as TableSchema['columns'][number]['type'],
required: false,
unique: false,
})
updatedMapping[header] = columnName
}
prospectiveTable = {
...table,
schema: { columns: [...table.schema.columns, ...newColumns] },
}
effectiveMapping = updatedMapping
}
let validation: ReturnType<typeof validateMapping>
try {
validation = validateMapping({
csvHeaders: headers,
mapping: effectiveMapping,
tableSchema: prospectiveTable.schema,
})
} catch (err) {
if (err instanceof CsvImportValidationError) {
return NextResponse.json({ error: err.message, details: err.details }, { status: 400 })
}
throw err
}
if (validation.mappedHeaders.length === 0) {
return NextResponse.json(
{
error: `No CSV headers map to columns on the table. CSV headers: ${headers.join(', ')}. Table columns: ${prospectiveTable.schema.columns.map((c) => c.name).join(', ')}`,
},
{ status: 400 }
)
}
const coerced = coerceRowsForTable(rows, prospectiveTable.schema, validation.effectiveMap)
// Atomically claim the table before writing. The pre-check above reads a checkAccess snapshot
// taken before the parse/validation; a background import could claim the table in that window.
// markTableJobRunning is the single atomic gate (same one the async kickoff uses) — released in
// the finally so a sync import can't write concurrently with a background one (corrupts replace).
const syncImportId = generateId()
if (!(await markTableJobRunning(tableId, syncImportId, 'import'))) {
return NextResponse.json(
{ error: 'A job is already in progress for this table' },
{ status: 409 }
)
}
claimedImportId = syncImportId
if (mode === 'append') {
if (prospectiveTable.rowCount + coerced.length > prospectiveTable.maxRows) {
const deficit = prospectiveTable.rowCount + coerced.length - prospectiveTable.maxRows
return NextResponse.json(
{
error: `Append would exceed table row limit (${prospectiveTable.maxRows}). Currently ${prospectiveTable.rowCount} rows, ${coerced.length} new rows, ${deficit} over.`,
},
{ status: 400 }
)
}
try {
const { inserted: insertedRows, table: finalTable } = await importAppendRows(
table,
additions,
coerced,
{ workspaceId, userId: authResult.userId, requestId }
)
const inserted = insertedRows.length
// Fire trigger + scheduler AFTER the tx commits — both read through the
// global db connection and would otherwise see no rows.
dispatchAfterBatchInsert(finalTable, insertedRows, requestId, authResult.userId)
logger.info(`[${requestId}] Append CSV imported`, {
tableId: table.id,
fileName: file.filename,
mode,
inserted,
createdColumns: additions.length,
mappedColumns: validation.mappedHeaders.length,
skippedHeaders: validation.skippedHeaders.length,
})
return NextResponse.json({
success: true,
data: {
tableId: table.id,
mode,
insertedCount: inserted,
mappedColumns: validation.mappedHeaders,
skippedHeaders: validation.skippedHeaders,
unmappedColumns: validation.unmappedColumns,
sourceFile: file.filename,
},
})
} catch (err) {
const message = toError(err).message
logger.warn(`[${requestId}] Append failed for table ${tableId}`, {
total: coerced.length,
createdColumns: additions.length,
error: message,
})
const isClientError =
message.includes('row limit') ||
message.includes('Insufficient capacity') ||
message.includes('Schema validation') ||
message.includes('must be unique') ||
message.includes('Row size exceeds') ||
message.includes('already exists') ||
message.includes('Invalid column name') ||
/^Row \d+:/.test(message)
return NextResponse.json(
{
error: isClientError ? message : 'Failed to import CSV',
data: { insertedCount: 0 },
},
{ status: isClientError ? 400 : 500 }
)
}
}
try {
const result = await importReplaceRows(
table,
additions,
{ rows: coerced, workspaceId, userId: authResult.userId },
requestId
)
logger.info(`[${requestId}] Replace CSV imported`, {
tableId: table.id,
fileName: file.filename,
mode,
deleted: result.deletedCount,
inserted: result.insertedCount,
createdColumns: additions.length,
mappedColumns: validation.mappedHeaders.length,
})
return NextResponse.json({
success: true,
data: {
tableId: table.id,
mode,
deletedCount: result.deletedCount,
insertedCount: result.insertedCount,
mappedColumns: validation.mappedHeaders,
skippedHeaders: validation.skippedHeaders,
unmappedColumns: validation.unmappedColumns,
sourceFile: file.filename,
},
})
} catch (err) {
const message = toError(err).message
const isClientError =
message.includes('row limit') ||
message.includes('Schema validation') ||
message.includes('must be unique') ||
message.includes('Row size exceeds') ||
message.includes('already exists') ||
message.includes('Invalid column name') ||
/^Row \d+:/.test(message)
if (isClientError) {
return NextResponse.json({ error: message }, { status: 400 })
}
throw err
}
} catch (error) {
if (isMultipartError(error)) return multipartErrorResponse(error)
const message = toError(error).message
logger.error(`[${requestId}] CSV import into existing table failed:`, error)
const isClientError =
message.includes('CSV file has no') ||
message.includes('already exists') ||
message.includes('Invalid column name')
return NextResponse.json(
{ error: isClientError ? message : 'Failed to import CSV' },
{ status: isClientError ? 400 : 500 }
)
} finally {
fileStream?.destroy()
// Release before the response returns, so a client refetch never observes the transient claim.
if (claimedImportId) await releaseJobClaim(tableId, claimedImportId).catch(() => {})
}
})