-- Run on each school database that already has staff_attendance_enrollments.
-- Skip the ALTER if storage_type already exists.

ALTER TABLE `staff_attendance_enrollments`
  ADD COLUMN `storage_type` VARCHAR(32) NOT NULL DEFAULT 'backblaze' AFTER `photo_object_key`;

-- Keep the newest current row per staff_id. Delete replaced history and any extra rows.

DELETE `e`
FROM `staff_attendance_enrollments` AS `e`
LEFT JOIN (
  SELECT `staff_id`, MAX(`enrollment_id`) AS `keep_id`
  FROM `staff_attendance_enrollments`
  WHERE `status` = 'current'
  GROUP BY `staff_id`
) AS `keep_row` ON `keep_row`.`keep_id` = `e`.`enrollment_id`
WHERE `keep_row`.`keep_id` IS NULL;
