Files
coder/coderd/database/queries/chatmodelconfigs.sql
T
Ethan ad1906589d fix(coderd): allow deleting chat providers used in historical chats (#24568)
Drop the `chat_model_configs.provider -> chat_providers.provider`
foreign key and soft-delete model configs when their provider is
removed. The provider row is now hard-deleted inside a transaction that
also tombstones its model configs and promotes a replacement default
when needed.

Historical chats and messages keep pointing at the soft-deleted model
config rows, which are hidden from live/admin queries but still resolve
for read. The runtime chat path already falls back to the default model
config when a soft-deleted config is looked up.

Replaces the lost FK validation in the create/update model-config
handlers with an explicit provider lookup that returns the existing
`Chat provider is not configured.` 400.

## UX

**Admin deleting a chat provider that has historical usage**

- Before: blocked with 400 `Provider models are still referenced by
existing chats.` Admins had no in-product way to remove a provider that
had ever been used.
- After: delete succeeds (204). Any model configs under that provider
are soft-deleted. If the removed provider owned the default model
config, one of the remaining live configs is auto-promoted to the new
default. The promotion is deterministic (`ensureDefaultChatModelConfig`
picks the first live config by `provider ASC, model ASC, updated_at
DESC, id DESC`); there is no picker, and no toast or response detail
names which config became the new default.

**End users with chats that used a deleted provider's model**

- Old chats still open and their history still renders unchanged.
- Sending a new turn in such a chat silently falls back to the current
default model. No banner or warning tells the user the original model is
gone.
- The model picker no longer lists the deleted model.
- If no default model config exists at all after the delete, sending a
new turn fails with `no default chat model config is available`.

**Admin creating or updating a model config against a provider that is
not configured**

- Same as before: 400 `Chat provider is not configured.` Only the
detection mechanism changed (explicit `FOR UPDATE` lookup inside the
transaction, which also serializes against a concurrent provider
delete).

**Admin updating a model config whose row disappears mid-transaction**

- Now returns the standard 404 `Resource not found or you do not have
access to this resource` instead of the previous 500 that leaked `sql:
no rows in result set` in the detail. Unrelated internal races (for
example a race on the promoted default candidate) are still reported as
500 so they are not misclassified as "your target is gone".

Closes CODAGT-23
2026-04-22 19:34:34 +10:00

141 lines
2.7 KiB
SQL

-- name: GetChatModelConfigByID :one
SELECT
*
FROM
chat_model_configs
WHERE
id = @id::uuid
AND deleted = FALSE;
-- name: GetDefaultChatModelConfig :one
SELECT
*
FROM
chat_model_configs
WHERE
is_default = TRUE
AND deleted = FALSE;
-- name: GetChatModelConfigs :many
SELECT
*
FROM
chat_model_configs
WHERE
deleted = FALSE
ORDER BY
provider ASC,
model ASC,
updated_at DESC,
id DESC;
-- name: GetEnabledChatModelConfigs :many
SELECT
cmc.*
FROM
chat_model_configs cmc
JOIN
chat_providers cp ON cp.provider = cmc.provider
WHERE
cmc.enabled = TRUE
AND cmc.deleted = FALSE
AND cp.enabled = TRUE
ORDER BY
cmc.provider ASC,
cmc.model ASC,
cmc.updated_at DESC,
cmc.id DESC;
-- name: GetEnabledChatModelConfigByID :one
SELECT
cmc.*
FROM
chat_model_configs cmc
-- Providers can be disabled independently of their model configs.
-- Check both to ensure the selected config is actually usable.
JOIN
chat_providers cp ON cp.provider = cmc.provider
WHERE
cmc.id = @id::uuid
AND cmc.deleted = FALSE
AND cmc.enabled = TRUE
AND cp.enabled = TRUE;
-- name: InsertChatModelConfig :one
INSERT INTO chat_model_configs (
provider,
model,
display_name,
created_by,
updated_by,
enabled,
is_default,
context_limit,
compression_threshold,
options
) VALUES (
@provider::text,
@model::text,
@display_name::text,
sqlc.narg('created_by')::uuid,
sqlc.narg('updated_by')::uuid,
@enabled::boolean,
@is_default::boolean,
@context_limit::bigint,
@compression_threshold::integer,
@options::jsonb
)
RETURNING
*;
-- name: UpdateChatModelConfig :one
UPDATE
chat_model_configs
SET
provider = @provider::text,
model = @model::text,
display_name = @display_name::text,
updated_by = sqlc.narg('updated_by')::uuid,
enabled = @enabled::boolean,
is_default = @is_default::boolean,
context_limit = @context_limit::bigint,
compression_threshold = @compression_threshold::integer,
options = @options::jsonb,
updated_at = NOW()
WHERE
id = @id::uuid
AND deleted = FALSE
RETURNING
*;
-- name: UnsetDefaultChatModelConfigs :exec
UPDATE
chat_model_configs
SET
is_default = FALSE,
updated_at = NOW()
WHERE
is_default = TRUE
AND deleted = FALSE;
-- name: DeleteChatModelConfigByID :exec
UPDATE
chat_model_configs
SET
deleted = TRUE,
deleted_at = NOW(),
updated_at = NOW()
WHERE
id = @id::uuid;
-- name: DeleteChatModelConfigsByProvider :exec
UPDATE
chat_model_configs
SET
deleted = TRUE,
deleted_at = NOW(),
updated_at = NOW()
WHERE
provider = @provider::text
AND deleted = FALSE;