()
| 215 | const BATCH_SIZE = 50 |
| 216 | |
| 217 | async function migrateWorkflows() { |
| 218 | console.log('Starting deployment version migration...') |
| 219 | console.log(`Mode: ${DRY_RUN ? 'DRY RUN' : 'LIVE'}`) |
| 220 | console.log(`Batch size: ${BATCH_SIZE}`) |
| 221 | console.log('---') |
| 222 | |
| 223 | try { |
| 224 | const workflows = await db |
| 225 | .select({ |
| 226 | id: workflow.id, |
| 227 | name: workflow.name, |
| 228 | isDeployed: workflow.isDeployed, |
| 229 | deployedState: workflow.deployedState, |
| 230 | deployedAt: workflow.deployedAt, |
| 231 | userId: workflow.userId, |
| 232 | }) |
| 233 | .from(workflow) |
| 234 | |
| 235 | console.log(`Found ${workflows.length} workflows to process`) |
| 236 | |
| 237 | const existingVersions = await db |
| 238 | .select({ workflowId: workflowDeploymentVersion.workflowId }) |
| 239 | .from(workflowDeploymentVersion) |
| 240 | |
| 241 | const existingWorkflowIds = new Set(existingVersions.map((v) => v.workflowId as string)) |
| 242 | console.log(`${existingWorkflowIds.size} workflows already have deployment versions`) |
| 243 | |
| 244 | let successCount = 0 |
| 245 | let skipCount = 0 |
| 246 | let errorCount = 0 |
| 247 | |
| 248 | for (let i = 0; i < workflows.length; i += BATCH_SIZE) { |
| 249 | const batch = workflows.slice(i, i + BATCH_SIZE) |
| 250 | console.log( |
| 251 | `\nProcessing batch ${Math.floor(i / BATCH_SIZE) + 1} (workflows ${i + 1}-${Math.min(i + BATCH_SIZE, workflows.length)})` |
| 252 | ) |
| 253 | |
| 254 | const deploymentVersions: Array<{ |
| 255 | id: string |
| 256 | workflowId: string |
| 257 | version: number |
| 258 | state: WorkflowState |
| 259 | createdAt: Date |
| 260 | createdBy: string |
| 261 | isActive: boolean |
| 262 | }> = [] |
| 263 | |
| 264 | for (const wf of batch as any[]) { |
| 265 | if (existingWorkflowIds.has(wf.id)) { |
| 266 | console.log(` [SKIP] ${wf.id} (${wf.name}) - already has deployment version`) |
| 267 | skipCount++ |
| 268 | continue |
| 269 | } |
| 270 | |
| 271 | let state: WorkflowState | null = null |
| 272 | |
| 273 | if (wf.deployedState) { |
| 274 | state = wf.deployedState as WorkflowState |
no test coverage detected