-- Milestone publishing retries. Run once against the target database before
-- deploying the application version that uses these columns.
--
--   docker-compose exec db psql -U postgres -d qrmaster -f - < sql/2026-08-16_social_milestone_retries.sql
--
-- A failed X post used to stay failed forever: the consent dialog only opens
-- for freshly detected milestones, so nobody ever saw the retry button again.
-- The publisher now re-queues a failed attempt on its own until it runs out of
-- attempts, and the customer can retry a permanently failed post from Settings.
ALTER TABLE "SocialMilestone" ADD COLUMN IF NOT EXISTS "attempts" INTEGER NOT NULL DEFAULT 0;
ALTER TABLE "SocialMilestone" ADD COLUMN IF NOT EXISTS "nextAttemptAt" TIMESTAMP(3);

-- The publisher polls for the oldest approved milestone that is due.
CREATE INDEX IF NOT EXISTS "SocialMilestone_brandStatus_brandApprovedAt_idx"
  ON "SocialMilestone" ("brandStatus", "brandApprovedAt");
