From 66df58af47b41158d2a5108ec8f775bd4754342c Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Mon, 5 Sep 2022 09:55:04 +0200 Subject: [PATCH] feat: is_trigger is just a type tag, soon to include failure and command (#523) * script kind * all * init code flow * kind: trigger * kind: trigger --- .../20220904123027_script_kind.down.sql | 1 + .../20220904123027_script_kind.up.sql | 5 + backend/openapi.yaml | 27 ++-- backend/sqlx-data.json | 81 ++++++------ backend/src/scripts.rs | 25 ++-- frontend/src/lib/components/ItemPicker.svelte | 4 +- frontend/src/lib/components/ModuleStep.svelte | 3 +- .../src/lib/components/ScriptBuilder.svelte | 119 +++++++++++------- .../src/lib/components/ScriptPicker.svelte | 6 +- frontend/src/lib/components/Tooltip.svelte | 1 - .../lib/components/flows/FlowInputs.svelte | 21 ++-- .../lib/components/flows/flowStateUtils.ts | 9 +- .../flows/pickers/PickHubScript.svelte | 7 +- .../flows/pickers/PickScript.svelte | 19 +-- .../lib/components/landing/ScriptBox.svelte | 4 +- frontend/src/lib/script_helpers.ts | 31 +++-- frontend/src/lib/stores.ts | 2 +- frontend/src/lib/utils.ts | 8 +- frontend/src/routes/schedule/add.svelte | 9 +- frontend/src/routes/scripts.svelte | 6 +- frontend/src/routes/scripts/add.svelte | 3 +- .../src/routes/scripts/get/[...hash].svelte | 6 +- frontend/src/routes/workspace_settings.svelte | 10 +- 23 files changed, 243 insertions(+), 164 deletions(-) create mode 100644 backend/migrations/20220904123027_script_kind.down.sql create mode 100644 backend/migrations/20220904123027_script_kind.up.sql diff --git a/backend/migrations/20220904123027_script_kind.down.sql b/backend/migrations/20220904123027_script_kind.down.sql new file mode 100644 index 0000000000..d2f607c5b8 --- /dev/null +++ b/backend/migrations/20220904123027_script_kind.down.sql @@ -0,0 +1 @@ +-- Add down migration script here diff --git a/backend/migrations/20220904123027_script_kind.up.sql b/backend/migrations/20220904123027_script_kind.up.sql new file mode 100644 index 0000000000..aeb52483a8 --- /dev/null +++ b/backend/migrations/20220904123027_script_kind.up.sql @@ -0,0 +1,5 @@ +-- Add up migration script here +CREATE TYPE SCRIPT_KIND AS ENUM ('script', 'trigger', 'failure', 'command'); + +ALTER TABLE script ADD COLUMN kind SCRIPT_KIND NOT NULL DEFAULT 'script'; +ALTER TABLE script DROP COLUMN is_trigger; \ No newline at end of file diff --git a/backend/openapi.yaml b/backend/openapi.yaml index f2a39ce750..b6d05097fa 100644 --- a/backend/openapi.yaml +++ b/backend/openapi.yaml @@ -1532,8 +1532,9 @@ paths: type: string approved: type: boolean - is_trigger: - type: boolean + kind: + type: string + enum: [script, failure, trigger, command] votes: type: number views: @@ -1544,7 +1545,7 @@ paths: - summary - app - approved - - is_trigger + - kind - views - votes @@ -1688,15 +1689,13 @@ paths: in: query schema: type: boolean - - name: is_trigger + - name: kind description: | (default regardless) - if true show only the trigger scripts - if false show only the non trigger scripts - if not defined, show all regardless of if the script is a trigger script + script kind in: query schema: - type: boolean + type: string responses: "200": description: All available scripts @@ -1744,8 +1743,9 @@ paths: language: type: string enum: [python3, deno] - is_trigger: - type: boolean + kind: + type: string + enum: [script, failure, trigger, command] required: - path - summary @@ -3241,8 +3241,9 @@ components: language: type: string enum: [python3, deno] - is_trigger: - type: boolean + kind: + type: string + enum: [script, failure, trigger, command] required: - hash - path @@ -3255,7 +3256,7 @@ components: - is_template - extra_perms - language - - is_trigger + - kind ScriptArgs: type: object diff --git a/backend/sqlx-data.json b/backend/sqlx-data.json index 5d2b340f67..295d443d11 100644 --- a/backend/sqlx-data.json +++ b/backend/sqlx-data.json @@ -1215,6 +1215,52 @@ "nullable": [] } }, + "5fee34ce48f01bb55707227548ea4d1e77ba75abfae412a902b1d8d6a1963725": { + "query": "INSERT INTO script (workspace_id, hash, path, parent_hashes, summary, description, content, created_by, schema, is_template, extra_perms, lock, language, kind) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::text::json, $10, $11, $12, $13, $14)", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Varchar", + "Int8", + "Varchar", + "Int8Array", + "Text", + "Text", + "Text", + "Varchar", + "Text", + "Bool", + "Jsonb", + "Text", + { + "Custom": { + "name": "script_lang", + "kind": { + "Enum": [ + "python3", + "deno" + ] + } + } + }, + { + "Custom": { + "name": "script_kind", + "kind": { + "Enum": [ + "script", + "trigger", + "failure" + ] + } + } + } + ] + }, + "nullable": [] + } + }, "6199e8be5cb13db71108e555ea20f0b76dc38476670f9fc0667b057d2766d42e": { "query": "SELECT set_config('session.groups', $1, true)", "describe": { @@ -2368,41 +2414,6 @@ ] } }, - "b49868120aa48f9163ffb0262804c0528723c858d20d7e556750fe712e3d5123": { - "query": "INSERT INTO script (workspace_id, hash, path, parent_hashes, summary, description, content, created_by, schema, is_template, extra_perms, lock, language, is_trigger) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::text::json, $10, $11, $12, $13, $14)", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Varchar", - "Int8", - "Varchar", - "Int8Array", - "Text", - "Text", - "Text", - "Varchar", - "Text", - "Bool", - "Jsonb", - "Text", - { - "Custom": { - "name": "script_lang", - "kind": { - "Enum": [ - "python3", - "deno" - ] - } - } - }, - "Bool" - ] - }, - "nullable": [] - } - }, "b7dd791cd69748ef51b7520f505c0c8bb1b4014a273476eddfecf1ab658a18b4": { "query": "select hash from script where path = $1 AND (workspace_id = $2 OR workspace_id = 'starter') AND\n created_at = (SELECT max(created_at) FROM script WHERE path = $1 AND (workspace_id = $2 OR workspace_id = 'starter')) AND\n deleted = false", "describe": { diff --git a/backend/src/scripts.rs b/backend/src/scripts.rs index 6e0029a7f9..9502966498 100644 --- a/backend/src/scripts.rs +++ b/backend/src/scripts.rs @@ -124,6 +124,15 @@ impl Serialize for ScriptHashes { } } +#[derive(sqlx::Type, Serialize, Deserialize, Debug, Hash)] +#[sqlx(type_name = "SCRIPT_KIND", rename_all = "lowercase")] +#[serde(rename_all = "lowercase")] +pub enum ScriptKind { + Trigger, + Failure, + Script, +} + #[derive(FromRow, Serialize)] pub struct Script { pub workspace_id: String, @@ -143,7 +152,7 @@ pub struct Script { pub lock: Option, pub lock_error_logs: Option, pub language: ScriptLang, - pub is_trigger: bool, + pub kind: ScriptKind, } #[derive(Serialize, Deserialize, sqlx::Type, Debug)] @@ -170,7 +179,7 @@ pub struct NewScript { pub is_template: Option, pub lock: Option>, pub language: ScriptLang, - pub is_trigger: Option, + pub kind: Option, } #[derive(Deserialize)] @@ -185,7 +194,7 @@ pub struct ListScriptQuery { pub order_by: Option, pub order_desc: Option, pub is_template: Option, - pub is_trigger: Option, + pub kind: Option, } async fn list_scripts( @@ -216,7 +225,7 @@ async fn list_scripts( "null as lock", "CASE WHEN lock_error_logs IS NOT NULL THEN 'error' ELSE null END as lock_error_logs", "language", - "is_trigger", + "kind", ]) .order_by("created_at", lq.order_desc.unwrap_or(true)) .and_where("workspace_id = ? OR workspace_id = 'starter'".bind(&w_id)) @@ -254,8 +263,8 @@ async fn list_scripts( if let Some(it) = &lq.is_template { sqlb.and_where_eq("is_template", it); } - if let Some(it) = &lq.is_trigger { - sqlb.and_where_eq("is_trigger", it); + if let Some(k) = &lq.kind { + sqlb.and_where_eq("kind", "?".bind(&k.to_lowercase())); } let sql = sqlb.sql().map_err(|e| Error::InternalErr(e.to_string()))?; @@ -407,7 +416,7 @@ async fn create_script( //::text::json is to ensure we use serde_json with preserve order sqlx::query!( "INSERT INTO script (workspace_id, hash, path, parent_hashes, summary, description, \ - content, created_by, schema, is_template, extra_perms, lock, language, is_trigger) \ + content, created_by, schema, is_template, extra_perms, lock, language, kind) \ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::text::json, $10, $11, $12, $13, $14)", &w_id, &hash.0, @@ -422,7 +431,7 @@ async fn create_script( extra_perms, lock, ns.language: ScriptLang, - ns.is_trigger.unwrap_or(false), + ns.kind.unwrap_or(ScriptKind::Script): ScriptKind, ) .execute(&mut tx) .await?; diff --git a/frontend/src/lib/components/ItemPicker.svelte b/frontend/src/lib/components/ItemPicker.svelte index 868b8712c1..c637e88289 100644 --- a/frontend/src/lib/components/ItemPicker.svelte +++ b/frontend/src/lib/components/ItemPicker.svelte @@ -36,7 +36,7 @@ -
search a {itemName}
+
Search a {itemName}
@@ -57,7 +57,7 @@ {/if} - {obj[extraField]}{obj[extraField]}{obj['path'] ?? ''}

diff --git a/frontend/src/lib/components/ModuleStep.svelte b/frontend/src/lib/components/ModuleStep.svelte index 00fe322d73..ea6637ba3f 100644 --- a/frontend/src/lib/components/ModuleStep.svelte +++ b/frontend/src/lib/components/ModuleStep.svelte @@ -93,7 +93,8 @@ on:new={(e) => apply(createInlineScriptModule, { language: e.detail.language, - type: e.detail.type + kind: e.detail.kind, + subkind: e.detail.subkind })} on:loop={() => applyCreateLoop()} /> diff --git a/frontend/src/lib/components/ScriptBuilder.svelte b/frontend/src/lib/components/ScriptBuilder.svelte index e399ea3ebe..450413a81d 100644 --- a/frontend/src/lib/components/ScriptBuilder.svelte +++ b/frontend/src/lib/components/ScriptBuilder.svelte @@ -1,15 +1,10 @@ @@ -34,13 +27,11 @@ }} itemName={'Script'} extraField="summary" - loadItems={async () => { - return items - }} + {loadItems} /> itemPicker.openModal()} diff --git a/frontend/src/lib/components/landing/ScriptBox.svelte b/frontend/src/lib/components/landing/ScriptBox.svelte index ee7908b1bf..561027f89d 100644 --- a/frontend/src/lib/components/landing/ScriptBox.svelte +++ b/frontend/src/lib/components/landing/ScriptBox.svelte @@ -24,9 +24,9 @@ {String(script.language).toUpperCase()} - {#if script.is_trigger} + {#if script.kind != 'script'} - TRIGGER + {script.kind} {/if}
diff --git a/frontend/src/lib/script_helpers.ts b/frontend/src/lib/script_helpers.ts index 48573bba36..c45a7a8304 100644 --- a/frontend/src/lib/script_helpers.ts +++ b/frontend/src/lib/script_helpers.ts @@ -1,3 +1,5 @@ +import type { Script } from "./gen" + export const PYTHON_INIT_CODE = `import os import wmill from datetime import datetime @@ -98,19 +100,34 @@ export async function main() { } ` -export function initialCode(language: 'deno' | 'python3', type: 'trigger' | 'flow' | 'pgsql' | undefined): string { +const ALL_INITIAL_CODE = [PYTHON_INIT_CODE, DENO_INIT_CODE, POSTGRES_INIT_CODE, DENO_INIT_CODE_TRIGGER, DENO_INIT_CODE_CLEAR, PYTHON_INIT_CODE_CLEAR] + +export function isInitialCode(content: string): boolean { + for (const code of ALL_INITIAL_CODE) { + if (content === code) { + return true + } + } + return false +} + +export function initialCode(language: 'deno' | 'python3', kind: Script.kind, subkind: 'pgsql' | 'flow' | 'script' | undefined): string { if (language === 'deno') { - if (type === 'trigger') { + if (kind === 'trigger') { return DENO_INIT_CODE_TRIGGER - } else if (type === 'flow') { - return DENO_INIT_CODE_CLEAR - } else if (type === 'pgsql') { - return POSTGRES_INIT_CODE + } else if (kind === 'script') { + if (subkind === 'flow') { + return DENO_INIT_CODE_CLEAR + } else if (subkind === 'pgsql') { + return POSTGRES_INIT_CODE + } else { + return DENO_INIT_CODE + } } else { return DENO_INIT_CODE } } else { - if (type === 'flow') { + if (subkind === 'flow') { return PYTHON_INIT_CODE_CLEAR } else { return PYTHON_INIT_CODE diff --git a/frontend/src/lib/stores.ts b/frontend/src/lib/stores.ts index cf77cd3f34..3c9e14b510 100644 --- a/frontend/src/lib/stores.ts +++ b/frontend/src/lib/stores.ts @@ -27,7 +27,7 @@ export const hubScripts = writable< path: string summary: string approved: boolean - is_trigger: boolean + kind: string app: string }> | undefined diff --git a/frontend/src/lib/utils.ts b/frontend/src/lib/utils.ts index c228745677..24b7c3e024 100644 --- a/frontend/src/lib/utils.ts +++ b/frontend/src/lib/utils.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { goto } from '$app/navigation' -import { FlowService, ScriptService, type Flow, type FlowModule, type User } from '$lib/gen' +import { FlowService, Script, ScriptService, type Flow, type FlowModule, type User } from '$lib/gen' import { toast } from '@zerodevx/svelte-toast' import { get } from 'svelte/store' import type { Schema } from './common' @@ -502,7 +502,7 @@ export async function loadHubScripts() { path: `hub/${x.id}/${x.app}/${x.summary.toLowerCase().replaceAll(/\s+/g, '_')}`, summary: `${x.summary} (${x.app}) ${x.views} uses`, approved: x.approved, - is_trigger: x.is_trigger, + kind: x.kind, app: x.app, views: x.views, votes: x.votes, @@ -545,14 +545,14 @@ export function scriptToHubUrl( content: string, summary: string, description: string, - trigger: boolean + kind: Script.kind ): URL { const url = new URL('https://hub.windmill.dev/scripts/add') url.searchParams.append('content', content) url.searchParams.append('summary', summary) url.searchParams.append('description', description) - url.searchParams.append('trigger', trigger.toString()) + url.searchParams.append('kind', kind) return url } diff --git a/frontend/src/routes/schedule/add.svelte b/frontend/src/routes/schedule/add.svelte index d82f301602..87bb5616c6 100644 --- a/frontend/src/routes/schedule/add.svelte +++ b/frontend/src/routes/schedule/add.svelte @@ -9,7 +9,7 @@