fix(tables): refuse adding a required column that rows leave empty

addTableColumn now counts the rows the new column leaves empty (every row for a fresh column, the
rows without a value for one re-added under its old id) and refuses a required column over any,
so every surface (REST, v1, v2 reshape, Copilot, workspace source) refuses what the workspace
source plan already did. Undoing a column delete re-adds the column optional, restores its cells,
then sets it required.
This commit is contained in:
Waleed Latif committed 2026-11-18 09:55:11 -08:00
1 parent 5fc59b1db1
commit facb1c4f4d
3 files changed
+94 -24

No files matched your search

+30 -23
View File
@@ -384,7 +384,10 @@ export function useTableUndo({
...(action.columnId ? { id: action.columnId } : {}),
name: action.columnName,
type: action.columnType,
required: action.columnRequired,
// Re-added optional: its cells are restored below, and only then can the
// server accept the column as required (it refuses a required column that
// any row leaves empty).
required: false,
unique: action.columnUnique,
// A select column is rejected without its options, and the
// cell data restored below is keyed by those option ids.
@@ -395,30 +398,34 @@ export function useTableUndo({
},
{
onSuccess: () => {
if (action.cellData.length > 0) {
const updates = action.cellData.map((c) => ({
rowId: c.rowId,
data: { [colKey]: c.value },
}))
void (async () => {
try {
for (
let i = 0;
i < updates.length;
i += TABLE_LIMITS.MAX_BULK_OPERATION_SIZE
) {
await batchUpdateRowsMutation.mutateAsync({
updates: updates.slice(i, i + TABLE_LIMITS.MAX_BULK_OPERATION_SIZE),
})
}
} catch (error) {
logger.error('Failed to restore cell data on delete-column undo', {
columnName: action.columnName,
error,
const updates = action.cellData.map((c) => ({
rowId: c.rowId,
data: { [colKey]: c.value },
}))
void (async () => {
try {
for (
let i = 0;
i < updates.length;
i += TABLE_LIMITS.MAX_BULK_OPERATION_SIZE
) {
await batchUpdateRowsMutation.mutateAsync({
updates: updates.slice(i, i + TABLE_LIMITS.MAX_BULK_OPERATION_SIZE),
})
}
})()
}
if (action.columnRequired) {
await updateColumnMutation.mutateAsync({
columnName: colKey,
updates: { required: true },
})
}
} catch (error) {
logger.error('Failed to restore cell data on delete-column undo', {
columnName: action.columnName,
error,
})
}
})()
// Cell restore above runs everywhere — it's row data. The
// layout below belongs to the recorded view: elsewhere the
// restored column still reappears via the grid's append
+13
View File
@@ -197,6 +197,19 @@ export async function addTableColumn(
const newColumnId = getColumnId(newColumn)
// A fresh column is empty in every row; one re-added under its old id (an undo of a
// delete) holds only what its cells still carry. Either way a required column refuses rows
// it leaves empty, as setting `required` on an existing column does.
if (newColumn.required) {
const emptyCount = await countEmptyCells(trx, tableId, table.workspaceId, newColumnId)
if (emptyCount > 0) {
throw new OrchestrationError(
'validation',
`Column "${column.name}" cannot be added as required: ${emptyCount} row(s) would have no value. Add it without required, fill its cells, then make it required.`
)
}
}
const columns = [...schema.columns]
if (
column.position !== undefined &&
+51 -1
View File
@@ -22,7 +22,7 @@ vi.mock('@/lib/table/workflow-columns', () => tableWorkflowColumnsMock)
vi.mock('@/lib/uploads/core/storage-service', () => storageServiceMock)
import { encryptSecret } from '@/lib/core/security/encryption'
import { deleteColumn, updateColumnConstraints } from '@/lib/table/columns/service'
import { addTableColumn, deleteColumn, updateColumnConstraints } from '@/lib/table/columns/service'
import { getMaxRowSizeBytes } from '@/lib/table/constants'
import { bulkInsertImportBatch, importAppendRows, importReplaceRows } from '@/lib/table/import-data'
import { selectExportRowPage } from '@/lib/table/jobs/service'
@@ -2482,4 +2482,54 @@ describe('table row writes against real PostgreSQL', () => {
}
})
})
describe('adding a required column', () => {
it('refuses a new required column on a table that has rows, from every surface', async () => {
const table = await createTable(textColumns('note'))
await seedRows(table.id, [{ id: generateId(), data: { note: 'kept' }, orderKey: 'a0' }])
await expect(
addTableColumn(table.id, { name: 'owner', type: 'string', required: true }, 'required-add')
).rejects.toThrow(/cannot be added as required: 1 row\(s\) would have no value/)
expect((await getTableById(table.id))?.schema.columns.map((column) => column.name)).toEqual([
'note',
])
})
it('adds a required column to an empty table', async () => {
const table = await createTable(textColumns('note'))
const updated = await addTableColumn(
table.id,
{ name: 'owner', type: 'string', required: true },
'required-empty'
)
expect(updated.schema.columns.find((column) => column.name === 'owner')?.required).toBe(true)
})
it('restores a deleted required column only when no row leaves it empty', async () => {
const table = await createTable([
{ id: 'owner', name: 'owner', type: 'string', required: true },
...textColumns('note'),
])
await seedRows(table.id, [
{ id: generateId(), data: { owner: 'ana', note: 'a' }, orderKey: 'a0' },
])
await deleteColumn({ tableId: table.id, columnName: 'owner' }, 'undo-delete')
// A row added after the delete never held the column, so an undo cannot restore it required.
await seedRows(table.id, [{ id: generateId(), data: { note: 'b' }, orderKey: 'a1' }])
await expect(
addTableColumn(
table.id,
{ id: 'owner', name: 'owner', type: 'string', required: true },
'undo-required'
)
).rejects.toThrow(/cannot be added as required/)
const restored = await addTableColumn(
table.id,
{ id: 'owner', name: 'owner', type: 'string' },
'undo-optional'
)
expect(restored.schema.columns.find((column) => column.name === 'owner')?.required).toBe(
false
)
})
})
})