-- Remove duplicate images, keeping only the most recent one per (filename, location_target, data length)
-- This handles upload accidents where same image was uploaded multiple times

-- First, identify duplicates
-- SELECT filename, location_target, COUNT(*) as dupes, array_agg(id) as ids 
-- FROM images GROUP BY filename, location_target, LENGTH(data) HAVING COUNT(*) > 1;

-- Delete duplicates, keeping the latest (highest id) per group
DELETE FROM images a
USING images b
WHERE a.id < b.id
  AND a.filename = b.filename
  AND a.location_target = b.location_target
  AND LENGTH(a.data) = LENGTH(b.data);

-- Verify cleanup
SELECT filename, location_target, COUNT(*) as count
FROM images
GROUP BY filename, location_target, LENGTH(data)
HAVING COUNT(*) > 1;