From ba0cfb199c110c56b3f842c9d0d0ee32442ea65c Mon Sep 17 00:00:00 2001 From: dieriba Date: Mon, 10 Mar 2025 12:38:11 +0100 Subject: [PATCH] use-exists-function-for-folder-and-improve-error-handling (#5464) * update: removing try and catch for checkExistalready fn and use exist function folder instead of get folder for folder kind * update .sqlx --- ...05d394a7cbcf0038c72a78add5c7b02ef5927.json | 2 +- backend/windmill-api/openapi.yaml | 17 +++ backend/windmill-api/src/folders.rs | 17 +++ .../src/lib/components/DeployWorkspace.svelte | 132 ++++++++++-------- 4 files changed, 108 insertions(+), 60 deletions(-) diff --git a/backend/.sqlx/query-ddf2eccb78a310ed00c7d8b9c3f05d394a7cbcf0038c72a78add5c7b02ef5927.json b/backend/.sqlx/query-ddf2eccb78a310ed00c7d8b9c3f05d394a7cbcf0038c72a78add5c7b02ef5927.json index 5bfff47576..c2dfed73a2 100644 --- a/backend/.sqlx/query-ddf2eccb78a310ed00c7d8b9c3f05d394a7cbcf0038c72a78add5c7b02ef5927.json +++ b/backend/.sqlx/query-ddf2eccb78a310ed00c7d8b9c3f05d394a7cbcf0038c72a78add5c7b02ef5927.json @@ -15,7 +15,7 @@ ] }, "nullable": [ - true + null ] }, "hash": "ddf2eccb78a310ed00c7d8b9c3f05d394a7cbcf0038c72a78add5c7b02ef5927" diff --git a/backend/windmill-api/openapi.yaml b/backend/windmill-api/openapi.yaml index 33bd3fbf34..ad59bda610 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -10033,6 +10033,23 @@ paths: application/json: schema: $ref: "#/components/schemas/Folder" + + /w/{workspace}/folders/exists/{name}: + get: + summary: exists folder + operationId: existsFolder + tags: + - folder + parameters: + - $ref: "#/components/parameters/WorkspaceId" + - $ref: "#/components/parameters/Name" + responses: + "200": + description: folder exists + content: + application/json: + schema: + type: boolean /w/{workspace}/folders/getusage/{name}: get: diff --git a/backend/windmill-api/src/folders.rs b/backend/windmill-api/src/folders.rs index cab039c64f..5047a96cad 100644 --- a/backend/windmill-api/src/folders.rs +++ b/backend/windmill-api/src/folders.rs @@ -42,6 +42,7 @@ pub fn workspaced_service() -> Router { .route("/listnames", get(list_foldernames)) .route("/create", post(create_folder)) .route("/get/:name", get(get_folder)) + .route("/exists/:name", get(exists_folder)) .route("/update/:name", post(update_folder)) .route("/getusage/:name", get(get_folder_usage)) .route("/delete/:name", delete(delete_folder)) @@ -426,6 +427,22 @@ async fn get_folder( Ok(Json(folder)) } +async fn exists_folder( + Extension(db): Extension, + Path((w_id, name)): Path<(String, String)>, +) -> JsonResult { + let exists = sqlx::query_scalar!( + "SELECT EXISTS(SELECT 1 FROM folder WHERE name = $1 AND workspace_id = $2)", + name, + w_id + ) + .fetch_one(&db) + .await? + .unwrap_or(false); + + Ok(Json(exists)) +} + #[derive(Serialize)] struct FolderUsage { pub scripts: i64, diff --git a/frontend/src/lib/components/DeployWorkspace.svelte b/frontend/src/lib/components/DeployWorkspace.svelte index 44f4f8a0b6..a51446a93d 100644 --- a/frontend/src/lib/components/DeployWorkspace.svelte +++ b/frontend/src/lib/components/DeployWorkspace.svelte @@ -30,6 +30,7 @@ type AdditionalInformation, type Kind } from '$lib/utils_deployable' + import type { TriggerKind } from './triggers' const dispatch = createEventDispatcher() @@ -174,67 +175,80 @@ } async function checkAlreadyExists(kind: Kind, path: string): Promise { - let exists = true - try { - if (kind == 'flow') { - exists = await FlowService.existsFlowByPath({ - workspace: workspaceToDeployTo!, - path: path - }) - } else if (kind == 'script') { - exists = await ScriptService.existsScriptByPath({ - workspace: workspaceToDeployTo!, - path: path - }) - } else if (kind == 'app') { - exists = await AppService.existsApp({ - workspace: workspaceToDeployTo!, - path: path - }) - } else if (kind == 'raw_app') { - exists = await RawAppService.existsRawApp({ - workspace: workspaceToDeployTo!, - path: path - }) - } else if (kind == 'variable') { - exists = await VariableService.existsVariable({ - workspace: workspaceToDeployTo!, - path: path - }) - } else if (kind == 'resource') { - exists = await ResourceService.existsResource({ - workspace: workspaceToDeployTo!, - path: path - }) - } else if (kind == 'schedule') { - exists = await ScheduleService.existsSchedule({ - workspace: workspaceToDeployTo!, - path: path - }) - } else if (kind == 'resource_type') { - exists = await ResourceService.existsResourceType({ - workspace: workspaceToDeployTo!, - path: path - }) - } else if (kind == 'folder') { - await FolderService.getFolder({ - workspace: workspaceToDeployTo!, - name: path - }) - } else if (kind === 'trigger') { - if (additionalInformation?.triggers) { - exists = await existsTrigger( - { workspace: workspaceToDeployTo!, path }, - additionalInformation.triggers.kind - ) - } else { - throw new Error('Missing triggers kind') - } + let exists: boolean + if (kind == 'flow') { + exists = await FlowService.existsFlowByPath({ + workspace: workspaceToDeployTo!, + path: path + }) + } else if (kind == 'script') { + exists = await ScriptService.existsScriptByPath({ + workspace: workspaceToDeployTo!, + path: path + }) + } else if (kind == 'app') { + exists = await AppService.existsApp({ + workspace: workspaceToDeployTo!, + path: path + }) + } else if (kind == 'raw_app') { + exists = await RawAppService.existsRawApp({ + workspace: workspaceToDeployTo!, + path: path + }) + } else if (kind == 'variable') { + exists = await VariableService.existsVariable({ + workspace: workspaceToDeployTo!, + path: path + }) + } else if (kind == 'resource') { + exists = await ResourceService.existsResource({ + workspace: workspaceToDeployTo!, + path: path + }) + } else if (kind == 'schedule') { + exists = await ScheduleService.existsSchedule({ + workspace: workspaceToDeployTo!, + path: path + }) + } else if (kind == 'resource_type') { + exists = await ResourceService.existsResourceType({ + workspace: workspaceToDeployTo!, + path: path + }) + } else if (kind == 'folder') { + exists = await FolderService.existsFolder({ + workspace: workspaceToDeployTo!, + name: path + }) + } else if (kind === 'trigger') { + const triggersKind: TriggerKind[] = [ + 'kafka', + 'mqtt', + 'nats', + 'postgres', + 'routes', + 'schedules', + 'sqs', + 'websockets' + ] + if ( + additionalInformation?.triggers && + triggersKind.includes(additionalInformation.triggers.kind) + ) { + exists = await existsTrigger( + { workspace: workspaceToDeployTo!, path }, + additionalInformation.triggers.kind + ) } else { - throw new Error(`Unknown kind ${kind}`) + throw new Error( + `Unexpected triggers kind, expected one of: '${triggersKind.join(', ')}' got: ${ + additionalInformation?.triggers?.kind + }` + ) } - } catch (error) { - exists = false + } else { + throw new Error(`Unknown kind ${kind}`) } return exists }