-- AlterTable: an encounter can be put away, and has a place in the campaign's own order ALTER TABLE "encounters" ADD COLUMN "archivedAt" TIMESTAMP(3); ALTER TABLE "encounters" ADD COLUMN "sortOrder" INTEGER NOT NULL DEFAULT 0; -- Existing encounters keep the order the list has always shown: newest first. UPDATE "encounters" AS e SET "sortOrder" = r.pos FROM ( SELECT id, ROW_NUMBER() OVER (PARTITION BY "campaignId" ORDER BY "createdAt" DESC, id) - 1 AS pos FROM "encounters" ) AS r WHERE e.id = r.id;