-- AlterTable
ALTER TABLE "court_bookings" ADD COLUMN "booking_code" VARCHAR(8);

-- Backfill existing rows
UPDATE "court_bookings"
SET "booking_code" = UPPER(SUBSTRING(REPLACE("id"::text, '-', '') FROM 1 FOR 8))
WHERE "booking_code" IS NULL;

-- Enforce not-null and uniqueness
ALTER TABLE "court_bookings" ALTER COLUMN "booking_code" SET NOT NULL;
CREATE UNIQUE INDEX "court_bookings_booking_code_key" ON "court_bookings"("booking_code");
