BEGIN READ ONLY;
-- Who administers 'default'.
SELECT m.user_id FROM user_organisations m JOIN organisations o ON o.id = m.organisation_id
WHERE o.slug = 'default' AND m.role = 'org_admin';
-- 'default' workspaces with strands and cross-workspace matching on.
SELECT w.id FROM workspaces w JOIN organisations o ON o.id = w.organisation_id
WHERE o.slug = 'default' AND w.strands_enabled AND w.cross_workspace_matching;
-- Topic links between workspaces of different owners (the backfill drops those that end up
-- in different organisations).
SELECT count(*) FROM strand_relations r
JOIN strands a ON a.id = r.from_strand_id JOIN workspaces wa ON wa.id = a.workspace_id
JOIN strands b ON b.id = r.to_strand_id JOIN workspaces wb ON wb.id = b.workspace_id
WHERE wa.owner_user_id IS DISTINCT FROM wb.owner_user_id;
-- Workspaces the backfill will place, by how many real accounts use them (1 → Personal,
-- 2+ → majority team), and how many accounts get a Personal organisation.
SELECT people, count(*) FROM (
SELECT w.id, count(DISTINCT u.id) AS people
FROM workspaces w
LEFT JOIN users u ON (u.id = w.owner_user_id
OR u.id IN (SELECT uw.user_id FROM user_workspaces uw WHERE uw.workspace_id = w.id))
AND NOT u.is_guest AND u.deleted_at IS NULL
WHERE w.organisation_id IS NULL
OR w.organisation_id IN (SELECT id FROM organisations WHERE slug = 'default')
GROUP BY w.id
) t GROUP BY people ORDER BY people;
SELECT count(*) FROM users u
WHERE NOT u.is_guest AND u.deleted_at IS NULL AND u.frozen_at IS NULL;
ROLLBACK;