Skip to content

Commit f0f96e7

Browse files
authored
fix: allow workflows to be empty or undefined (#9039)
### What? - Makes `jobs.workflows` optional - Dynamically include the `workflowSlugs` select field in the jobs collection as needed ### Why? When configuring jobs, it should be possible to define `job` with just some simple tasks and not be forced to define workflows. ### How? Workflows type was made optional and optional chaining is added where needed. The workflowSlugs field is added to the jobs collection if workflows are defined. Fixes # When using postgres, the workflowSlugs being an empty enum cause an error when drizzle fails to detect the enum already exists. This results in the error `"enum_payload_jobs_workflow_slug" already exists`. Drizzle tries to make the enum as: `enum_payload_jobs_workflow_slug as enum();` and the check for existing enums only works when it has values.
1 parent 0165ab8 commit f0f96e7

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

packages/payload/src/queues/config/jobsCollection.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CollectionConfig } from '../../collections/config/types.js'
22
import type { Config } from '../../config/types.js'
3+
import type { Field } from '../../fields/config/types.js'
34

45
import { runJobsEndpoint } from '../restEndpointRun.js'
56
import { getJobTaskStatus } from '../utilities/getJobTaskStatus.js'
@@ -14,7 +15,7 @@ export const getDefaultJobsCollection: (config: Config) => CollectionConfig | nu
1415

1516
const queueNames: Set<string> = new Set(['default'])
1617

17-
config.jobs.workflows.forEach((workflow) => {
18+
config.jobs?.workflows.forEach((workflow) => {
1819
workflowSlugs.add(workflow.slug)
1920

2021
if (workflow.queue) {
@@ -141,16 +142,20 @@ export const getDefaultJobsCollection: (config: Config) => CollectionConfig | nu
141142
},
142143
],
143144
},
144-
{
145-
name: 'workflowSlug',
146-
type: 'select',
147-
admin: {
148-
position: 'sidebar',
149-
},
150-
index: true,
151-
options: [...workflowSlugs],
152-
required: false,
153-
},
145+
// only include the workflowSlugs field if workflows exist
146+
...((workflowSlugs.size > 0
147+
? [
148+
{
149+
name: 'workflowSlug',
150+
type: 'select',
151+
admin: {
152+
position: 'sidebar',
153+
},
154+
index: true,
155+
options: [...workflowSlugs],
156+
},
157+
]
158+
: []) as Field[]),
154159
{
155160
name: 'taskSlug',
156161
type: 'select',

packages/payload/src/queues/config/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ export type JobsConfig = {
4141
/**
4242
* Define all the workflows here. Workflows orchestrate the flow of multiple tasks.
4343
*/
44-
workflows: WorkflowConfig<any>[]
44+
workflows?: WorkflowConfig<any>[]
4545
}

packages/payload/src/queues/operations/runJobs/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export const runJobs = async ({
138138
const jobReq = isolateObjectProperty(req, 'transactionID')
139139

140140
const workflowConfig: WorkflowConfig<WorkflowTypes> = job.workflowSlug
141-
? req.payload.config.jobs.workflows.find(({ slug }) => slug === job.workflowSlug)
141+
? req.payload.config.jobs?.workflows.find(({ slug }) => slug === job.workflowSlug)
142142
: {
143143
slug: 'singleTask',
144144
handler: async ({ job, tasks }) => {

0 commit comments

Comments
 (0)