@@ -11,9 +11,13 @@ import { SOURCE_ENTRY_FILE } from '@sim/workflow-code/limits'
1111import { workflowFolder } from '@sim/workspace-code/kinds/workflow'
1212import { readWorkflowSource } from '@/lib/workflows/application/read-workflow-source'
1313import { 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'
1519import { 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'
1721import type { TablePlan } from '@/lib/workspace-source/application/kinds/table/adapter'
1822import 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 */
7591export 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