From b03173d61ef5bc152ca92a037b18fe7ba6731e8a Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 15 Jan 2020 23:02:42 -0600 Subject: [PATCH] favgroups: fix favgroups with invalid post ids. Fix favgroups containing post ids that don't exist. Most of these are from typos or from old expunged posts that weren't properly removed from favgroups. --- script/fixes/060_fix_favgroup_posts.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100755 script/fixes/060_fix_favgroup_posts.rb diff --git a/script/fixes/060_fix_favgroup_posts.rb b/script/fixes/060_fix_favgroup_posts.rb new file mode 100755 index 000000000..3528b82fd --- /dev/null +++ b/script/fixes/060_fix_favgroup_posts.rb @@ -0,0 +1,12 @@ +#!/usr/bin/env ruby + +require_relative "../../config/environment" + +favgroups = FavoriteGroup.select("*, unnest(string_to_array(post_ids, ' '))::bigint AS post_id") +favgroups = FavoriteGroup.select("*").from(favgroups).where("NOT EXISTS (SELECT 1 FROM posts WHERE id = post_id)") + +FavoriteGroup.transaction do + favgroups.each do |favgroup| + favgroup.remove!(favgroup.post_id) + end +end