Global Replace across database


(Jay Pfaffman) #1

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?


(Jeff Atwood) #2

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


(Arpit Jalan) #3

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.


(Jay Pfaffman) #4

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