-- Migration 004: Project locks for session exclusivity
-- Prevents multiple Claude sessions from working on the same project

CREATE TABLE IF NOT EXISTS project_locks (
    project TEXT PRIMARY KEY REFERENCES projects(key) ON DELETE CASCADE,
    session_id TEXT NOT NULL,
    locked_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    expires_at TIMESTAMP WITH TIME ZONE,
    reason TEXT
);

CREATE INDEX IF NOT EXISTS idx_locks_session ON project_locks(session_id);
CREATE INDEX IF NOT EXISTS idx_locks_expires ON project_locks(expires_at);

-- Record migration
INSERT INTO schema_migrations (version, applied_at) VALUES ('004_project_locks', NOW())
ON CONFLICT DO NOTHING;
