fix: guard all legacy migrations against fresh schema for CI compat

On fresh databases, 0001_init.up.sql creates providers/provider_id
(not llm_providers/llm_provider_id). Migrations 0013, 0041, 0046, 0047
referenced the old names without guards, causing CI migration failures.

- 0013: check llm_provider_id column exists before adding old constraint
- 0041: check llm_providers table exists before backfill/constraint DDL
- 0046: wrap CREATE TABLE in DO block with llm_providers existence check
- 0047: use ALTER TABLE IF EXISTS + DO block guard
This commit is contained in:
Acbox
2026-04-08 00:59:04 +08:00
parent 889b6dbaa4
commit e9b7acd2b4
5 changed files with 52 additions and 42 deletions
@@ -8,8 +8,11 @@ ALTER TABLE IF EXISTS llm_providers
-- 26. Backfill and migrate only when old columns exist (idempotent for fresh DBs).
DO $$ BEGIN
-- Back-fill provider client_type from models.client_type (old column)
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'models' AND column_name = 'client_type') THEN
-- Back-fill provider client_type from models.client_type (old column).
-- Only runs on pre-0061 schema where llm_providers table still exists.
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'llm_providers')
AND EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'models' AND column_name = 'client_type')
THEN
UPDATE llm_providers p
SET client_type = sub.client_type
FROM (
@@ -21,8 +24,10 @@ DO $$ BEGIN
WHERE p.id = sub.llm_provider_id;
END IF;
-- Add CHECK constraint (skip if already present)
IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE constraint_name = 'llm_providers_client_type_check') THEN
-- Add CHECK constraint (skip if already present or table renamed)
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'llm_providers')
AND NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE constraint_name = 'llm_providers_client_type_check')
THEN
ALTER TABLE llm_providers
ADD CONSTRAINT llm_providers_client_type_check
CHECK (client_type IN ('openai-responses', 'openai-completions', 'anthropic-messages', 'google-generative-ai'));