Skip to content

Commit 0df245e

Browse files
loganjLarryclaude
committed
fix(scripts): bind each starter id to its name across every channel row
The starter rule looked for a starter id only among the listed channels with that name. A row at the id that was renamed, made private or unlisted, or soft-deleted still makes the relay refuse the create there, so the archived namesakes were renamed for nothing. The plan now checks the id against every channel row in the community, per starter name. The script trims whitespace and every trailing "/" from the origin, as desktop does, accepts BUZZ_STARTER_ORIGIN for a desktop build with its own relay HTTP origin, and prints the ids it computed so they can be checked against a client. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Logan Johnson <[email protected]>
1 parent 9f7b3b2 commit 0df245e

3 files changed

Lines changed: 77 additions & 30 deletions

File tree

‎crates/buzz-db/src/runtime/tests/listed_channel_name_postgres_tests.rs‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ async fn plan_keeps_each_listed_name_on_its_oldest_unarchived_channel() {
180180
/// when no open, unarchived stream has the name, and the relay refuses that
181181
/// create while an archived channel holds it. The plan renames every holder of
182182
/// a starter name when all of them are archived, and no other archived name,
183-
/// unless one of them is at the clients' starter id.
184-
/// Mutation: drop the `all_archived` or `NOT starter_held` condition in the
185-
/// plan, or the `at_starter_id` sort key → RED.
183+
/// unless a channel row has that name's starter id.
184+
/// Mutation: drop the `all_archived` or `NOT IN starters` condition in the
185+
/// plan, the `at_starter_id` sort key, or the slug binding in `starters` → RED.
186186
#[tokio::test]
187187
#[ignore = "requires Postgres"]
188188
async fn plan_frees_a_starter_name_that_only_archived_channels_hold() {
@@ -265,6 +265,35 @@ async fn plan_frees_a_starter_name_that_only_archived_channels_hold() {
265265
rename_plan_with_starters(&pool, &fixed.host, &[starter, Uuid::new_v4()]).await,
266266
vec![(older, "general".to_owned(), renamed(older, "general").1)]
267267
);
268+
269+
// Any row at a starter's id blocks the create there, whatever its name or
270+
// visibility, so the starter rule renames nothing for that name. Each id
271+
// binds only its own name: `welcome-everyone` is still freed.
272+
let moved = Seed::new(&pool).await;
273+
let [general_id, archived_general, archived_welcome] = [(); 3].map(|_| Uuid::new_v4());
274+
moved.open(&pool, general_id, "general", 90).await;
275+
moved.open(&pool, archived_general, "general", 30).await;
276+
moved
277+
.open(&pool, archived_welcome, "welcome-everyone", 30)
278+
.await;
279+
sqlx::query("UPDATE channels SET name = 'general-old', visibility = 'private' WHERE id = $1")
280+
.bind(general_id)
281+
.execute(&pool)
282+
.await
283+
.unwrap();
284+
sqlx::query("UPDATE channels SET archived_at = now() WHERE id = ANY($1)")
285+
.bind(vec![archived_general, archived_welcome])
286+
.execute(&pool)
287+
.await
288+
.unwrap();
289+
assert_eq!(
290+
rename_plan_with_starters(&pool, &moved.host, &[general_id, Uuid::new_v4()]).await,
291+
vec![(
292+
archived_welcome,
293+
"welcome-everyone".to_owned(),
294+
renamed(archived_welcome, "welcome-everyone").1
295+
)]
296+
);
268297
}
269298

270299
#[tokio::test]

‎scripts/maintenance/plan_duplicate_listed_channel_renames.sql‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,28 @@
2020
-- mobile apps create those starter channels when no open, unarchived stream
2121
-- has the name, and the relay refuses that create while an archived channel
2222
-- holds the name. The rename frees the name for them. The apps create each
23-
-- starter at a fixed id (`starter_ids`, from the rename script). When an
24-
-- archived channel holds that id, the relay refuses the create at the id
25-
-- whatever the name, so no rename helps: the archived starter then keeps
26-
-- the name, and only its duplicates are renamed.
23+
-- starter at a fixed id (`starter_ids`, from the rename script, in the order
24+
-- general, welcome-everyone). When any channel row has that id, whatever
25+
-- its name, visibility or state, the relay refuses the create at the id, so
26+
-- no rename helps: the starter rule then renames nothing for that name. An
27+
-- archived starter at its id also keeps the name over its namesakes.
2728
--
2829
-- psql -X -v ON_ERROR_STOP=1 -v host=<community host> \
2930
-- -v starter_ids='{<general id>,<welcome-everyone id>}' \
3031
-- -f scripts/maintenance/plan_duplicate_listed_channel_renames.sql
3132

32-
WITH listed AS (
33+
WITH starters AS (
34+
-- The starter names whose fixed id some channel row already has.
35+
SELECT s.slug, s.id
36+
FROM unnest(ARRAY['general', 'welcome-everyone'], :'starter_ids'::uuid[]) AS s(slug, id)
37+
JOIN communities co ON co.host = :'host'
38+
WHERE EXISTS (
39+
SELECT 1 FROM channels ch WHERE ch.community_id = co.id AND ch.id = s.id
40+
)
41+
),
42+
listed AS (
3343
SELECT ch.id, ch.name, ch.archived_at, ch.created_at,
34-
ch.id = ANY(:'starter_ids'::uuid[]) AS at_starter_id
44+
ch.id IN (SELECT id FROM starters) AS at_starter_id
3545
FROM channels ch
3646
JOIN communities co ON co.id = ch.community_id
3747
WHERE co.host = :'host'
@@ -51,13 +61,13 @@ duplicates AS (
5161
archived_at IS NOT NULL AND NOT at_starter_id,
5262
created_at, id
5363
) AS place,
54-
bool_and(archived_at IS NOT NULL) OVER (PARTITION BY lower(name)) AS all_archived,
55-
bool_or(at_starter_id) OVER (PARTITION BY lower(name)) AS starter_held
64+
bool_and(archived_at IS NOT NULL) OVER (PARTITION BY lower(name)) AS all_archived
5665
FROM listed
5766
) ranked
5867
WHERE place > 1
59-
OR (all_archived AND NOT starter_held
60-
AND lower(name) IN ('general', 'welcome-everyone'))
68+
OR (all_archived
69+
AND lower(name) IN ('general', 'welcome-everyone')
70+
AND lower(name) NOT IN (SELECT slug FROM starters))
6171
)
6272
SELECT d.id AS channel_id, d.name,
6373
CASE WHEN EXISTS (

‎scripts/maintenance/rename_duplicate_listed_channel_names.sh‎

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@
3737
# (add-member also finds only an unarchived community), run this script, and
3838
# archive it again. For one being deleted, it stops: no rename can land there,
3939
# and its duplicates go when its deletion completes.
40-
# BUZZ_RELAY_URL defaults to wss://<community host>; it must be the URL
41-
# clients use, because it also gives the clients' starter channel ids.
42-
# BUZZ_CLI defaults to `buzz`. The script needs psql and python3.
40+
# BUZZ_RELAY_URL defaults to wss://<community host>. It also gives the
41+
# clients' starter channel ids unless BUZZ_STARTER_ORIGIN is set (below), so
42+
# it must be the URL clients use. BUZZ_CLI defaults to `buzz`. The script
43+
# needs psql and python3.
4344
set -euo pipefail
4445

4546
usage() {
@@ -63,22 +64,29 @@ buzz=${BUZZ_CLI:-buzz}
6364
plan_sql="$(cd "$(dirname "$0")" && pwd)/plan_duplicate_listed_channel_renames.sql"
6465

6566
# The ids released desktop and mobile apps create their starter channels at:
66-
# UUIDv5 of "starter-channel:v1:<relay HTTP origin>:<slug>", where the origin
67-
# is the relay URL with wss:// as https:// and ws:// as http://
68-
# (starter_channel_uuid in desktop/src-tauri/src/commands/channels.rs). The
69-
# plan keeps the name on an archived starter at one of these ids.
70-
starter_scope=${relay_url%/}
71-
case $starter_scope in
72-
wss://*) starter_scope=https://${starter_scope#wss://} ;;
73-
ws://*) starter_scope=http://${starter_scope#ws://} ;;
74-
esac
75-
starter_ids=$(python3 -I -c '
67+
# UUIDv5 of "starter-channel:v1:<origin>:<slug>" (starter_channel_uuid in
68+
# desktop/src-tauri/src/commands/channels.rs). The origin is the clients'
69+
# relay HTTP origin: BUZZ_STARTER_ORIGIN when set (for a desktop build with
70+
# its own BUZZ_RELAY_HTTP), else the relay URL with wss:// as https:// and
71+
# ws:// as http://, trimmed of whitespace and trailing "/" as desktop does.
72+
# The script prints the ids so they can be checked against a client.
73+
starter=$(python3 -I -c '
7674
import sys, uuid
75+
origin = sys.argv[1].strip().rstrip("/")
76+
if not sys.argv[2]:
77+
for ws, http in (("wss://", "https://"), ("ws://", "http://")):
78+
if origin.startswith(ws):
79+
origin = http + origin[len(ws):]
80+
break
7781
ns = uuid.UUID("3ce33bea-8f09-5f1b-9c85-8a7d2659e6b0")
78-
ids = (uuid.uuid5(ns, "starter-channel:v1:%s:%s" % (sys.argv[1], slug))
79-
for slug in ("general", "welcome-everyone"))
80-
print("{" + ",".join(map(str, ids)) + "}")
81-
' "$starter_scope")
82+
ids = [str(uuid.uuid5(ns, "starter-channel:v1:%s:%s" % (origin.strip(), slug)))
83+
for slug in ("general", "welcome-everyone")]
84+
print(origin)
85+
print("{" + ",".join(ids) + "}")
86+
' "${BUZZ_STARTER_ORIGIN:-$relay_url}" "${BUZZ_STARTER_ORIGIN:-}")
87+
starter_origin=${starter%%$'\n'*}
88+
starter_ids=${starter#*$'\n'}
89+
echo "starter channel ids for $starter_origin (general, welcome-everyone): $starter_ids" >&2
8290

8391
plan=$(mktemp)
8492
trap 'rm -f "$plan"' EXIT

0 commit comments

Comments
 (0)