From 97dedc2f10a1d1fcf6bf3b93accc0a9b53fea5d5 Mon Sep 17 00:00:00 2001 From: Guilhem Date: Wed, 11 Nov 2026 17:34:07 +0100 Subject: [PATCH] fix: keep favorites when forking a workspace (#12325) * fix: keep favorites when forking a workspace Co-Authored-By: Claude Opus 5.5 * refactor: check the fork favorites query at compile time Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- ...f01124e63ea722cc8bffa404820193c64b0bc.json | 16 +++++++++ .../windmill-api-workspaces/src/workspaces.rs | 34 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 backend/.sqlx/query-0e321f4b596c6bebd0a6d6beab7f01124e63ea722cc8bffa404820193c64b0bc.json diff --git a/backend/.sqlx/query-0e321f4b596c6bebd0a6d6beab7f01124e63ea722cc8bffa404820193c64b0bc.json b/backend/.sqlx/query-0e321f4b596c6bebd0a6d6beab7f01124e63ea722cc8bffa404820193c64b0bc.json new file mode 100644 index 0000000000..707669e898 --- /dev/null +++ b/backend/.sqlx/query-0e321f4b596c6bebd0a6d6beab7f01124e63ea722cc8bffa404820193c64b0bc.json @@ -0,0 +1,16 @@ +{ + "db_name": "PostgreSQL", + "query": "INSERT INTO favorite (usr, workspace_id, path, favorite_kind)\n SELECT f.usr, $2::varchar, f.path, f.favorite_kind\n FROM favorite f\n WHERE f.workspace_id = $1\n AND (f.usr = $3 OR f.usr IN (SELECT username FROM usr WHERE workspace_id = $2))\n ON CONFLICT DO NOTHING", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Text", + "Varchar", + "Text" + ] + }, + "nullable": [] + }, + "hash": "0e321f4b596c6bebd0a6d6beab7f01124e63ea722cc8bffa404820193c64b0bc" +} diff --git a/backend/windmill-api-workspaces/src/workspaces.rs b/backend/windmill-api-workspaces/src/workspaces.rs index dbdd5bc47f..89d907c3d1 100644 --- a/backend/windmill-api-workspaces/src/workspaces.rs +++ b/backend/windmill-api-workspaces/src/workspaces.rs @@ -7656,6 +7656,14 @@ async fn clone_workspace_data( // drafts would belong to someone the fork holds no membership for. clone_drafts(tx, source_workspace_id, target_workspace_id, &authed.email).await?; + clone_favorites( + tx, + source_workspace_id, + target_workspace_id, + &authed.username, + ) + .await?; + // Clone workspace runnable dependencies and dependency map clone_workspace_runnable_dependencies(tx, source_workspace_id, target_workspace_id).await?; @@ -9317,6 +9325,32 @@ async fn clone_drafts( Ok(()) } +/// Favorites of the fork's members, read after the membership is copied. The forker is named +/// apart: a superadmin can fork a workspace without having a `usr` row in it. +async fn clone_favorites( + tx: &mut Transaction<'_, Postgres>, + source_workspace_id: &str, + target_workspace_id: &str, + forker_username: &str, +) -> Result<()> { + // Deleting a workspace leaves its favorites, so a re-used fork id can already hold some. + sqlx::query!( + "INSERT INTO favorite (usr, workspace_id, path, favorite_kind) + SELECT f.usr, $2::varchar, f.path, f.favorite_kind + FROM favorite f + WHERE f.workspace_id = $1 + AND (f.usr = $3 OR f.usr IN (SELECT username FROM usr WHERE workspace_id = $2)) + ON CONFLICT DO NOTHING", + source_workspace_id, + target_workspace_id, + forker_username, + ) + .execute(&mut **tx) + .await?; + + Ok(()) +} + async fn clone_workspace_runnable_dependencies( tx: &mut Transaction<'_, Postgres>, source_workspace_id: &str,