# Global Replace across database

**URL:** https://meta.discourse.org/t/global-replace-across-database/55930
**Category:** Support
**Created:** [January 17, 2017, 10:06pm UTC](https://meta.discourse.org/t/global-replace-across-database/55930 "2017-01-17T22:06:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [January 17, 2017, 10:06pm UTC](https://meta.discourse.org/t/global-replace-across-database/55930/1 "2017-01-17T22:06:00Z")

</div>

On a new importer, I included a wonder feature that includes a link to the post on the original site so that it’s easy to see what the post looked like on the old system to make it easier to find problems. It’s great.

But I screwed up and did the full import (~2.3 million posts) leaving that link in the first post in every topic. I’d love to be able to strip from all those topics and not have to re-run the script from scratch.

```
\[ORIGINAL POST\]\(.*t\)

```

There was discussion [here](https://meta.discourse.org/t/replace-a-string-in-all-posts/48729) about doing that, but it doesn’t do regexes. Maybe I just want to live on the edge and do it from the console?

Something like this, but remove the `Regex.escape`?

> <https://github.com/discourse/discourse/blob/main/lib/tasks/posts.rake#L129-L140>

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [January 18, 2017, 5:15am UTC](https://meta.discourse.org/t/global-replace-across-database/55930/2 "2017-01-18T05:15:23Z")

</div>

I believe @techapj has done something similar and can advise you.

---

<div class="post-metadata">

### Author: ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### Post date: [January 18, 2017, 3:09pm UTC](https://meta.discourse.org/t/global-replace-across-database/55930/3 "2017-01-18T15:09:01Z")

</div>

> [@pfaffman](#):
>
> leaving that link in the first post in every topic.

In this case what I will recommend is looping through all the topics and revising the first post. You should add a new custom rake task for this manually on the droplet. The rake task will be like:

```ruby
task 'posts:remap_custom' => :environment do
  puts "Remapping"
  i = 0
  Topic.all.each do |t|
    p = t.first_post
    new_raw = p.raw.dup
    new_raw = new_raw.gsub!(/\[ORIGINAL POST\]\(.*t\)/, "") || new_raw

    if new_raw != p.raw
      p.revise(Discourse.system_user, { raw: new_raw }, { bypass_bump: true, skip_revision: true })
      putc "."
      i += 1
    end
  end
  puts "", "#{i} posts remapped!", ""
end

```

Considering that there are ~2.3 million posts this process will take some time to complete.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [January 18, 2017, 4:39pm UTC](https://meta.discourse.org/t/global-replace-across-database/55930/4 "2017-01-18T16:39:16Z")

</div>

Thanks, @techAPJ! I really appreciate it.

> [@techAPJ](#):
>
> Considering that there are ~2.3 million posts this process will take some time to complete.

Indeed, but there are many fewer topics, so this will probably be better than re-running the import from scratch. Thanks very much. Right now, I’m running a script to create permalinks from when they moved from phpBB to IPB. That started yesterday and is still rolling. . .

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [April 23, 2019, 3:02am UTC](https://meta.discourse.org/t/global-replace-across-database/55930/5 "2019-04-23T03:02:30Z")

</div>



---

<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: [April 25, 2019, 8:40am UTC](https://meta.discourse.org/t/global-replace-across-database/55930/6 "2019-04-25T08:40:52Z")

</div>


