DEV: Avoid nil custom fields (#128)

DEV: Avoid nil custom fields (#128)

diff --git a/db/post_migrate/20210429154322_remove_nil_custom_fields_from_solved.rb b/db/post_migrate/20210429154322_remove_nil_custom_fields_from_solved.rb
new file mode 100644
index 0000000..bdca8c2
--- /dev/null
+++ b/db/post_migrate/20210429154322_remove_nil_custom_fields_from_solved.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class RemoveNilCustomFieldsFromSolved < ActiveRecord::Migration[6.0]
+  def up
+    execute <<~SQL
+      DELETE FROM post_custom_fields
+      WHERE name = 'is_accepted_answer' AND value IS NULL
+    SQL
+
+    execute <<~SQL
+      DELETE FROM topic_custom_fields
+      WHERE name = 'accepted_answer_post_id' AND value IS NULL
+    SQL
+
+    execute <<~SQL
+      DELETE FROM topic_custom_fields
+      WHERE name = 'solved_auto_close_topic_timer_id' AND value IS NULL
+    SQL
+  end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration
+  end
+end
diff --git a/plugin.rb b/plugin.rb
index 7dcc639..8a74ac4 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -83,7 +83,7 @@ SQL
 
         if accepted_id > 0
           if p2 = Post.find_by(id: accepted_id)
-            p2.custom_fields["is_accepted_answer"] = nil
+            p2.custom_fields.delete("is_accepted_answer")
             p2.save!
 
             if defined?(UserAction::SOLVED)
@@ -168,13 +168,13 @@ SQL
       topic ||= post.topic
 
       DistributedMutex.synchronize("discourse_solved_toggle_answer_#{topic.id}") do
-        post.custom_fields["is_accepted_answer"] = nil
-        topic.custom_fields["accepted_answer_post_id"] = nil
+        post.custom_fields.delete("is_accepted_answer")
+        topic.custom_fields.delete("accepted_answer_post_id")
 
         if timer_id = topic.custom_fields[AUTO_CLOSE_TOPIC_TIMER_CUSTOM_FIELD]
           topic_timer = TopicTimer.find_by(id: timer_id)
           topic_timer.destroy! if topic_timer
-          topic.custom_fields[AUTO_CLOSE_TOPIC_TIMER_CUSTOM_FIELD] = nil
+          topic.custom_fields.delete(AUTO_CLOSE_TOPIC_TIMER_CUSTOM_FIELD)
         end
 
         topic.save!

GitHub sha: ce63d80a

This commit appears in #128 which was approved by tgxworld. It was merged by CvX.