Skip to content

Commit da088b2

Browse files
committed
fix(workspace-source): warn on a column rename the live version of a deployed workflow still uses
1 parent 92b6812 commit da088b2

3 files changed

Lines changed: 129 additions & 29 deletions

File tree

‎apps/sim/lib/workspace-source/application/deployments.integration.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,61 @@ describe('workspace source: deployments', () => {
10551055
}
10561056
)
10571057

1058+
check(
1059+
'warns when a column rename breaks the live version of a workflow whose draft writes the new name, unless the apply redeploys',
1060+
async () => {
1061+
const workspaceId = await workspaces.create({ name: 'Deployed column user', ownerId })
1062+
const kinds: WorkspaceKind[] = ['table', 'workflow', 'deployment']
1063+
const created = await apply(
1064+
workspaceId,
1065+
{
1066+
'tables/leads.ts': LEADS_TABLE,
1067+
'workflows/ingest/workflow.ts': INGEST_LEADS,
1068+
'deployments/ingest.ts': DRAFT,
1069+
},
1070+
{ kinds }
1071+
)
1072+
expect(created.failed).toBeUndefined()
1073+
const read = await getAll(workspaceId, kinds)
1074+
// The table renames email, and the draft writes the new name; the live version still writes email.
1075+
const renamed: Files = {
1076+
...read.files,
1077+
'tables/leads.ts': LEADS_TABLE.replace(
1078+
'email: column.string(),',
1079+
"email: column.string({ name: 'email_address' }),"
1080+
),
1081+
'workflows/ingest/workflow.ts': INGEST_LEADS.replace(
1082+
'data: { email: start.email }',
1083+
'data: { email_address: start.email }'
1084+
),
1085+
}
1086+
1087+
const { 'deployments/ingest.ts': _deployment, ...undeclaredDeployment } = renamed
1088+
const kept = await apply(workspaceId, undeclaredDeployment, {
1089+
kinds: ['table', 'workflow'],
1090+
base: read.base,
1091+
dryRun: true,
1092+
})
1093+
expect(codes(kept.diagnostics)).toEqual([])
1094+
const warning = kept.diagnostics.find(
1095+
(finding) => finding.code === 'W_TABLE_COLUMN_RENAMED_IN_USE'
1096+
)
1097+
expect(warning).toMatchObject({ kind: 'table', key: 'leads' })
1098+
expect(warning?.message).toContain('live version')
1099+
1100+
const redeployed = await apply(workspaceId, renamed, {
1101+
kinds,
1102+
base: read.base,
1103+
dryRun: true,
1104+
})
1105+
expect(codes(redeployed.diagnostics)).toEqual([])
1106+
expect(actions(redeployed.plan)).toContain('update deployment:ingest')
1107+
expect(codes(redeployed.diagnostics, 'warning')).not.toContain(
1108+
'W_TABLE_COLUMN_RENAMED_IN_USE'
1109+
)
1110+
}
1111+
)
1112+
10581113
check('refuses a deployment of a workflow the workspace does not hold', async () => {
10591114
const workspaceId = await workspaces.create({ name: 'Deploy nothing', ownerId })
10601115
const refused = await refusal(apply(workspaceId, { 'deployments/missing.ts': DRAFT }))

‎apps/sim/lib/workspace-source/application/in-use.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export function inUseUncheckedFinding(change: Pick<PlannedChange, 'kind' | 'key'
136136
* The workflows whose live version this plan replaces or stops before its deletes run: a
137137
* deployment it writes that publishes the draft, or one it undeploys. Held changes write nothing.
138138
*/
139-
function liveVersionsReplaced(changes: readonly PlannedChange[]): Set<string> {
139+
export function liveVersionsReplaced(changes: readonly PlannedChange[]): Set<string> {
140140
const replaced = new Set<string>()
141141
for (const change of changes) {
142142
if (change.kind !== 'deployment' || change.held) continue

‎apps/sim/lib/workspace-source/application/kinds/table/renames.ts‎

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ import { SOURCE_ENTRY_FILE } from '@sim/workflow-code/limits'
1111
import { workflowFolder } from '@sim/workspace-code/kinds/workflow'
1212
import { readWorkflowSource } from '@/lib/workflows/application/read-workflow-source'
1313
import { collectTableBlockFieldIssues } from '@/lib/workflows/editing/lint'
14-
import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
14+
import {
15+
loadDeployedWorkflowState,
16+
loadWorkflowFromNormalizedTables,
17+
NoActiveDeploymentError,
18+
} from '@/lib/workflows/persistence/utils'
1519
import { finding, where } from '@/lib/workspace-source/application/findings'
16-
import { workflowsUsing } from '@/lib/workspace-source/application/in-use'
20+
import { liveVersionsReplaced, workflowsUsing } from '@/lib/workspace-source/application/in-use'
1721
import type { TablePlan } from '@/lib/workspace-source/application/kinds/table/adapter'
1822
import type { PlanContext, PlannedChange } from '@/lib/workspace-source/application/types'
1923

@@ -67,10 +71,22 @@ function locateWrite(
6771
return { line: declaration + 1, column: 1 }
6872
}
6973

74+
/** The live version of `workflowId`'s deployment, or `null` when it has none. */
75+
async function liveBlocks(workflowId: string) {
76+
try {
77+
return (await loadDeployedWorkflowState(workflowId)).blocks
78+
} catch (error) {
79+
if (error instanceof NoActiveDeploymentError) return null
80+
throw error
81+
}
82+
}
83+
7084
/**
7185
* Warns `W_TABLE_COLUMN_RENAMED_IN_USE` on each table change that renames a column some stored
7286
* workflow's Table block still writes, filters or sorts by the old name: one finding per block,
73-
* naming the workflow and the block, pinned to the declared workflow's file and line.
87+
* naming the workflow and the block, pinned to the declared workflow's file and line. A deployed
88+
* workflow's live version runs apart from its draft, so it is checked too (an evaluated draft
89+
* included), unless the apply publishes the draft or undeploys it.
7490
*/
7591
export async function checkColumnRenamesInUse(
7692
changes: readonly PlannedChange[],
@@ -89,45 +105,74 @@ export async function checkColumnRenamesInUse(
89105
if (change.kind !== 'workflow' || change.storedId === undefined) continue
90106
declared.set(change.storedId, { key: change.key, evaluated: change.refs !== undefined })
91107
}
108+
const replacedLive = liveVersionsReplaced(changes)
92109
for (const table of tables) {
110+
const columns = new Map([
111+
[table.tableId, { name: table.tableName, columnNames: table.columnNames }],
112+
])
93113
for (const workflow of using.get(table.tableId) ?? []) {
94-
if (!workflow.draft) continue
95114
const folder = declared.get(workflow.id)
96115
if (
97-
folder?.evaluated ||
98116
changes.some(
99117
(c) => c.kind === 'workflow' && c.storedId === workflow.id && c.action === 'delete'
100118
)
101-
)
119+
) {
102120
continue
103-
const loaded = await loadWorkflowFromNormalizedTables(workflow.id)
104-
if (!loaded) continue
105-
const issues = collectTableBlockFieldIssues(
106-
loaded.blocks,
107-
new Map([[table.tableId, { name: table.tableName, columnNames: table.columnNames }]])
108-
).filter((issue) => table.renamed.has(issue.field))
109-
if (issues.length === 0) continue
110-
const text = folder
111-
? (
112-
await readWorkflowSource.execute({
113-
principal: ctx.principal,
114-
input: { workflowId: workflow.id },
115-
})
116-
).files[SOURCE_ENTRY_FILE]
117-
: undefined
118-
for (const issue of issues) {
121+
}
122+
/** `blockId:field` of each finding already made from the draft. */
123+
const reported = new Set<string>()
124+
if (workflow.draft && !folder?.evaluated) {
125+
const loaded = await loadWorkflowFromNormalizedTables(workflow.id)
126+
const issues = loaded
127+
? collectTableBlockFieldIssues(loaded.blocks, columns).filter((issue) =>
128+
table.renamed.has(issue.field)
129+
)
130+
: []
131+
const text =
132+
folder && issues.length > 0
133+
? (
134+
await readWorkflowSource.execute({
135+
principal: ctx.principal,
136+
input: { workflowId: workflow.id },
137+
})
138+
).files[SOURCE_ENTRY_FILE]
139+
: undefined
140+
for (const issue of issues) {
141+
reported.add(`${issue.blockId}:${issue.field}`)
142+
const renamedTo = table.renamed.get(issue.field)
143+
const blockName = issue.blockName ?? issue.blockId
144+
const at = text !== undefined ? locateWrite(text, blockName, issue.field) : undefined
145+
table.change.diagnostics.push(
146+
finding(
147+
'W_TABLE_COLUMN_RENAMED_IN_USE',
148+
`${where('table', table.change.key)} renames column "${issue.field}" to "${renamedTo}", but block "${blockName}" of workflow "${workflow.name}" still ${issue.rowWrite ? 'writes' : 'reads'} "${issue.field}"; the run drops it.`,
149+
{
150+
hint: `Write "${renamedTo}" in that block${folder ? ` (${workflowFolder(folder.key)}/${SOURCE_ENTRY_FILE})` : ''}, in this apply or right after it.`,
151+
...(folder
152+
? { file: `${workflowFolder(folder.key)}/${SOURCE_ENTRY_FILE}`, ...(at ?? {}) }
153+
: {}),
154+
kind: 'table',
155+
key: table.change.key,
156+
}
157+
)
158+
)
159+
}
160+
}
161+
if (!workflow.live || replacedLive.has(workflow.id)) continue
162+
const live = await liveBlocks(workflow.id)
163+
if (!live) continue
164+
for (const issue of collectTableBlockFieldIssues(live, columns)) {
165+
if (!table.renamed.has(issue.field) || reported.has(`${issue.blockId}:${issue.field}`)) {
166+
continue
167+
}
119168
const renamedTo = table.renamed.get(issue.field)
120169
const blockName = issue.blockName ?? issue.blockId
121-
const at = text !== undefined ? locateWrite(text, blockName, issue.field) : undefined
122170
table.change.diagnostics.push(
123171
finding(
124172
'W_TABLE_COLUMN_RENAMED_IN_USE',
125-
`${where('table', table.change.key)} renames column "${issue.field}" to "${renamedTo}", but block "${blockName}" of workflow "${workflow.name}" still ${issue.rowWrite ? 'writes' : 'reads'} "${issue.field}"; the run drops it.`,
173+
`${where('table', table.change.key)} renames column "${issue.field}" to "${renamedTo}", but block "${blockName}" of workflow "${workflow.name}" (live version) still ${issue.rowWrite ? 'writes' : 'reads'} "${issue.field}"; its runs drop it until the workflow is deployed again.`,
126174
{
127-
hint: `Write "${renamedTo}" in that block${folder ? ` (${workflowFolder(folder.key)}/${SOURCE_ENTRY_FILE})` : ''}, in this apply or right after it.`,
128-
...(folder
129-
? { file: `${workflowFolder(folder.key)}/${SOURCE_ENTRY_FILE}`, ...(at ?? {}) }
130-
: {}),
175+
hint: `Write "${renamedTo}" in that block and deploy the workflow (\`deployments/<key>.ts\` with live: 'draft'), in this apply or right after it.`,
131176
kind: 'table',
132177
key: table.change.key,
133178
}

0 commit comments

Comments
 (0)