ALTER TABLE workshop_bookings
  ADD KEY idx_workshop_booking_slot (slot_id),
  DROP INDEX uniq_workshop_booking_slot,
  ADD COLUMN active_slot_id CHAR(36) CHARACTER SET ascii COLLATE ascii_bin
    GENERATED ALWAYS AS (
      CASE WHEN status = 'cancelled' THEN NULL ELSE slot_id END
    ) STORED AFTER status,
  ADD UNIQUE KEY uniq_workshop_booking_active_slot (active_slot_id);

INSERT INTO migrations (migration, batch)
SELECT '005_workshop_payment_lifecycle', COALESCE(MAX(batch), 0) + 1
FROM migrations
WHERE NOT EXISTS (
  SELECT 1 FROM migrations WHERE migration = '005_workshop_payment_lifecycle'
);
