-- AlterTable
ALTER TABLE "Warning" ADD COLUMN "warningNumber" INTEGER;

-- Backfill per-guild sequential numbers (oldest first)
WITH numbered AS (
  SELECT id, ROW_NUMBER() OVER (PARTITION BY "guildId" ORDER BY "createdAt" ASC, id ASC) AS rn
  FROM "Warning"
)
UPDATE "Warning" AS w
SET "warningNumber" = numbered.rn
FROM numbered
WHERE w.id = numbered.id;

ALTER TABLE "Warning" ALTER COLUMN "warningNumber" SET NOT NULL;

CREATE UNIQUE INDEX "Warning_guildId_warningNumber_key" ON "Warning"("guildId", "warningNumber");
