Files
model-gateway/update.sql
T
19904408334andClaude 58d9891205 chore: 存量拷贝转引用迁移 SQL
按同名系统模型把存量拷贝行转引用行(配置列清空、幂等、ref_system_model_id 守卫)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-29 14:39:18 +08:00

356 lines
20 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.
-- =========================
-- model_gateway_models
-- =========================
CREATE TABLE IF NOT EXISTS model_gateway_models (
id int8 PRIMARY KEY,
tenant_id int8 NOT NULL DEFAULT 0,
creator varchar(64) NOT NULL,
created_at timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
updater varchar(64) NOT NULL,
updated_at timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp(6),
model_name varchar(128) NOT NULL,
model_type int2 NOT NULL DEFAULT 0,
operator_name varchar(64) NOT NULL DEFAULT '',
base_url varchar(256) NOT NULL,
http_method varchar(8) NOT NULL DEFAULT 'POST',
head_msg jsonb NOT NULL DEFAULT '{}',
api_key varchar(256) NOT NULL DEFAULT '',
is_private int2 NOT NULL DEFAULT 0,
enabled int2 NOT NULL DEFAULT 1,
is_chat_model int2 NOT NULL DEFAULT 0,
is_owner int2 NOT NULL DEFAULT 99,
form_json jsonb NOT NULL DEFAULT '{}',
request_mapping jsonb NOT NULL DEFAULT '{}',
response_mapping jsonb NOT NULL DEFAULT '{}',
response_body varchar(128) NOT NULL DEFAULT '',
token_config jsonb NOT NULL DEFAULT '{}',
extend_mapping jsonb NOT NULL DEFAULT '{}',
query_config jsonb NOT NULL DEFAULT '{}',
stream_config jsonb NOT NULL DEFAULT '{}',
first_frame varchar(128) NOT NULL DEFAULT '',
last_frame varchar(128) NOT NULL DEFAULT '',
max_concurrency int4 NOT NULL DEFAULT 10,
timeout_seconds int4 NOT NULL DEFAULT 600,
retry_times int2 NOT NULL DEFAULT 3,
response_token_field varchar(128) NOT NULL DEFAULT '',
call_mode int2 NOT NULL DEFAULT 0,
required_fields jsonb NOT NULL DEFAULT '[]',
max_tokens int4 DEFAULT 0
);
CREATE UNIQUE INDEX IF NOT EXISTS uk_model_gateway_models_tenant_creator_model ON model_gateway_models (tenant_id, creator, model_name);
CREATE INDEX IF NOT EXISTS idx_model_gateway_models_model_name ON model_gateway_models (model_name);
CREATE INDEX IF NOT EXISTS idx_model_gateway_models_model_type ON model_gateway_models (model_type);
CREATE INDEX IF NOT EXISTS idx_model_gateway_models_tenant_id ON model_gateway_models (tenant_id);
CREATE INDEX IF NOT EXISTS idx_model_gateway_models_deleted_at ON model_gateway_models (deleted_at);
CREATE INDEX IF NOT EXISTS idx_model_gateway_models_enabled ON model_gateway_models (enabled);
COMMENT ON TABLE model_gateway_models IS '模型配置表';
COMMENT ON COLUMN model_gateway_models.id IS '主键ID(非自增)';
COMMENT ON COLUMN model_gateway_models.tenant_id IS '租户ID';
COMMENT ON COLUMN model_gateway_models.creator IS '创建人';
COMMENT ON COLUMN model_gateway_models.created_at IS '创建时间';
COMMENT ON COLUMN model_gateway_models.updater IS '更新人';
COMMENT ON COLUMN model_gateway_models.updated_at IS '更新时间';
COMMENT ON COLUMN model_gateway_models.deleted_at IS '删除时间(软删)';
COMMENT ON COLUMN model_gateway_models.model_name IS '模型名称';
COMMENT ON COLUMN model_gateway_models.model_type IS '模型类型';
COMMENT ON COLUMN model_gateway_models.operator_name IS '运营商名称';
COMMENT ON COLUMN model_gateway_models.base_url IS '模型地址';
COMMENT ON COLUMN model_gateway_models.http_method IS '请求方式 GET/POST';
COMMENT ON COLUMN model_gateway_models.head_msg IS '请求头信息';
COMMENT ON COLUMN model_gateway_models.api_key IS '调用凭证/密钥';
COMMENT ON COLUMN model_gateway_models.is_private IS '是否私有化:0-私有 1-公共';
COMMENT ON COLUMN model_gateway_models.enabled IS '是否启用:0-停用 1-启用';
COMMENT ON COLUMN model_gateway_models.is_chat_model IS '是否为对话模型:0-否 1-是';
COMMENT ON COLUMN model_gateway_models.is_owner IS '1=当前用户创建 0=超级管理员';
COMMENT ON COLUMN model_gateway_models.call_mode IS '调用模式:0-同步 1-异步 2-流式';
COMMENT ON COLUMN model_gateway_models.form_json IS '动态表单结构';
COMMENT ON COLUMN model_gateway_models.request_mapping IS '请求映射';
COMMENT ON COLUMN model_gateway_models.response_mapping IS '返回映射';
COMMENT ON COLUMN model_gateway_models.response_body IS '返回主体';
COMMENT ON COLUMN model_gateway_models.token_config IS 'Token计算配置';
COMMENT ON COLUMN model_gateway_models.extend_mapping IS '附加映射';
COMMENT ON COLUMN model_gateway_models.query_config IS '查询/回调配置';
COMMENT ON COLUMN model_gateway_models.stream_config IS '流式输出配置';
COMMENT ON COLUMN model_gateway_models.first_frame IS '首帧图片参数';
COMMENT ON COLUMN model_gateway_models.last_frame IS '尾帧图片参数';
COMMENT ON COLUMN model_gateway_models.max_concurrency IS '最大并发数';
COMMENT ON COLUMN model_gateway_models.timeout_seconds IS '调用模型超时(秒)';
COMMENT ON COLUMN model_gateway_models.retry_times IS '失败重试次数';
COMMENT ON COLUMN model_gateway_models.response_token_field IS '响应中消耗token的字段映射';
COMMENT ON COLUMN model_gateway_models.required_fields IS '必选字段列表';
COMMENT ON COLUMN model_gateway_models.max_tokens IS '最大 token 数,0 表示不传';
-- =========================
-- model_gateway_task
-- =========================
CREATE TABLE IF NOT EXISTS model_gateway_task (
id int8 PRIMARY KEY,
tenant_id int8 NOT NULL DEFAULT 0,
creator varchar(64) NOT NULL,
created_at timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
updater varchar(64) NOT NULL,
updated_at timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp(6),
model_name varchar(128) NOT NULL,
task_id varchar(64) NOT NULL,
biz_name varchar(128) NOT NULL DEFAULT '',
callback_url varchar(512) DEFAULT '',
state int2 NOT NULL DEFAULT 0,
retry_count int4 NOT NULL DEFAULT 0,
phase int2 NOT NULL DEFAULT 0,
tmp_file text DEFAULT '',
error_msg text DEFAULT '',
result_file jsonb NOT NULL DEFAULT '{}',
request_payload jsonb NOT NULL DEFAULT '{}',
text_result jsonb NOT NULL DEFAULT '{}',
expend_tokens int8 NOT NULL DEFAULT 0,
duration_seconds int8 NOT NULL DEFAULT 0,
epicycle_id varchar(64) NOT NULL DEFAULT ''
);
CREATE UNIQUE INDEX IF NOT EXISTS uk_model_gateway_task_tenant_creator_task_id ON model_gateway_task (tenant_id, creator, task_id);
CREATE INDEX IF NOT EXISTS idx_model_gateway_task_task_id ON model_gateway_task (task_id);
CREATE INDEX IF NOT EXISTS idx_model_gateway_task_state ON model_gateway_task (state);
CREATE INDEX IF NOT EXISTS idx_model_gateway_task_deleted_at ON model_gateway_task (deleted_at);
COMMENT ON TABLE model_gateway_task IS '模型网关任务表';
COMMENT ON COLUMN model_gateway_task.id IS '主键ID';
COMMENT ON COLUMN model_gateway_task.tenant_id IS '租户ID';
COMMENT ON COLUMN model_gateway_task.creator IS '创建人';
COMMENT ON COLUMN model_gateway_task.created_at IS '创建时间';
COMMENT ON COLUMN model_gateway_task.updater IS '更新人';
COMMENT ON COLUMN model_gateway_task.updated_at IS '更新时间';
COMMENT ON COLUMN model_gateway_task.deleted_at IS '删除时间(软删)';
COMMENT ON COLUMN model_gateway_task.model_name IS '模型名称';
COMMENT ON COLUMN model_gateway_task.task_id IS '任务ID(对外返回)';
COMMENT ON COLUMN model_gateway_task.biz_name IS '业务名称(调用方模块/系统)';
COMMENT ON COLUMN model_gateway_task.callback_url IS '回调地址';
COMMENT ON COLUMN model_gateway_task.state IS '0排队中/1执行中/2成功/3失败/4已下载';
COMMENT ON COLUMN model_gateway_task.retry_count IS '已重试次数';
COMMENT ON COLUMN model_gateway_task.phase IS '执行阶段:0模型阶段/1OSS阶段';
COMMENT ON COLUMN model_gateway_task.tmp_file IS '临时结果文件路径';
COMMENT ON COLUMN model_gateway_task.error_msg IS '错误信息';
COMMENT ON COLUMN model_gateway_task.result_file IS '结果文件:{oss_file, file_type, file_size}';
COMMENT ON COLUMN model_gateway_task.request_payload IS '请求参数(JSON';
COMMENT ON COLUMN model_gateway_task.text_result IS '文本类结果';
COMMENT ON COLUMN model_gateway_task.expend_tokens IS '消耗token数';
COMMENT ON COLUMN model_gateway_task.duration_seconds IS '耗时(秒)';
COMMENT ON COLUMN model_gateway_task.epicycle_id IS '轮次ID';
-- =========================
-- model_gateway_log_stat
-- =========================
CREATE TABLE IF NOT EXISTS model_gateway_log_stat (
day date NOT NULL,
tenant_id int8 NOT NULL DEFAULT 0,
creator varchar(64) NOT NULL DEFAULT '',
model_name varchar(128) NOT NULL DEFAULT '',
request_count int8 NOT NULL DEFAULT 0,
created_at timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (day, tenant_id, creator, model_name)
);
CREATE INDEX IF NOT EXISTS idx_model_gateway_log_stat_day ON model_gateway_log_stat (day);
CREATE INDEX IF NOT EXISTS idx_model_gateway_log_stat_creator ON model_gateway_log_stat (creator);
CREATE INDEX IF NOT EXISTS idx_model_gateway_log_stat_model_name ON model_gateway_log_stat (model_name);
CREATE INDEX IF NOT EXISTS idx_model_gateway_log_stat_tenant_day ON model_gateway_log_stat (tenant_id, day);
COMMENT ON TABLE model_gateway_log_stat IS '按天统计表';
COMMENT ON COLUMN model_gateway_log_stat.day IS '天(YYYY-MM-DD';
COMMENT ON COLUMN model_gateway_log_stat.tenant_id IS '租户ID';
COMMENT ON COLUMN model_gateway_log_stat.creator IS '创建人';
COMMENT ON COLUMN model_gateway_log_stat.model_name IS '模型名称';
COMMENT ON COLUMN model_gateway_log_stat.request_count IS '请求次数';
COMMENT ON COLUMN model_gateway_log_stat.created_at IS '创建时间';
COMMENT ON COLUMN model_gateway_log_stat.updated_at IS '更新时间';
-- =========================
-- model_gateway_logs_op
-- =========================
CREATE TABLE IF NOT EXISTS model_gateway_logs_op (
id int8 PRIMARY KEY,
tenant_id int8 NOT NULL DEFAULT 0,
creator varchar(64) NOT NULL,
created_at timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
updater varchar(64) NOT NULL,
updated_at timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp(6),
ip varchar(64) DEFAULT '',
user_agent varchar(256) DEFAULT '',
api_path varchar(256) DEFAULT '',
http_method varchar(16) DEFAULT '',
biz_name varchar(128) NOT NULL DEFAULT '',
model_name varchar(128) NOT NULL DEFAULT '',
task_id varchar(64) NOT NULL DEFAULT '',
op_type varchar(64) NOT NULL DEFAULT 'createTask',
success int2 NOT NULL DEFAULT 1,
error_msg text DEFAULT '',
cost_ms int8 NOT NULL DEFAULT 0,
request_payload jsonb,
response_payload jsonb
);
CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_task_id ON model_gateway_logs_op (task_id);
CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_biz_name ON model_gateway_logs_op (biz_name);
CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_model_name ON model_gateway_logs_op (model_name);
CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_op_type ON model_gateway_logs_op (op_type);
CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_deleted_at ON model_gateway_logs_op (deleted_at);
CREATE INDEX IF NOT EXISTS idx_model_gateway_logs_op_tenant_time ON model_gateway_logs_op (tenant_id, created_at);
COMMENT ON TABLE model_gateway_logs_op IS '操作日志表';
COMMENT ON COLUMN model_gateway_logs_op.id IS '主键ID(非自增)';
COMMENT ON COLUMN model_gateway_logs_op.tenant_id IS '租户ID';
COMMENT ON COLUMN model_gateway_logs_op.creator IS '创建人';
COMMENT ON COLUMN model_gateway_logs_op.created_at IS '创建时间';
COMMENT ON COLUMN model_gateway_logs_op.updater IS '更新人';
COMMENT ON COLUMN model_gateway_logs_op.updated_at IS '更新时间';
COMMENT ON COLUMN model_gateway_logs_op.deleted_at IS '删除时间(软删)';
COMMENT ON COLUMN model_gateway_logs_op.ip IS '客户端IP';
COMMENT ON COLUMN model_gateway_logs_op.user_agent IS 'User-Agent';
COMMENT ON COLUMN model_gateway_logs_op.api_path IS '接口路径';
COMMENT ON COLUMN model_gateway_logs_op.http_method IS 'HTTP方法';
COMMENT ON COLUMN model_gateway_logs_op.biz_name IS '业务名称(调用方模块/系统)';
COMMENT ON COLUMN model_gateway_logs_op.model_name IS '模型名称';
COMMENT ON COLUMN model_gateway_logs_op.task_id IS '任务ID';
COMMENT ON COLUMN model_gateway_logs_op.op_type IS '操作类型';
COMMENT ON COLUMN model_gateway_logs_op.success IS '是否成功:1成功/0失败';
COMMENT ON COLUMN model_gateway_logs_op.error_msg IS '错误信息(失败时)';
COMMENT ON COLUMN model_gateway_logs_op.cost_ms IS '耗时(毫秒)';
COMMENT ON COLUMN model_gateway_logs_op.request_payload IS '请求 JSON';
COMMENT ON COLUMN model_gateway_logs_op.response_payload IS '响应 JSON';
-- =====================================================================================
--------------------pgsql创建model_gateway_model_manage表语句---------------------------
-- 模型管理表
CREATE TABLE IF NOT EXISTS model_gateway_model_manage (
id BIGINT PRIMARY KEY,
tenant_id BIGINT NOT NULL DEFAULT 0,
creator VARCHAR(64) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updater VARCHAR(64) NOT NULL,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp(6),
model_supplier VARCHAR(64) NOT NULL DEFAULT '',
model_name VARCHAR(128) NOT NULL DEFAULT '',
model_type INT NOT NULL DEFAULT 0,
base_url VARCHAR(512) NOT NULL DEFAULT '',
http_method VARCHAR(32) NOT NULL DEFAULT '',
system_model BOOLEAN NOT NULL DEFAULT false,
private_model BOOLEAN NOT NULL DEFAULT false,
chat_model BOOLEAN NOT NULL DEFAULT false,
invoke_type VARCHAR(32) NOT NULL DEFAULT '',
api_key VARCHAR(255) NOT NULL DEFAULT '',
enabled BOOLEAN NOT NULL DEFAULT false,
request_mapping JSONB DEFAULT '{}',
response_mapping JSONB DEFAULT '{}',
max_concurrency INT NOT NULL DEFAULT 0
);
-- 索引
CREATE INDEX idx_model_manage_tenant_id ON model_gateway_model_manage(tenant_id);
CREATE INDEX idx_model_manage_supplier ON model_gateway_model_manage(model_supplier);
CREATE INDEX idx_model_manage_type ON model_gateway_model_manage(model_type);
CREATE INDEX idx_model_manage_enabled ON model_gateway_model_manage(enabled);
CREATE INDEX idx_model_manage_deleted_at ON model_gateway_model_manage(deleted_at);
-- 字段与表注释
COMMENT ON TABLE model_gateway_model_manage IS '模型管理表';
COMMENT ON COLUMN model_gateway_model_manage.id IS '主键ID';
COMMENT ON COLUMN model_gateway_model_manage.tenant_id IS '租户ID';
COMMENT ON COLUMN model_gateway_model_manage.creator IS '创建人';
COMMENT ON COLUMN model_gateway_model_manage.created_at IS '创建时间';
COMMENT ON COLUMN model_gateway_model_manage.updater IS '更新人';
COMMENT ON COLUMN model_gateway_model_manage.updated_at IS '更新时间';
COMMENT ON COLUMN model_gateway_model_manage.deleted_at IS '删除时间(软删)';
COMMENT ON COLUMN model_gateway_model_manage.model_supplier IS '模型供应商';
COMMENT ON COLUMN model_gateway_model_manage.model_name IS '模型名称';
COMMENT ON COLUMN model_gateway_model_manage.model_type IS '模型类型';
COMMENT ON COLUMN model_gateway_model_manage.base_url IS '模型地址';
COMMENT ON COLUMN model_gateway_model_manage.http_method IS 'http方法';
COMMENT ON COLUMN model_gateway_model_manage.system_model IS '是否系统模型';
COMMENT ON COLUMN model_gateway_model_manage.private_model IS '是否私有模型';
COMMENT ON COLUMN model_gateway_model_manage.chat_model IS '是否聊天模型';
COMMENT ON COLUMN model_gateway_model_manage.invoke_type IS '调用类型';
COMMENT ON COLUMN model_gateway_model_manage.api_key IS 'api key';
COMMENT ON COLUMN model_gateway_model_manage.enabled IS '是否启用';
COMMENT ON COLUMN model_gateway_model_manage.request_mapping IS '请求映射';
COMMENT ON COLUMN model_gateway_model_manage.response_mapping IS '响应映射';
COMMENT ON COLUMN model_gateway_model_manage.max_concurrency IS '最大并发数';
--------------------pgsql创建model_gateway_model_manage表语句---------------------------
-- =========================
-- 计费规则:model_manage 新增 price_configJSONB),model_session 新增 total_costNUMERIC
-- =========================
ALTER TABLE model_gateway_model_manage
ADD COLUMN IF NOT EXISTS price_config JSONB DEFAULT NULL;
COMMENT ON COLUMN model_gateway_model_manage.price_config
IS '计费规则:{currency,unit,dimensions,rules,discount},未配置为NULL(费用按0处理)';
ALTER TABLE model_gateway_session
ADD COLUMN IF NOT EXISTS total_cost NUMERIC DEFAULT 0;
COMMENT ON COLUMN model_gateway_session.total_cost
IS '本次调用总费用(元),未配置计费规则为0';
-- =========================
-- 异步任务计费:task_start 快照 media_typetask_end 记录 total_cost
-- 模型计费配置(price_config)任务完成时按 modelId 从 model_manage 现查,不在 task_start 快照
-- =========================
ALTER TABLE model_gateway_model_task_start
ADD COLUMN IF NOT EXISTS media_type VARCHAR(32) DEFAULT NULL;
COMMENT ON COLUMN model_gateway_model_task_start.media_type
IS '输入媒体类型快照(audio/no_video/has_video,创建任务时按请求体参考媒体字段推导)';
ALTER TABLE model_gateway_model_task_end
ADD COLUMN IF NOT EXISTS total_cost NUMERIC DEFAULT 0;
COMMENT ON COLUMN model_gateway_model_task_end.total_cost
IS '本次调用总费用(元),未配置计费规则为0';
-- =========================
-- 模型引用化:model_manage 新增 ref_system_model_id(引用行指向系统模型)
-- 系统模型 = 配置唯一来源;引用行只存 apiKey/enabled/chatModel,配置列留空
-- =========================
ALTER TABLE model_gateway_model_manage
ADD COLUMN IF NOT EXISTS ref_system_model_id BIGINT DEFAULT NULL;
COMMENT ON COLUMN model_gateway_model_manage.ref_system_model_id
IS '引用的系统模型IDNULL=非引用行;system_model=false 且该列非空=引用行)';
CREATE INDEX IF NOT EXISTS idx_model_manage_ref_system_model_id
ON model_gateway_model_manage(ref_system_model_id);
-- =========================
-- 存量迁移:拷贝行 → 引用行(按同名系统模型匹配,一次性,幂等)
-- 前提:存量拷贝均原封不动、无自定义(用户已确认)。若存在用户独立创建的同名自有模型会被误转,执行前复核一次:
-- SELECT r.id, r.creator, r.model_name, r.ref_system_model_id, s.id AS sys_id
-- FROM model_gateway_model_manage r
-- JOIN model_gateway_model_manage s ON s.model_name = r.model_name AND s.system_model = true
-- WHERE r.system_model = false AND r.ref_system_model_id IS NOT NULL;
-- 列名以 entity orm 为准(update.sql 顶部旧 DDL 已过时)。
-- =========================
UPDATE model_gateway_model_manage r SET
ref_system_model_id = s.id,
base_url = NULL, http_method = NULL,
request_head_mapping = NULL, request_body_mapping = NULL,
request_business_field_mapping = NULL,
response_mapping = NULL, response_body_mapping = NULL,
response_business_field_mapping = NULL,
max_concurrency = 0, token_mapping = NULL, async_task_mapping = NULL,
token_predict_price = 0, token_predict_price_unit = NULL,
max_tokens = 0, min_duration = 0, max_duration = 0,
last_frame = NULL, error_message_mapping = NULL
FROM model_gateway_model_manage s
WHERE r.system_model = false AND s.system_model = true
AND r.model_name = s.model_name
AND r.ref_system_model_id IS NULL;