-- Per-user FIFO queue order for pending guess assignments (defer pushes to back).

ALTER TABLE market_history_prospective_assignments
  ADD COLUMN IF NOT EXISTS view_order_at TIMESTAMPTZ;

UPDATE market_history_prospective_assignments
SET view_order_at = created_at
WHERE view_order_at IS NULL;

ALTER TABLE market_history_prospective_assignments
  ALTER COLUMN view_order_at SET NOT NULL,
  ALTER COLUMN view_order_at SET DEFAULT now();

CREATE INDEX IF NOT EXISTS market_history_prospective_assignments_user_view_order_idx
  ON market_history_prospective_assignments (assigned_user_id, view_order_at ASC)
  WHERE status = 'pending';
