-- One-time cleanup for the loadDocumentation() bug (see
-- inc/functions.php's loadDocumentation()) that inserted a documentation
-- row twice whenever a slot had both a file and a detail filled in. For
-- each group of rows sharing the same appeal/appeal_level_2/file/detail,
-- keeps only the lowest id and deletes the rest. Uses <=> (NULL-safe
-- equality) throughout since appeal_level_2 and detail (and rarely file)
-- can be NULL.
--
-- Apply to every environment's database once (re-running is a no-op, since
-- there's nothing left to delete after the first run).

DELETE d1 FROM `documentation` d1
INNER JOIN `documentation` d2
  ON d1.appeal = d2.appeal
  AND (d1.appeal_level_2 <=> d2.appeal_level_2)
  AND (d1.file <=> d2.file)
  AND (d1.detail <=> d2.detail)
  AND d1.id > d2.id;
