Global Replace across database

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 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/master/lib/tasks/posts.rake#L129-L140

2 Likes

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

4 Likes

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:

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.

3 Likes

Thanks, @techAPJ! I really appreciate it.

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

2 Likes