sub2api/backend/migrations/138_channel_monitor_openai_api_mode.sql
benjamin 3eff5f512a feat(channel-monitor): 添加 API 模式迁移
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-05-19 22:05:43 +08:00

41 lines
1.6 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Migration: 137_channel_monitor_openai_api_mode
-- 为渠道监控和请求模板增加 OpenAI 协议模式:
-- chat_completions -> /v1/chat/completions + messages
-- responses -> /v1/responses + instructions/input
-- 历史数据默认保持 chat_completions避免改变现有监控行为。
ALTER TABLE channel_monitors
ADD COLUMN IF NOT EXISTS api_mode VARCHAR(32) NOT NULL DEFAULT 'chat_completions';
ALTER TABLE channel_monitor_request_templates
ADD COLUMN IF NOT EXISTS api_mode VARCHAR(32) NOT NULL DEFAULT 'chat_completions';
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.table_constraints
WHERE constraint_name = 'channel_monitors_api_mode_check'
AND table_name = 'channel_monitors'
) THEN
ALTER TABLE channel_monitors
ADD CONSTRAINT channel_monitors_api_mode_check
CHECK (api_mode IN ('chat_completions', 'responses'));
END IF;
IF NOT EXISTS (
SELECT 1 FROM information_schema.table_constraints
WHERE constraint_name = 'channel_monitor_request_templates_api_mode_check'
AND table_name = 'channel_monitor_request_templates'
) THEN
ALTER TABLE channel_monitor_request_templates
ADD CONSTRAINT channel_monitor_request_templates_api_mode_check
CHECK (api_mode IN ('chat_completions', 'responses'));
END IF;
END $$;
CREATE INDEX IF NOT EXISTS idx_channel_monitors_provider_api_mode
ON channel_monitors (provider, api_mode);
CREATE INDEX IF NOT EXISTS idx_channel_monitor_templates_provider_api_mode
ON channel_monitor_request_templates (provider, api_mode);