-- 原子 GPU 预留：评测和推理与训练统一纳入调度占用模型。
BEGIN;

CREATE TABLE IF NOT EXISTS gpu_reservations (
    id TEXT PRIMARY KEY,
    node_id TEXT NOT NULL REFERENCES compute_nodes(id) ON DELETE CASCADE,
    gpu_index INTEGER NOT NULL,
    owner_type TEXT NOT NULL CHECK (owner_type IN ('eval', 'inference')),
    owner_id TEXT NOT NULL,
    status TEXT NOT NULL DEFAULT 'reserved' CHECK (status IN ('reserved', 'released')),
    create_time TEXT NOT NULL,
    released_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_gpu_reservations_owner
    ON gpu_reservations(owner_type, owner_id, status);
CREATE INDEX IF NOT EXISTS idx_gpu_reservations_node_status
    ON gpu_reservations(node_id, status);
CREATE UNIQUE INDEX IF NOT EXISTS uq_gpu_reservations_active
    ON gpu_reservations(node_id, gpu_index) WHERE status = 'reserved';

COMMIT;
