fix: carry schedule, trigger and folder-rule identities through a username rename

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
hugocasa
2026-10-19 13:24:07 +02:00
co-authored by Claude Opus 5.5
parent de66e47b1c
commit 03dd843a9d
2 changed files with 62 additions and 0 deletions
@@ -0,0 +1,16 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE folder SET default_permissioned_as = (\n SELECT jsonb_agg(\n CASE WHEN rule->>'permissioned_as' = $2\n THEN jsonb_set(rule, ARRAY['permissioned_as'], to_jsonb($1::text))\n ELSE rule END\n ORDER BY ord)\n FROM jsonb_array_elements(default_permissioned_as) WITH ORDINALITY AS t(rule, ord))\n WHERE default_permissioned_as @> jsonb_build_array(jsonb_build_object('permissioned_as', $2::text))\n AND workspace_id = $3",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Text",
"Text",
"Text"
]
},
"nullable": []
},
"hash": "4dce3863311ed156a8eaf1b5879f7416452bf1b95ea771ef51e22aae164cb0ef"
}
+46
View File
@@ -911,6 +911,33 @@ async fn update_username_in_workpsace<'c>(
.execute(&mut **tx)
.await?;
// ---- schedule and trigger identities ----
// Left naming the old username, they fail to run, and run as whoever is given that name next.
for table in [
"schedule",
"http_trigger",
"websocket_trigger",
"postgres_trigger",
"mqtt_trigger",
"kafka_trigger",
"nats_trigger",
"sqs_trigger",
"gcp_trigger",
"email_trigger",
"amqp_trigger",
"azure_trigger",
] {
// SAFETY: `table` comes from the hardcoded list above, not user input.
sqlx::query(&format!(
"UPDATE {table} SET permissioned_as = $1 WHERE permissioned_as = $2 AND workspace_id = $3"
))
.bind(&new_principal)
.bind(&old_principal)
.bind(w_id)
.execute(&mut **tx)
.await?;
}
// ---- group_ ----
sqlx::query!(
@@ -951,6 +978,25 @@ async fn update_username_in_workpsace<'c>(
.execute(&mut **tx)
.await?;
// Rules are ordered, first match wins, so the rewrite keeps their order. A rule left on the
// old username refuses the creation of every runnable it matches.
sqlx::query!(
r#"UPDATE folder SET default_permissioned_as = (
SELECT jsonb_agg(
CASE WHEN rule->>'permissioned_as' = $2
THEN jsonb_set(rule, ARRAY['permissioned_as'], to_jsonb($1::text))
ELSE rule END
ORDER BY ord)
FROM jsonb_array_elements(default_permissioned_as) WITH ORDINALITY AS t(rule, ord))
WHERE default_permissioned_as @> jsonb_build_array(jsonb_build_object('permissioned_as', $2::text))
AND workspace_id = $3"#,
&new_principal,
&old_principal,
w_id
)
.execute(&mut **tx)
.await?;
// ---- input ----
sqlx::query!(