refactor(coderd): stop storing chat gateway key IDs and drop the columns (#27171)

> Mux is working on behalf of Mike.

## Summary

Stop reading and writing the legacy `api_key_id` columns on chat
messages and queued messages, and drop the columns in the same PR.
Runtime AI Gateway attribution continues to use the per-user synthetic
key introduced by #27170.

With the columns gone, `sqlc` generates `database.ChatMessage` and
`database.ChatQueuedMessage` without `api_key_id`, so no transitional
query scaffolding is needed.

Migration `000548` drops the `api_key_id` columns. #27170 already
removed their foreign keys, so the down migration re-adds nullable text
columns without constraints. Previous column values cannot be restored.

Also moves the model config validation in `CreateChat` above the
message-building work so a disabled or invalid model fails fast. On main
this mattered more: the old ordering minted a synthetic API key before
rejecting the request.

Deploy note: replicas still running the previous release write
`api_key_id` on insert, so chat message inserts on old replicas fail
during the rolling window after the column drop. This was previously
split across two PRs to avoid that window; per review feedback the split
added more churn than it was worth for an experimental surface.

Depends on #27170 (merged).
This commit is contained in:
Michael Suchacz
2026-07-20 19:49:19 +02:00
committed by GitHub
parent f18c664be2
commit 4ed6fcced7
30 changed files with 186 additions and 613 deletions
+2 -6
View File
@@ -895,7 +895,6 @@ updated_chat AS (
INSERT INTO chat_messages (
chat_id,
created_by,
api_key_id,
model_config_id,
reasoning_effort,
role,
@@ -916,7 +915,6 @@ INSERT INTO chat_messages (
SELECT
@chat_id::uuid,
NULLIF(UNNEST(@created_by::uuid[]), '00000000-0000-0000-0000-000000000000'::uuid),
NULLIF(UNNEST(@api_key_id::text[]), ''),
NULLIF(UNNEST(@model_config_id::uuid[]), '00000000-0000-0000-0000-000000000000'::uuid),
NULLIF(UNNEST(@reasoning_effort::text[]), '')::chat_reasoning_effort,
UNNEST(@role::chat_message_role[]),
@@ -1859,13 +1857,12 @@ RETURNING
-- Legacy queue insertion path. When no caller-supplied creator exists,
-- preserve the created_by invariant by attributing the queued row to the
-- chat owner.
INSERT INTO chat_queued_messages (chat_id, content, model_config_id, reasoning_effort, api_key_id, created_by)
INSERT INTO chat_queued_messages (chat_id, content, model_config_id, reasoning_effort, created_by)
SELECT
@chat_id::uuid,
@content::jsonb,
sqlc.narg('model_config_id')::uuid,
sqlc.narg('reasoning_effort')::chat_reasoning_effort,
sqlc.narg('api_key_id')::text,
chats.owner_id
FROM chats
WHERE chats.id = @chat_id::uuid
@@ -2865,13 +2862,12 @@ SELECT NOW()::timestamptz AS now;
-- Inserts a queued message that carries a position (from the default
-- sequence) and an explicit created_by reference. Use this when the
-- queued-message creator differs from the chat owner.
INSERT INTO chat_queued_messages (chat_id, content, model_config_id, reasoning_effort, api_key_id, created_by)
INSERT INTO chat_queued_messages (chat_id, content, model_config_id, reasoning_effort, created_by)
VALUES (
@chat_id::uuid,
@content::jsonb,
sqlc.narg('model_config_id')::uuid,
sqlc.narg('reasoning_effort')::chat_reasoning_effort,
sqlc.narg('api_key_id')::text,
@created_by::uuid
)
RETURNING *;