-- Migration: Add source tracking columns to worker_tasks
-- Purpose: Track where tasks originated from (schedule, API, manual)

-- Add source tracking columns
ALTER TABLE worker_tasks ADD COLUMN IF NOT EXISTS source VARCHAR(50);
ALTER TABLE worker_tasks ADD COLUMN IF NOT EXISTS source_schedule_id INTEGER REFERENCES task_schedules(id);
ALTER TABLE worker_tasks ADD COLUMN IF NOT EXISTS source_metadata JSONB;

-- Index for tracking tasks by schedule
CREATE INDEX IF NOT EXISTS idx_worker_tasks_source_schedule
ON worker_tasks (source_schedule_id) WHERE source_schedule_id IS NOT NULL;

-- Comments
COMMENT ON COLUMN worker_tasks.source IS 'Origin of task: schedule, api, manual, chain';
COMMENT ON COLUMN worker_tasks.source_schedule_id IS 'ID of schedule that created this task';
COMMENT ON COLUMN worker_tasks.source_metadata IS 'Additional metadata about task origin';
