Skip to content

Commit db8a55e

Browse files
committed
fix(tables): mint fork-copied table ids with the same tbl_ shape as created ones
1 parent f62386d commit db8a55e

6 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎apps/sim/ee/workspace-forking/lib/copy/copy-resources.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,10 @@ describe('copyForkResourceContainers table views', () => {
13071307

13081308
const copiedTableId = result.idMap.get('table')?.get('table-with-view')
13091309
const legacyTableId = result.idMap.get('table')?.get('legacy-table')
1310+
// A copied table's id has the same shape as a created one, so nothing downstream can
1311+
// tell which path minted it.
1312+
expect(copiedTableId).toMatch(/^tbl_[0-9a-f]{32}$/)
1313+
expect(legacyTableId).toMatch(/^tbl_[0-9a-f]{32}$/)
13101314
const copiedViews = inserted.get(tableViews)
13111315
expect(copiedViews).toEqual(
13121316
expect.arrayContaining([

‎apps/sim/ee/workspace-forking/lib/copy/copy-resources.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
replaceKnowledgeDocumentSecretProvenanceInTx,
5858
} from '@/lib/knowledge/secret-provenance'
5959
import { DEFAULT_TABLE_VIEW_NAME } from '@/lib/table/constants'
60+
import { generateTableId } from '@/lib/table/ids'
6061
import { nKeysBetween } from '@/lib/table/order-key'
6162
import {
6263
classifyTableRowSecretProvenanceForCopy,
@@ -673,7 +674,7 @@ export async function copyForkResourceContainers(
673674
const inserts: (typeof userTableDefinitions.$inferInsert)[] = []
674675
const viewInserts: (typeof tableViews.$inferInsert)[] = []
675676
for (const definition of definitions) {
676-
const childTableId = generateId()
677+
const childTableId = generateTableId()
677678
const remappedSchema = remapForkTableWorkflowGroups(
678679
definition.schema as TableSchema,
679680
workflowIdMap,

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @vitest-environment node
3+
*/
4+
import { describe, expect, it } from 'vitest'
5+
import { generateTableId } from '@/lib/table/ids'
6+
7+
describe('generateTableId', () => {
8+
it('mints a tbl_ prefix over a dash-stripped UUID', () => {
9+
expect(generateTableId()).toMatch(/^tbl_[0-9a-f]{32}$/)
10+
})
11+
12+
it('mints a distinct id each time', () => {
13+
expect(generateTableId()).not.toBe(generateTableId())
14+
})
15+
})

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { generateId } from '@sim/utils/id'
2+
3+
/**
4+
* Mints a fresh table id: `tbl_` plus a dash-stripped v4 UUID, the shape Tables have carried
5+
* since they shipped and the one the CLI and Copilot describe to users. Every path that
6+
* creates a table definition — the create service and the fork copy — mints through here, so
7+
* a table's id never reveals which path made it.
8+
*/
9+
export function generateTableId(): string {
10+
return `tbl_${generateId().replace(/-/g, '')}`
11+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from '@/lib/table/constants'
1212
export * from '@/lib/table/currency'
1313
export * from '@/lib/table/dates'
1414
export * from '@/lib/table/errors'
15+
export * from '@/lib/table/ids'
1516
export * from '@/lib/table/import'
1617
export * from '@/lib/table/import-data'
1718
export * from '@/lib/table/jobs/service'

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
TABLE_LIMITS,
4444
} from '@/lib/table/constants'
4545
import { appendTableEvent } from '@/lib/table/events'
46+
import { generateTableId } from '@/lib/table/ids'
4647
import {
4748
EMPTY_JOB_FIELDS,
4849
latestJobsForTables,
@@ -565,7 +566,7 @@ export async function createTable(
565566
await assertTableRowTtlEnabled()
566567
}
567568

568-
const tableId = `tbl_${generateId().replace(/-/g, '')}`
569+
const tableId = generateTableId()
569570
const now = new Date()
570571

571572
// Stamp stable ids so the table is id-keyed from its first row write.

0 commit comments

Comments
 (0)