Files
taiji-AI-PAD/services/mcp-server/migrations/008_add_quota_management.sql
T

133 lines
5.2 KiB
PL/PgSQL

-- 阶段二:配额管理和申请审批数据库迁移
-- 版本: 008
-- 日期: 2026-01-05
-- 说明: 添加资源申请、平台Agent配额和Agent计费记录表
-- 1. 资源申请表(统一的申请审批)
CREATE TABLE IF NOT EXISTS resource_applications (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
channel_id UUID NOT NULL REFERENCES channels(id),
-- 申请类型
resource_type VARCHAR(30) NOT NULL, -- platform_agent, provider, custom_agent_quota
-- 平台 Agent 申请字段
template_name VARCHAR(100),
requested_pod_quota INTEGER,
-- 模型供应商申请字段
provider_id UUID REFERENCES model_providers(id),
requested_rpm INTEGER,
requested_tpm INTEGER,
-- 自定义 Agent 资源申请字段
requested_cpu_quota NUMERIC(12, 2),
requested_memory_quota NUMERIC(12, 2),
-- 申请信息
reason TEXT NOT NULL,
-- 审批状态
status VARCHAR(20) DEFAULT 'pending', -- pending, approved, rejected
-- 审批结果
approved_pod_quota INTEGER,
approved_rpm INTEGER,
approved_tpm INTEGER,
approved_cpu_quota NUMERIC(12, 2),
approved_memory_quota NUMERIC(12, 2),
-- 审批信息
reviewed_by UUID REFERENCES users(id),
review_reason TEXT,
reviewed_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_resource_app_channel ON resource_applications(channel_id);
CREATE INDEX IF NOT EXISTS idx_resource_app_type ON resource_applications(resource_type);
CREATE INDEX IF NOT EXISTS idx_resource_app_status ON resource_applications(status);
CREATE INDEX IF NOT EXISTS idx_resource_app_created ON resource_applications(created_at);
-- 2. 平台 Agent 配额表
CREATE TABLE IF NOT EXISTS platform_agent_quotas (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
target_id UUID NOT NULL,
target_type VARCHAR(20) NOT NULL, -- channel, tenant
template_name VARCHAR(100) NOT NULL,
pod_quota INTEGER NOT NULL DEFAULT 0,
pod_used INTEGER DEFAULT 0,
allocated_by UUID REFERENCES users(id),
allocated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(target_id, target_type, template_name)
);
CREATE INDEX IF NOT EXISTS idx_platform_agent_quota_target ON platform_agent_quotas(target_id, target_type);
CREATE INDEX IF NOT EXISTS idx_platform_agent_quota_template ON platform_agent_quotas(template_name);
-- 3. Agent 计费记录表
CREATE TABLE IF NOT EXISTS agent_billing_records (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id),
channel_id UUID REFERENCES channels(id),
agent_name VARCHAR(100) NOT NULL,
agent_type VARCHAR(20) NOT NULL, -- platform, custom
template_name VARCHAR(100) NOT NULL,
duration_seconds INTEGER NOT NULL,
cpu_seconds FLOAT DEFAULT 0,
memory_gb_seconds FLOAT DEFAULT 0,
request_count INTEGER DEFAULT 0,
cost NUMERIC(12, 4) NOT NULL,
currency VARCHAR(10) DEFAULT 'EU',
period_start TIMESTAMP NOT NULL,
period_end TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_agent_billing_user ON agent_billing_records(user_id);
CREATE INDEX IF NOT EXISTS idx_agent_billing_channel ON agent_billing_records(channel_id);
CREATE INDEX IF NOT EXISTS idx_agent_billing_period ON agent_billing_records(period_start, period_end);
CREATE INDEX IF NOT EXISTS idx_agent_billing_type ON agent_billing_records(agent_type);
-- 4. 添加触发器更新 updated_at
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ language 'plpgsql';
DROP TRIGGER IF EXISTS update_resource_applications_updated_at ON resource_applications;
CREATE TRIGGER update_resource_applications_updated_at
BEFORE UPDATE ON resource_applications
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
DROP TRIGGER IF EXISTS update_platform_agent_quotas_updated_at ON platform_agent_quotas;
CREATE TRIGGER update_platform_agent_quotas_updated_at
BEFORE UPDATE ON platform_agent_quotas
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
DROP TRIGGER IF EXISTS update_agent_billing_records_updated_at ON agent_billing_records;
CREATE TRIGGER update_agent_billing_records_updated_at
BEFORE UPDATE ON agent_billing_records
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- 5. 添加注释
COMMENT ON TABLE resource_applications IS '资源申请表(统一的申请审批)';
COMMENT ON TABLE platform_agent_quotas IS '平台 Agent 配额分配表';
COMMENT ON TABLE agent_billing_records IS 'Agent 计费记录表';
COMMENT ON COLUMN resource_applications.resource_type IS '申请类型: platform_agent, provider, custom_agent_quota';
COMMENT ON COLUMN resource_applications.status IS '审批状态: pending, approved, rejected';
COMMENT ON COLUMN platform_agent_quotas.target_type IS '分配目标类型: channel, tenant';
COMMENT ON COLUMN agent_billing_records.agent_type IS 'Agent 类型: platform, custom';