-- ============================================================
-- 权限体系扩展：GPU 分配表 + 资源所有权字段
-- ============================================================

-- GPU 分配表：管理员指定哪些用户可以使用哪些 GPU 卡
CREATE TABLE IF NOT EXISTS gpu_assignments (
    id          TEXT PRIMARY KEY,
    node_id     TEXT NOT NULL REFERENCES compute_nodes(id) ON DELETE CASCADE,
    gpu_index   INTEGER NOT NULL,
    user_id     TEXT NOT NULL,
    assigned_by TEXT,
    assigned_at TEXT NOT NULL,
    UNIQUE (node_id, gpu_index, user_id)
);
CREATE INDEX IF NOT EXISTS idx_gpu_assignments_user ON gpu_assignments(user_id);
CREATE INDEX IF NOT EXISTS idx_gpu_assignments_gpu ON gpu_assignments(node_id, gpu_index);

-- 资源所有权字段：用户创建的数据集/模型/训练产物/评测任务
ALTER TABLE datasets ADD COLUMN IF NOT EXISTS created_by TEXT;
ALTER TABLE models ADD COLUMN IF NOT EXISTS created_by TEXT;
ALTER TABLE trained_models ADD COLUMN IF NOT EXISTS created_by TEXT;
ALTER TABLE eval_tasks ADD COLUMN IF NOT EXISTS created_by TEXT;
