( tableId: string, newName: string, requestId: string )
| 496 | * @throws Error if name is invalid |
| 497 | */ |
| 498 | export async function renameTable( |
| 499 | tableId: string, |
| 500 | newName: string, |
| 501 | requestId: string |
| 502 | ): Promise<{ id: string; name: string }> { |
| 503 | const nameValidation = validateTableName(newName) |
| 504 | if (!nameValidation.valid) { |
| 505 | throw new Error(nameValidation.errors.join(', ')) |
| 506 | } |
| 507 | |
| 508 | const now = new Date() |
| 509 | try { |
| 510 | const result = await db |
| 511 | .update(userTableDefinitions) |
| 512 | .set({ name: newName, updatedAt: now }) |
| 513 | .where(eq(userTableDefinitions.id, tableId)) |
| 514 | .returning({ id: userTableDefinitions.id }) |
| 515 | |
| 516 | if (result.length === 0) { |
| 517 | throw new Error(`Table ${tableId} not found`) |
| 518 | } |
| 519 | |
| 520 | logger.info(`[${requestId}] Renamed table ${tableId} to "${newName}"`) |
| 521 | return { id: tableId, name: newName } |
| 522 | } catch (error: unknown) { |
| 523 | if (getPostgresErrorCode(error) === '23505') { |
| 524 | throw new TableConflictError(newName) |
| 525 | } |
| 526 | throw error |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Updates a table's metadata (UI state like column widths/order, plus behavioral |
no test coverage detected