Skip to content

Instantly share code, notes, and snippets.

@sam
Created May 7, 2012 17:23
Show Gist options
  • Save sam/2629092 to your computer and use it in GitHub Desktop.
Save sam/2629092 to your computer and use it in GitHub Desktop.
Remove bad photos from database
psql -h db.example -c <<"EOS" example_client_database
CREATE OR REPLACE FUNCTION delete_photo(uuid) RETURNS INTEGER AS $$
DECLARE
updated_photos_count INTEGER;
updated_photo_translations_count INTEGER;
updated_channels_photos_count INTEGER;
BEGIN
DELETE FROM photos WHERE uuid = $1;
GET DIAGNOSTICS updated_photos_count = ROW_COUNT;
DELETE FROM photo_translations WHERE photo_uuid = $1;
GET DIAGNOSTICS updated_photo_translations_count = ROW_COUNT;
DELETE FROM channels_photos WHERE photo_uuid = $1;
GET DIAGNOSTICS updated_channels_photos_count = ROW_COUNT;
RETURN updated_photos_count + updated_photo_translations_count + updated_channels_photos_count;
END;
$$ LANGUAGE plpgsql;
EOS
psql -h db.example -c "CREATE TABLE present_photos (uuid uuid PRIMARY KEY (uuid));" example_client_database
find private/photos -name *-original.jpg -exec basename {} \; | awk '{gsub(/-original.jpg/, "")}1' | tee photo-uuids.txt
while read uuid; do psql -h db.example -c "INSERT INTO present_photos VALUES ('$uuid');" example_client_database; done < photo-uuids.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment