-- name: CreateProcessingJob :one INSERT INTO processing_jobs ( company_id, user_id, status, total_products, processing_type, priority, estimated_tokens ) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *; -- name: GetProcessingJob :one SELECT * FROM processing_jobs WHERE id = $1 AND company_id = $2 LIMIT 1; -- name: ListProcessingJobs :many SELECT * FROM processing_jobs WHERE company_id = $1 ORDER BY created_at DESC LIMIT $2; -- name: UpdateProcessingJobStatus :one UPDATE processing_jobs SET status = $2, processed_products = COALESCE($3, processed_products), error = COALESCE($4, error), started_at = COALESCE($5, started_at), completed_at = COALESCE($6, completed_at), updated_at = now() WHERE id = $1 RETURNING *; -- name: CancelProcessingJob :one UPDATE processing_jobs SET status = 'cancelled', completed_at = now(), updated_at = now() WHERE id = $1 AND company_id = $2 AND status IN ('pending', 'running') RETURNING *; -- name: CreateProcessingJobProduct :one INSERT INTO processing_job_products (job_id, raw_product_id, status) VALUES ($1, $2, $3) RETURNING *; -- name: ListPendingJobProducts :many SELECT * FROM processing_job_products WHERE job_id = $1 AND status = 'pending' ORDER BY created_at LIMIT $2; -- name: UpdateJobProductStatus :one UPDATE processing_job_products SET status = $2, error = $3, processed_product_id = $4, updated_at = now() WHERE id = $1 RETURNING *; -- name: ClaimNextPendingJob :one UPDATE processing_jobs SET status = 'running', started_at = now(), updated_at = now() WHERE id = ( SELECT id FROM processing_jobs WHERE status = 'pending' ORDER BY priority DESC, created_at LIMIT 1 FOR UPDATE SKIP LOCKED ) RETURNING *;