Re-add usage of `add_directory_column` for user directory table solutions count (#140)

Re-add usage of add_directory_column for user directory table solutions count (#140)

diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index f66942c..7cffd18 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4,6 +4,7 @@ en:
       alt:
         solved:
           accepted_notification: "accepted"
+    solutions: "Solutions"
 
     solved:
       title: "Solved"
diff --git a/db/migrate/20210618142654_recreate_solutions_column.rb b/db/migrate/20210618142654_recreate_solutions_column.rb
new file mode 100644
index 0000000..0d30ad8
--- /dev/null
+++ b/db/migrate/20210618142654_recreate_solutions_column.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class RecreateSolutionsColumn < ActiveRecord::Migration[6.1]
+  def up
+    if !ActiveRecord::Base.connection.column_exists?(:directory_items, :solutions)
+      # A reverted commit had added this column previously so some sites have this
+      # column, and some so not. Only add if the DB doesn't already have it.
+      add_column :directory_items, :solutions, :integer, default: 0
+    end
+  end
+
+  def down
+    remove_column :directory_items, :solutions
+  end
+end
diff --git a/plugin.rb b/plugin.rb
index 78e4405..1173bcd 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -616,6 +616,26 @@ SQL
     options[:refresh_stream] = true if old_category_allows != new_category_allows
   end
 
+  query = "
+    WITH x AS (SELECT
+      u.id user_id,
+      COUNT(DISTINCT ua.id) AS solutions
+      FROM users AS u
+      LEFT OUTER JOIN user_actions AS ua ON ua.user_id = u.id AND ua.action_type = #{UserAction::SOLVED} AND COALESCE(ua.created_at, :since) > :since
+      WHERE u.active
+        AND u.silenced_till IS NULL
+        AND u.id > 0
+      GROUP BY u.id
+    )
+    UPDATE directory_items di SET
+      solutions = x.solutions
+    FROM x
+    WHERE x.user_id = di.user_id
+    AND di.period_type = :period_type
+    AND di.solutions <> x.solutions
+  "
+  add_directory_column("solutions", query: query)
+
   add_to_class(:composer_messages_finder, :check_topic_is_solved) do
     return if !SiteSetting.solved_enabled || SiteSetting.disable_solved_education_message
     return if !replying? || @topic.blank? || @topic.private_message?

GitHub sha: 52d46560111267698f370efd1effe65f17bc8370

This commit appears in #140 which was approved by eviltrout. It was merged by markvanlan.