Skip to content

Commit cd81e29

Browse files
committed
fix(tables): a table name that starts with tbl_ is still addressable by name; only a reference shaped like a minted id skips the name lookup
1 parent 705a065 commit cd81e29

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

‎apps/sim/lib/table/application/context.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,23 @@ describe('table application context', () => {
9696

9797
// A stale id is never read as a name, so it costs no second query.
9898
await expect(
99-
resolveActiveTableContext({ tableId: 'tbl_gone', assertedWorkspaceId: 'workspace-1' })
99+
resolveActiveTableContext({
100+
tableId: 'tbl_0123456789abcdef0123456789abcdef',
101+
assertedWorkspaceId: 'workspace-1',
102+
})
100103
).rejects.toMatchObject({ code: 'not_found' })
101104
expect(findByName).not.toHaveBeenCalled()
102105

106+
// A name that only starts like an id is still a name.
107+
findByName.mockResolvedValueOnce([
108+
{ id: 'table-2', workspaceId: 'workspace-1', name: 'tbl_sales' },
109+
])
110+
await expect(
111+
resolveActiveTableContext({ tableId: 'tbl_sales', assertedWorkspaceId: 'workspace-1' })
112+
).resolves.toMatchObject({ tableId: 'table-2' })
113+
expect(findByName).toHaveBeenLastCalledWith('workspace-1', 'tbl_sales')
114+
findByName.mockClear()
115+
103116
// A name never reaches past the asserted workspace, even if the lookup returned a foreign row.
104117
findByName.mockResolvedValueOnce([{ id: 'table-1', workspaceId: 'workspace-1', name: 'leads' }])
105118
await expect(

‎apps/sim/lib/table/application/context.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { OrchestrationError } from '@/lib/core/orchestration/types'
22
import type { TableAuthorizationContext } from '@/lib/table/application/authorization'
33
import { TABLE_NOT_FOUND_MESSAGE } from '@/lib/table/constants'
4-
import { TABLE_ID_PREFIX } from '@/lib/table/ids'
4+
import { isTableIdShaped } from '@/lib/table/ids'
55
import { findActiveTablesByExactName, getTableById } from '@/lib/table/service'
66
import type { TableDefinition } from '@/lib/table/types'
77
import { loadActiveWorkspaceApplicationContext } from '@/lib/workspaces/application/context'
@@ -33,7 +33,7 @@ export async function resolveTableWorkspaceContext(
3333
*/
3434
async function requireTable(tableId: string, workspaceId: string | undefined) {
3535
let table = await getTableById(tableId)
36-
if (!table && workspaceId !== undefined && !tableId.startsWith(TABLE_ID_PREFIX)) {
36+
if (!table && workspaceId !== undefined && !isTableIdShaped(tableId)) {
3737
const named = await findActiveTablesByExactName(workspaceId, tableId)
3838
if (named.length === 1) table = named[0]
3939
}

‎apps/sim/lib/table/ids.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import { generateId } from '@sim/utils/id'
22

3-
/** What every table id starts with: a reference that does not is no table id. */
4-
export const TABLE_ID_PREFIX = 'tbl_'
3+
/** What every table id starts with. */
4+
const TABLE_ID_PREFIX = 'tbl_'
5+
6+
/** The shape {@link generateTableId} mints: `tbl_` and 32 lowercase hex digits. */
7+
const TABLE_ID_SHAPE = /^tbl_[0-9a-f]{32}$/
8+
9+
/**
10+
* Whether `reference` is shaped like a table id, so it is never read as a table name: a name
11+
* that merely starts with `tbl_` (`tbl_sales`) still is one.
12+
*/
13+
export function isTableIdShaped(reference: string): boolean {
14+
return TABLE_ID_SHAPE.test(reference)
15+
}
516

617
/**
718
* Whether `reference`, a table route's `{tableId}`, names `table`: its id, or (when not shaped
819
* like an id) its name, which is unique among a workspace's active tables.
920
*/
1021
export function referencesTable(reference: string, table: { id: string; name: string }): boolean {
11-
return (
12-
reference === table.id || (!reference.startsWith(TABLE_ID_PREFIX) && reference === table.name)
13-
)
22+
return reference === table.id || (!isTableIdShaped(reference) && reference === table.name)
1423
}
1524

1625
/**

0 commit comments

Comments
 (0)