-- Fix sojorn-media URLs (for images) to img.sojorn.net
UPDATE profiles 
SET avatar_url = REGEXP_REPLACE(avatar_url, 'https://[a-zA-Z0-9]+\.r2\.cloudflarestorage\.com/sojorn-media/', 'https://img.sojorn.net/', 'g') 
WHERE avatar_url LIKE '%r2.cloudflarestorage.com/sojorn-media%';

UPDATE profiles 
SET cover_url = REGEXP_REPLACE(cover_url, 'https://[a-zA-Z0-9]+\.r2\.cloudflarestorage\.com/sojorn-media/', 'https://img.sojorn.net/', 'g') 
WHERE cover_url LIKE '%r2.cloudflarestorage.com/sojorn-media%';

UPDATE posts 
SET image_url = REGEXP_REPLACE(image_url, 'https://[a-zA-Z0-9]+\.r2\.cloudflarestorage\.com/sojorn-media/', 'https://img.sojorn.net/', 'g') 
WHERE image_url LIKE '%r2.cloudflarestorage.com/sojorn-media%';

UPDATE posts 
SET thumbnail_url = REGEXP_REPLACE(thumbnail_url, 'https://[a-zA-Z0-9]+\.r2\.cloudflarestorage\.com/sojorn-media/', 'https://img.sojorn.net/', 'g') 
WHERE thumbnail_url LIKE '%r2.cloudflarestorage.com/sojorn-media%';

-- Fix sojorn-videos URLs (for quips) to quips.sojorn.net
UPDATE posts 
SET video_url = REGEXP_REPLACE(video_url, 'https://[a-zA-Z0-9]+\.r2\.cloudflarestorage\.com/sojorn-videos/', 'https://quips.sojorn.net/', 'g') 
WHERE video_url LIKE '%r2.cloudflarestorage.com/sojorn-videos%';

-- Fix the one edge case where image_url contains a video URL
UPDATE posts 
SET image_url = REGEXP_REPLACE(image_url, 'https://[a-zA-Z0-9]+\.r2\.cloudflarestorage\.com/sojorn-videos/', 'https://quips.sojorn.net/', 'g') 
WHERE image_url LIKE '%r2.cloudflarestorage.com/sojorn-videos%';

-- Verify after
SELECT count(id) as remaining_bad_urls FROM posts WHERE image_url LIKE '%r2.cloudflarestorage.com%' OR video_url LIKE '%r2.cloudflarestorage.com%' OR thumbnail_url LIKE '%r2.cloudflarestorage.com%';
SELECT count(id) as remaining_bad_profiles FROM profiles WHERE avatar_url LIKE '%r2.cloudflarestorage.com%' OR cover_url LIKE '%r2.cloudflarestorage.com%';
