Files
coder/coderd/database/migrations/000648_chat_diff_statuses_root_chats.up.sql
T
Mathias Fredriksson 6239c3ac94 fix(coderd/database): poll only root chat git refs (#31920)
Diff-status rows on subagent chats still consume provider requests.
Move their refs to root chats so the root chat's Git panel shows them,
and limit diff-status refresh to rows on root chats.

Recognize a subagent chat when root_chat_id or parent_chat_id is set,
matching Chat.IsSubChat.

Keep existing root rows on conflict. Otherwise retain the most recently
refreshed row from a subagent chat, with its metadata and timestamps.
2026-11-30 16:21:47 +02:00

72 lines
1.7 KiB
SQL

-- Git refs belong to the root chat.
WITH subagent_statuses AS (
DELETE FROM chat_diff_statuses cds
USING chats c
WHERE cds.chat_id = c.id
-- Match Chat.IsSubChat when a deleted parent leaves
-- only root_chat_id set.
AND (c.root_chat_id IS NOT NULL OR c.parent_chat_id IS NOT NULL)
RETURNING cds.*, COALESCE(c.root_chat_id, c.parent_chat_id) AS root_chat_id
)
INSERT INTO chat_diff_statuses (
chat_id,
url,
pull_request_state,
changes_requested,
additions,
deletions,
changed_files,
refreshed_at,
stale_at,
created_at,
updated_at,
git_branch,
git_remote_origin,
pull_request_title,
pull_request_draft,
author_login,
author_avatar_url,
base_branch,
pr_number,
commits,
approved,
reviewer_count,
head_branch
)
SELECT DISTINCT ON (root_chat_id, git_remote_origin, git_branch)
root_chat_id,
url,
pull_request_state,
changes_requested,
additions,
deletions,
changed_files,
refreshed_at,
stale_at,
created_at,
updated_at,
git_branch,
git_remote_origin,
pull_request_title,
pull_request_draft,
author_login,
author_avatar_url,
base_branch,
pr_number,
commits,
approved,
reviewer_count,
head_branch
FROM subagent_statuses
ORDER BY
root_chat_id,
git_remote_origin,
git_branch,
-- Keep the most recently refreshed row from a subagent chat.
refreshed_at DESC NULLS LAST,
updated_at DESC,
chat_id
-- Preserve the root chat's metadata and report times so moving duplicate
-- refs does not reorder refs the root chat already tracks.
ON CONFLICT (chat_id, git_remote_origin, git_branch) DO NOTHING;