Restore a long topic that has been deleted

On our forums, we had a long topic with over 300 posts with a commercial webdev client and for some reason, that entire topic vanished a few day ago after moving a few posts to a different topic.

I checked the user activity log, and the record did not have any entries for that now “missing” topic being deleted nor edited. The only action was a few posts (around 10) were moved to a new topic.

Today, I checked the Discourse DB and found all 306 posts were there, but when I checked the topic_id in those 306 posts, the topic with that id was empty [].

Let’s say the missing topic record was topic_id “300000” in the post records.

When I did this in the console, here was the results:

> Topic.with_deleted.where(id: 300000)
> []
> Post.where(topic_id:300000).count
> 306

I restored all these 306 post to a new topic; by creating a “Dummy” (Placeholder) topic in the Discourse UI and then changed all the topic_id (s) of the “orphan” posts to the new placeholder topic_id, as follows:

Say the new topic created (the dummy topic) has id: 304000, then:

Post.where(topic_id: 300000).update_all(topic_id: 304000)

This worked and restored all the 306 posts to the new placeholder/dummy topic id.

Then, I simply edited the title of the new topic and all looks good; and since we did not have any links to this long, private topic, there was no concern about breaking links with a new topic id, so all is good.

I have no idea why the topic record was empty and all the posts with that topic id were in tact. The records and logs show no action (delete, edit) on that topic. The only action on that topic was moving a handful of posts to a new topic, but that should not have “nulled out” the topic record.

Since I cannot repeat this (and am not sure what happened), I did not report this as a “bug”; but instead just waited until I had time today and did some research and found the 306 posts lost their parent topic “someone”; but this technique restored them all.

If I had of wanted to preserve the original topic id, I would have tried to restore the original topic record, but this seems to be a bit more work; and so I just assigned the 306 “topic-less” post to a new topic, and the results, as expected:

> Post.where(topic_id:304000).count
> 307

Hope this helps anyone who may encounter a similar situation in the future.

2 Likes