# How to repair old titles?

**URL:** https://meta.discourse.org/t/how-to-repair-old-titles/76531
**Category:** Support
**Created:** [December 21, 2017, 2:38am UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531 "2017-12-21T02:38:11Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![terraboss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/terraboss/32/381157_2.png) [@terraboss](https://meta.discourse.org/u/terraboss)
#### Post date: [December 21, 2017, 2:38am UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/1 "2017-12-21T02:38:11Z")

</div>

Hello guys,  
I‘m not sure, if this [issue](https://meta.discourse.org/t/the-header-markdown-syntax-stop-working/67169/) was solved before?

I have a lot of old posts / wiki articles using titles like …

`#test`  
`##test`  
`###test`

As you might know, a few versions earlier this year, someone changed the behavior how to interpret these titles. Currently, the output isn’t one of these shiny markdown titles with bold text …

# test

… instead, it‘s just …

#text  
##test  
###test

How can I automatically fix this issue for all old articles and add the missing space between the hashtags and title text?

It becomes very frustrating to do this manually.

Any clue? Thanks in advance!

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [December 21, 2017, 10:30am UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/2 "2017-12-21T10:30:26Z")

</div>

I just [pushed a fix](https://github.com/discourse/discourse/commit/727a45185d40b6c2288877b0a9a42c62002fcf51) that should allow you to do this:

```bash
rake posts:remap['^(#+)([^#\\s].*)$','\\1 \\2','regex']

```

---

<div class="post-metadata">

### Author: ![terraboss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/terraboss/32/381157_2.png) [@terraboss](https://meta.discourse.org/u/terraboss)
#### Post date: [December 21, 2017, 12:35pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/3 "2017-12-21T12:35:15Z")

</div>

Thanks for your quick response.

Unfortunately, It didn’t worked for me. First I‘ve updated Discourse to the latest version via rebuild. Secondly, I‘ve executed the rake task.

This is how I end:

```
root@Ubuntu-1604-xenial-64-minimal-app:/var/www/discourse# rake posts:remap['^(#+)([^#\\s].*)$','\\1 \\2','regex'] --trace
** Invoke posts:remap (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute posts:remap
Are you sure you want to replace all regex occurrences of '^(#+)([^#\s].*)$' with '\1 \2'? (Y/n)
Y
Remapping
rake aborted!
NoMethodError: undefined method `acting_user=' for nil:NilClass
/var/www/discourse/lib/post_revisor.rb:132:in `revise!'
/var/www/discourse/app/models/post.rb:471:in `revise'
/var/www/discourse/lib/tasks/posts.rake:141:in `block in remap_posts'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/relation/batches.rb:63:in `block (2 levels) in find_each'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/relation/batches.rb:63:in `each'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/relation/batches.rb:63:in `block in find_each'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/relation/batches.rb:129:in `block in find_in_batches'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/relation/batches.rb:230:in `block in in_batches'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/relation/batches.rb:214:in `loop'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/relation/batches.rb:214:in `in_batches'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/relation/batches.rb:128:in `find_in_batches'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/relation/batches.rb:62:in `find_each'
/var/www/discourse/lib/tasks/posts.rake:133:in `remap_posts'
/var/www/discourse/lib/tasks/posts.rake:174:in `block in <top (required)>'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:251:in `block in execute'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:251:in `each'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:251:in `execute'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:195:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:188:in `invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:181:in `invoke'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:153:in `invoke_task'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:109:in `block (2 levels) in top_level'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:109:in `each'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:109:in `block in top_level'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:118:in `run_with_threads'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:103:in `top_level'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:81:in `block in run'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:179:in `standard_exception_handling'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:78:in `run'
bin/rake:4:in `<top (required)>'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:75:in `load'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:75:in `kernel_load'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/lib/bundler/cli.rb:424:in `exec'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/lib/bundler/cli.rb:27:in `dispatch'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/lib/bundler/cli.rb:18:in `start'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/exe/bundle:30:in `block in <top (required)>'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/lib/bundler/friendly_errors.rb:122:in `with_friendly_errors'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.16.1/exe/bundle:22:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => posts:remap

```

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [December 21, 2017, 1:01pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/4 "2017-12-21T13:01:04Z")

</div>

Hmm… looks like you found a post that doesn’t belong to a topic. 🤔

---

<div class="post-metadata">

### Author: ![terraboss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/terraboss/32/381157_2.png) [@terraboss](https://meta.discourse.org/u/terraboss)
#### Post date: [December 21, 2017, 4:41pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/5 "2017-12-21T16:41:20Z")

</div>

How can I fix this? 🤔

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [December 21, 2017, 6:26pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/6 "2017-12-21T18:26:35Z")

</div>

Will need to figure out how it came to be before it can be fixed.

If you have the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) plug-in installed that might help in figuring out what happened. You could also check out the Action Logs for the OP to see if it had been put into needs approval / Akismet queues, Flagged etc.

---

<div class="post-metadata">

### Author: ![terraboss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/terraboss/32/381157_2.png) [@terraboss](https://meta.discourse.org/u/terraboss)
#### Post date: [December 21, 2017, 11:44pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/7 "2017-12-21T23:44:35Z")

</div>

Isn’t it possible to skip this post without topic?

---

<div class="post-metadata">

### Author: ![terraboss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/terraboss/32/381157_2.png) [@terraboss](https://meta.discourse.org/u/terraboss)
#### Post date: [December 25, 2017, 1:45pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/8 "2017-12-25T13:45:14Z")

</div>

Merry X-mas dear friends,

(1) may I ask you: How do I use the [data explorer](https://meta.discourse.org/t/32566?silent=true)? I‘ve never been worked with it before. Which queries can I use to identify the issue?

(2) Action log: Which events are suspicious?

(3) Akismet: Nothing in the queue.

(4) Console output: Why are you thinking there is a post without topic? I didn’t read anything like that? Why is the rake task saying the first method „acting\_user“ is undefined?

Thanks in advance,  
Markus

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [December 25, 2017, 2:17pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/9 "2017-12-25T14:17:05Z")

</div>

Merry Christmas right back at ya!

The way I use the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) plugin is I have a query named “test” that I use for “alpha” queries and when I find one I like rename it to something more descriptive and save it then create another “test”.

To the right of the input area there is a list of Discourse tables. They can be expanded to show field names with their data types.

I have limited experience with different databases, so my SQL isn’t so bad. But I still check the postgreSQL documentation often

> **[PostgreSQL 18.4 Documentation](https://www.postgresql.org/docs/10/index.html)**
>
> PostgreSQL 18.4 Documentation The PostgreSQL Global Development Group Copyright © 1996–2026 The PostgreSQL Global Development Group Legal Notice Table of …

IMHO the plugin does a great job of letting you know when something is wrong with the query syntax. And it has safety built in in case you run a resource hungry query. However, similar to poor regex, you need to get things right. As an analogy, if you tell it “I wants watere” it will give you “plural used incorrectly, no such thing as watere” messages, but if you tell it “I want water” when you want bread, it will happily give you water.

I’m in the habit of explicitly SELECTing the fields I want so I don’t know if the “\*” wildcard will work, but it may. eg.

```plaintext
SELECT COUNT(id) 
FROM posts 
WHERE topic_id IS NULL 

```

should let you know how many of these you have.

---

<div class="post-metadata">

### Author: ![terraboss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/terraboss/32/381157_2.png) [@terraboss](https://meta.discourse.org/u/terraboss)
#### Post date: [December 25, 2017, 2:38pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/10 "2017-12-25T14:38:06Z")

</div>

Thank you very much! It seems the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) is a very mighty tool.

Unfortunately, I‘ve got as output count = 0. Looks like, there is something different wrong. 🤔

- Regarding to [this hint](https://meta.discourse.org/t/error-while-remaping-posts/53094/4) by @sam , I also did a rebuild of the app container. (no success)

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [December 28, 2017, 2:15pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/11 "2017-12-28T14:15:23Z")

</div>

> [@terraboss](#):
>
> Console output: Why are you thinking there is a post without topic? I didn’t read anything like that? Why is the rake task saying the first method „acting\_user“ is undefined?

It’s [failing to set the topic’s `acting_user`](https://github.com/discourse/discourse/blob/master/lib/post_revisor.rb#L132) because `@topic` is `nil`.

You could try the following [Data Explorer](https://meta.discourse.org/t/32566?silent=true) query to find the problematic posts.

```sql
SELECT *
FROM posts
WHERE NOT EXISTS(
    SELECT 1
    FROM topics
    WHERE posts.topic_id = topics.id
)

```

Anyway, I [changed the rake task to continue remapping](https://github.com/discourse/discourse/commit/81427e26ea7563c6a43148311130dfe5eccc9b17) when an error occurs.

---

<div class="post-metadata">

### Author: ![terraboss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/terraboss/32/381157_2.png) [@terraboss](https://meta.discourse.org/u/terraboss)
#### Post date: [December 28, 2017, 3:56pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/12 "2017-12-28T15:56:28Z")

</div>

Thank you very much, @gerhard! 🌹

When I try to open them, I‘m getting the message: The site ist not existing or private. And nearly everyone has `<p>...</p>` in common. That‘s intressting, cause I‘ve never put html code into these posts.

What is the best option:

1. Can I recover / repair them? It seems the content is still available.
2. If not, how can I delete them manually / automatically?

Some example:

 ![image](https://global.discourse-cdn.com/meta/original/3X/3/7/371885cbca3e5f79dd184cf1cee4a6415ff34ab6.jpeg)

Thank you in advance!

---

<div class="post-metadata">

### Author: ![terraboss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/terraboss/32/381157_2.png) [@terraboss](https://meta.discourse.org/u/terraboss)
#### Post date: [December 28, 2017, 4:24pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/13 "2017-12-28T16:24:05Z")

</div>

Yessssss 🎊🎄 ❤ Now it‘s working @gerhard, you are my hero 😊

---

<div class="post-metadata">

### Author: ![schungx](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/schungx/32/70989_2.png) [@schungx](https://meta.discourse.org/u/schungx)
#### Post date: [December 30, 2017, 4:42am UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/14 "2017-12-30T04:42:06Z")

</div>

Now you only have the one problem left… You have a post, or posts, that have incorrect topic\_id field set to a number that doesn’t correspond to a valid topic, or a topic that is abnormal.

If the topic\_id is invalid, then you have data inconsistencies in your database, which may trip up your other stuff in the future. You really need to get to the bottom of this.

If the topic\_id points to a valid topic, then it is simply something that the rake task hasn’t handled, which is safe.

---

<div class="post-metadata">

### Author: ![terraboss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/terraboss/32/381157_2.png) [@terraboss](https://meta.discourse.org/u/terraboss)
#### Post date: [December 30, 2017, 9:43am UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/15 "2017-12-30T09:43:06Z")

</div>

> [@schungx](#):
>
> If the topic\_id is invalid, then you have data inconsistencies in your database, which may trip up your other stuff in the future. You really need to get to the bottom of this.

For now, I’ve no clue how to fix this issue.  
It seems there is some data behind each topic but those entries can’t be restored or deleted.

Any suggestions how to solve this?

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [December 30, 2017, 11:11am UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/16 "2017-12-30T11:11:25Z")

</div>

Your screenshot gives the impression that only “About the category” topics are affected. Did you delete them at some point in time? I just gave it a try and it looks like deleting those topics isn’t possible anymore. Maybe it was in the past and there was a bug…

If you really want those posts gone, you could delete them in the Rails console (`rails c` inside the Docker container). This should work:

```ruby
Post.where("NOT EXISTS (SELECT 1 FROM topics WHERE posts.topic_id = topics.id)").delete_all

```

> [@schungx](#):
>
> If the topic\_id points to a valid topic, then it is simply something that the rake task hasn’t handled

No, it’s definitely not a problem with the rake task. The referenced topic IDs simply do not exist anymore.

---

<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: [June 8, 2024, 12:43pm UTC](https://meta.discourse.org/t/how-to-repair-old-titles/76531/17 "2024-06-08T12:43:46Z")

</div>

This topic was automatically closed after 2361 days. New replies are no longer allowed.
