FIX: Backfill topic timer duration (#9249)

FIX: Backfill topic timer duration (#9249)

diff --git a/app/services/topic_status_updater.rb b/app/services/topic_status_updater.rb
index f3aa541..795538c 100644
--- a/app/services/topic_status_updater.rb
+++ b/app/services/topic_status_updater.rb
@@ -89,7 +89,7 @@ TopicStatusUpdater = Struct.new(:topic, :user) do
   def message_for_autoclosed(locale_key)
     num_minutes =
       if @topic_status_update&.based_on_last_post
-        @topic_status_update.duration.hours
+        (@topic_status_update.duration || 0).hours
       elsif @topic_status_update&.created_at
         Time.zone.now - @topic_status_update.created_at
       else
diff --git a/db/migrate/20200320193612_back_fill_topic_timer_durations.rb b/db/migrate/20200320193612_back_fill_topic_timer_durations.rb
new file mode 100644
index 0000000..a417441
--- /dev/null
+++ b/db/migrate/20200320193612_back_fill_topic_timer_durations.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class BackFillTopicTimerDurations < ActiveRecord::Migration[6.0]
+  def up
+    DB.exec <<~SQL
+      UPDATE topic_timers
+      SET duration = 0
+      WHERE duration IS NULL AND (execute_at IS NULL OR created_at IS NULL)
+    SQL
+
+    DB.exec <<~SQL
+      UPDATE topic_timers
+      SET duration = EXTRACT(EPOCH FROM execute_at - created_at) / 3600
+      WHERE duration IS NULL AND execute_at IS NOT NULL AND created_at IS NOT NULL
+    SQL
+  end
+
+  def down
+  end
+end
diff --git a/lib/post_creator.rb b/lib/post_creator.rb
index 0e89a93..39fe22c 100644
--- a/lib/post_creator.rb
+++ b/lib/post_creator.rb
@@ -470,7 +470,7 @@ class PostCreator
 
       if topic_timer &&
          topic_timer.based_on_last_post &&
-         topic_timer.duration > 0
+         topic_timer.duration.to_i > 0
 
         @topic.set_or_create_timer(TopicTimer.types[:close],
           nil,

GitHub sha: c4bc734b

This commit appears in #9249 which was approved by vinothkannans. It was merged by vinothkannans.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/auto-close-topic-indefinite-internal-server-error/145189/4