138_channel_monitor_openai_api_mode.sql 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. -- Migration: 137_channel_monitor_openai_api_mode
  2. -- 为渠道监控和请求模板增加 OpenAI 协议模式:
  3. -- chat_completions -> /v1/chat/completions + messages
  4. -- responses -> /v1/responses + instructions/input
  5. -- 历史数据默认保持 chat_completions,避免改变现有监控行为。
  6. ALTER TABLE channel_monitors
  7. ADD COLUMN IF NOT EXISTS api_mode VARCHAR(32) NOT NULL DEFAULT 'chat_completions';
  8. ALTER TABLE channel_monitor_request_templates
  9. ADD COLUMN IF NOT EXISTS api_mode VARCHAR(32) NOT NULL DEFAULT 'chat_completions';
  10. DO $$
  11. BEGIN
  12. IF NOT EXISTS (
  13. SELECT 1 FROM information_schema.table_constraints
  14. WHERE constraint_name = 'channel_monitors_api_mode_check'
  15. AND table_name = 'channel_monitors'
  16. ) THEN
  17. ALTER TABLE channel_monitors
  18. ADD CONSTRAINT channel_monitors_api_mode_check
  19. CHECK (api_mode IN ('chat_completions', 'responses'));
  20. END IF;
  21. IF NOT EXISTS (
  22. SELECT 1 FROM information_schema.table_constraints
  23. WHERE constraint_name = 'channel_monitor_request_templates_api_mode_check'
  24. AND table_name = 'channel_monitor_request_templates'
  25. ) THEN
  26. ALTER TABLE channel_monitor_request_templates
  27. ADD CONSTRAINT channel_monitor_request_templates_api_mode_check
  28. CHECK (api_mode IN ('chat_completions', 'responses'));
  29. END IF;
  30. END $$;
  31. CREATE INDEX IF NOT EXISTS idx_channel_monitors_provider_api_mode
  32. ON channel_monitors (provider, api_mode);
  33. CREATE INDEX IF NOT EXISTS idx_channel_monitor_templates_provider_api_mode
  34. ON channel_monitor_request_templates (provider, api_mode);