Änderungszeitstempel rückgängig machen

Einer unserer Mods hat den Zeitstempel geändert, um ein Thema unbumpen zu können, und ich wollte diese Änderung rückgängig machen. Dafür habe ich eine Rake-Aufgabe geschrieben. Es ist zwar eine Kleinigkeit, aber warum nicht teilen?

# /var/www/discourse/lib/tasks/set_timestamp_to_last_version.rake
desc "Zeitstempel aller Beiträge in einem Thema auf die last_version_at des Beitrags setzen"
task "set_timestamp_to_last_version", [:topic_id] => :environment do |_, args|
  topic_id = args[:topic_id]
  if !topic_id
    puts "FEHLER: Erwartet wird rake set_timestamp_to_last_version[topic_id]"
    exit 1
  end

  Post.where(topic_id: topic_id).each do |p|
    p.created_at = p.last_version_at
    p.updated_at = p.last_version_at
    if !p.save
      puts "Fehler beim Speichern des Beitrags #{p.id}"
    end
  end

  p_first = Post.where(topic_id: topic_id).first
  p_last = Post.where(topic_id: topic_id).last

  t = Topic.where(id: topic_id).first
  t.created_at = p_first.last_version_at
  t.updated_at = p_last.last_version_at
  t.bumped_at = p_last.last_version_at
  t.last_posted_at = p_last.last_version_at
  if !t.save
    puts "Fehler beim Speichern des Themas #{t.id}"
  end
end
7 „Gefällt mir“

@zogstrip thoughts on pr-welcome? It’d be great for this to be on Github instead of needing to copy/paste from here if @yanokwa is willing to write one.

Does that happen often? I think it’s the first time I’ve seen someone undoing it. Also, why not change the timestamp back via the UI? What’s missing?

5 „Gefällt mir“

I only have a vague recollection of what went wrong, but I’ll try…

In Dec 2017, we accidentally moved our introductions topic (started in Jun 2017 and has daily posts) to Dec 2017. And it did what was expected in that it moved the first post to Dec 2017 and added the other posts sequentially. But it looked funny, in that all the posts had been squished into Dec 2017. I tried to move things back to Jun 2017, but then the squishing put al the posts into Jun 2017 instead of spreading them from Jun to Dec.

Again, this is what I think happened. Either way, I didn’t have time to figure it all out, so I wanted to restore the posts to their original date and when I poked around in the DB, this is what I came up with.

As far as things that would be good to add to the UI, I’d say:

  • If this squishing is reproducible, allow an undo of a timestamp change in the way I’ve done above.
  • A cleaner way to bump and unbump topics
2 „Gefällt mir“

3 Beiträge wurden in ein neues Thema aufgeteilt: Wie verwende ich die benutzerdefinierte Rake-Aufgabe zum Wiederherstellen von geänderten Zeitstempeln?