fix: keep favorites when forking a workspace (#12325)

* fix: keep favorites when forking a workspace

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor: check the fork favorites query at compile time

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Guilhem
2026-11-11 17:34:07 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent b4df6ef833
commit 97dedc2f10
2 changed files with 50 additions and 0 deletions
@@ -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"
}
@@ -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,