# Undo change timestamp

**URL:** https://meta.discourse.org/t/undo-change-timestamp/78966
**Category:** Sysadmins
**Tags:** how-to
**Created:** [January 25, 2018, 3:18pm UTC](https://meta.discourse.org/t/undo-change-timestamp/78966 "2018-01-25T15:18:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![yanokwa](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yanokwa/32/105083_2.png) [@yanokwa](https://meta.discourse.org/u/yanokwa)
#### Post date: [January 25, 2018, 3:18pm UTC](https://meta.discourse.org/t/undo-change-timestamp/78966/1 "2018-01-25T15:18:30Z")

</div>

One of our mods changed the timestamp as a way to unbump a topic and I wanted to revert that change because. I wrote a rake task that does this. It’s a small thing, but why not share?

```ruby
# /var/www/discourse/lib/tasks/set_timestamp_to_last_version.rake
desc "Set timestamp of all posts in a topic to the post's last_version_at"
task "set_timestamp_to_last_version", [:topic_id] => :environment do |_, args|
  topic_id = args[:topic_id]
  if !topic_id
    puts "ERROR: Expecting 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 "post save error on #{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 "post save error on #{t.id}"
  end
end

```

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jomaxro/32/126216_2.png) [@jomaxro](https://meta.discourse.org/u/jomaxro)
#### Post date: [January 25, 2018, 3:23pm UTC](https://meta.discourse.org/t/undo-change-timestamp/78966/2 "2018-01-25T15:23:03Z")

</div>

@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.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [January 25, 2018, 3:28pm UTC](https://meta.discourse.org/t/undo-change-timestamp/78966/3 "2018-01-25T15:28:13Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![yanokwa](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yanokwa/32/105083_2.png) [@yanokwa](https://meta.discourse.org/u/yanokwa)
#### Post date: [January 25, 2018, 4:54pm UTC](https://meta.discourse.org/t/undo-change-timestamp/78966/4 "2018-01-25T16:54:06Z")

</div>

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

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [May 26, 2024, 12:12pm UTC](https://meta.discourse.org/t/undo-change-timestamp/78966/5 "2024-05-26T12:12:41Z")

</div>

3 posts were split to a new topic: [How to use restore changed timestamp custom rake task?](https://meta.discourse.org/t/how-to-use-restore-changed-timestamp-custom-rake-task/309450)
