about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2019-11-18 01:43:18 -0600
committermultiple creatures <dev@multiple-creature.party>2019-11-18 02:19:09 -0600
commit54afd828c2defddfc57faca2f6ea9052dad5bfa4 (patch)
treefc0999ff7834caef710d9c9742790ba3fda9d330 /lib
parent4d6e1aa81de57d5102340b42d267d8fbf0073eec (diff)
Split indexing Rake tasks to: `monsterfork:index_statuses` (reindex statuses that do not normalized text yet), `monsterfork:reindex_statuses` (reindex all statuses), and `monsterfork:reindex_media_desc` (reindex statuses with media descriptions). These tasks are only needed by admins setting up Monsterfork for the first time or if the normalization scheme has changed drastically.
Diffstat (limited to 'lib')
-rw-r--r--lib/tasks/monsterfork.rake50
1 files changed, 33 insertions, 17 deletions
diff --git a/lib/tasks/monsterfork.rake b/lib/tasks/monsterfork.rake
index efc45b2fe..465299562 100644
--- a/lib/tasks/monsterfork.rake
+++ b/lib/tasks/monsterfork.rake
@@ -1,24 +1,40 @@
-namespace :monsterfork do
-  desc '(Re-)Index statuses for search.'
-  task index_statuses: :environment do
-    include TextHelper
+# frozen_string_literal: true
+
+def index_statuses(statuses_query)
+  include TextHelper
 
-    i = 0
-    total = Status.count
+  i = 0
+  total = statuses_query.count
 
-    Status.find_in_batches do |statuses|
-      ActiveRecord::Base.logger.info("Indexing status #{1+i} of #{total}.")
-      ActiveRecord::Base.logger.silence do
-        i += statuses.count
-        statuses.each do |s|
-          begin
-            next if s.destroyed?
-            s.update_column(:normalized_text, normalize_status(s))
-          rescue ActiveRecord::RecordNotFound
-            true
-          end
+  statuses_query.find_in_batches do |statuses|
+    ActiveRecord::Base.logger.info("Indexing status #{1+i} of #{total}.")
+    ActiveRecord::Base.logger.silence do
+      i += statuses.count
+      statuses.each do |s|
+        begin
+          next if s.destroyed?
+          s.update_column(:normalized_text, normalize_status(s))
+        rescue ActiveRecord::RecordNotFound
+          true
         end
       end
     end
   end
 end
+
+namespace :monsterfork do
+  desc 'Index statuses for search that have not been indexed yet.'
+  task index_statuses: :environment do
+    index_statuses(Status.where(normalized_text: ''))
+  end
+
+  desc 'Reindex all statuses for search.'
+  task reindex_statuses: :environment do
+    index_statuses(Status)
+  end
+
+  desc 'Reindex statuses containing media with descriptions for search.'
+  task reindex_media_descs: :environment do
+    index_statuses(Status.left_outer_joins(:media_attachments).where('media_attachments.description IS NOT NULL'))
+  end
+end