From 7034a960484b54c593d89e3bbc72bea48e2395e4 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 20 Nov 2017 12:13:28 -0600 Subject: [PATCH] Purge invalid gentags from tags table (#3390). Purges gentags with invalid names from the tags table. Addresses #3390 (Searching certain metatags results in an empty paginator) by removing metatags from the tags table. --- app/models/tag.rb | 4 ++++ script/fixes/051_purge_invalid_tags.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100755 script/fixes/051_purge_invalid_tags.rb diff --git a/app/models/tag.rb b/app/models/tag.rb index a71ec7807..e7c33519b 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -783,6 +783,10 @@ class Tag < ApplicationRecord end module SearchMethods + def empty + where("tags.post_count <= 0") + end + def nonempty where("tags.post_count > 0") end diff --git a/script/fixes/051_purge_invalid_tags.rb b/script/fixes/051_purge_invalid_tags.rb new file mode 100755 index 000000000..1c168ab7f --- /dev/null +++ b/script/fixes/051_purge_invalid_tags.rb @@ -0,0 +1,20 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')) + +CurrentUser.user = User.system +CurrentUser.ip_addr = "127.0.0.1" + +Tag.transaction do + empty_gentags = Tag.empty.where(category: Tag.categories.general) + total = empty_gentags.count + + empty_gentags.find_each.with_index do |tag, i| + STDERR.puts %{validating "#{tag.name}" (#{i}/#{total})} if i % 1000 == 0 + + if tag.invalid?(:create) + # puts ({ name: tag.name, id: tag.id }).to_json + tag.delete + end + end +end