From 6af4e6bd05c8a286378d0d3ff3324d7b8e2db07f Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Wed, 17 Aug 2016 12:47:07 +0530 Subject: [PATCH] FEATURE: new rake task to remap posts matching a string --- lib/tasks/posts.rake | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rake index b9b28920e..b982ad1fb 100644 --- a/lib/tasks/posts.rake +++ b/lib/tasks/posts.rake @@ -110,3 +110,27 @@ task 'posts:normalize_code' => :environment do puts puts "#{i} posts normalized!" end + +desc 'Remap all posts matching specific string' +task 'posts:remap', [:find, :replace] => [:environment] do |_,args| + find = args[:find] + replace = args[:replace] + if !find || !replace + puts "ERROR: Expecting rake posts:rebake_match[find,replace]" + exit 1 + end + + puts "Remapping" + i = 0 + Post.where("raw LIKE ?", "%#{find}%").each do |p| + new_raw = p.raw.dup + new_raw = new_raw.gsub!(/#{Regexp.escape(find)}/, replace) || new_raw + + if new_raw != p.raw + p.revise(Discourse.system_user, { raw: new_raw }, { bypass_bump: true }) + putc "." + i += 1 + end + end + puts "", "#{i} posts remapped!", "" +end