FEATURE: support regex in rake post:remap (#5201)

This commit is contained in:
Kyle Zhao 2017-10-03 20:47:53 -04:00 committed by Sam
parent 4ee2fcd3d5
commit 0342324b47
3 changed files with 76 additions and 22 deletions

View File

@ -91,6 +91,16 @@ class Post < ActiveRecord::Base
q.order('posts.created_at ASC') q.order('posts.created_at ASC')
} }
scope :raw_match, -> (pattern, type = 'string') {
type = type&.downcase
case type
when 'string'
where('raw ILIKE ?', "%#{pattern}%")
when 'regex'
where('raw ~ ?', pattern)
end
}
delegate :username, to: :user delegate :username, to: :user

View File

@ -42,23 +42,18 @@ end
desc 'Rebake all posts matching string/regex and optionally delay the loop' desc 'Rebake all posts matching string/regex and optionally delay the loop'
task 'posts:rebake_match', [:pattern, :type, :delay] => [:environment] do |_, args| task 'posts:rebake_match', [:pattern, :type, :delay] => [:environment] do |_, args|
args.with_defaults(type: 'string')
pattern = args[:pattern] pattern = args[:pattern]
type = args[:type] type = args[:type]&.downcase
type = type.downcase if type delay = args[:delay]&.to_i
delay = args[:delay].to_i if args[:delay]
if !pattern if !pattern
puts "ERROR: Expecting rake posts:rebake_match[pattern,type,delay]" puts "ERROR: Expecting rake posts:rebake_match[pattern,type,delay]"
exit 1 exit 1
elsif delay && delay < 1 elsif delay && delay < 1
puts "ERROR: delay parameter should be an integer and greater than 0" puts "ERROR: delay parameter should be an integer and greater than 0"
exit 1 exit 1
end elsif type != 'string' && type != 'regex'
if type == "regex"
search = Post.where("raw ~ ?", pattern)
elsif type == "string" || !type
search = Post.where("raw ILIKE ?", "%#{pattern}%")
else
puts "ERROR: Expecting rake posts:rebake_match[pattern,type] where type is string or regex" puts "ERROR: Expecting rake posts:rebake_match[pattern,type] where type is string or regex"
exit 1 exit 1
end end
@ -66,7 +61,7 @@ task 'posts:rebake_match', [:pattern, :type, :delay] => [:environment] do |_, ar
rebaked = 0 rebaked = 0
total = search.count total = search.count
search.find_each do |post| Post.raw_match(pattern, type).find_each do |post|
rebake_post(post) rebake_post(post)
print_status(rebaked += 1, total) print_status(rebaked += 1, total)
sleep(delay) if delay sleep(delay) if delay
@ -130,11 +125,15 @@ task 'posts:normalize_code' => :environment do
puts "#{i} posts normalized!" puts "#{i} posts normalized!"
end end
def remap_posts(find, replace = "") def remap_posts(find, type, replace = "")
i = 0 i = 0
Post.where("raw LIKE ?", "%#{find}%").each do |p|
new_raw = p.raw.dup Post.raw_match(find, type).find_each do |p|
new_raw = new_raw.gsub!(/#{Regexp.escape(find)}/, replace) || new_raw new_raw =
case type
when 'string' then p.raw.gsub(/#{Regexp.escape(find)}/, replace)
when 'regex' then p.raw.gsub(/#{find}/, replace)
end
if new_raw != p.raw if new_raw != p.raw
p.revise(Discourse.system_user, { raw: new_raw }, bypass_bump: true, skip_revision: true) p.revise(Discourse.system_user, { raw: new_raw }, bypass_bump: true, skip_revision: true)
@ -142,42 +141,59 @@ def remap_posts(find, replace = "")
i += 1 i += 1
end end
end end
i i
end end
desc 'Remap all posts matching specific string' desc 'Remap all posts matching specific string'
task 'posts:remap', [:find, :replace] => [:environment] do |_, args| task 'posts:remap', [:find, :replace, :type] => [:environment] do |_, args|
require 'highline/import'
args.with_defaults(type: 'string')
find = args[:find] find = args[:find]
replace = args[:replace] replace = args[:replace]
type = args[:type]&.downcase
if !find if !find
puts "ERROR: Expecting rake posts:remap['find','replace']" puts "ERROR: Expecting rake posts:remap['find','replace']"
exit 1 exit 1
elsif !replace elsif !replace
puts "ERROR: Expecting rake posts:remap['find','replace']. Want to delete a word/string instead? Try rake posts:delete_word['word-to-delete']" puts "ERROR: Expecting rake posts:remap['find','replace']. Want to delete a word/string instead? Try rake posts:delete_word['word-to-delete']"
exit 1 exit 1
elsif type != 'string' && type != 'regex'
puts "ERROR: Expecting rake posts:delete_word[pattern, type] where type is string or regex"
exit 1
else
confirm_replace = ask("Are you sure you want to replace all #{type} occurrences of '#{find}' with '#{replace}'? (Y/n)")
exit 1 unless (confirm_replace == "" || confirm_replace.downcase == 'y')
end end
puts "Remapping" puts "Remapping"
total = remap_posts(find, replace) total = remap_posts(find, type, replace)
puts "", "#{total} posts remapped!", "" puts "", "#{total} posts remapped!", ""
end end
desc 'Delete occurrence of a word/string' desc 'Delete occurrence of a word/string'
task 'posts:delete_word', [:find] => [:environment] do |_, args| task 'posts:delete_word', [:find, :type] => [:environment] do |_, args|
require 'highline/import' require 'highline/import'
args.with_defaults(type: 'string')
find = args[:find] find = args[:find]
type = args[:type]&.downcase
if !find if !find
puts "ERROR: Expecting rake posts:delete_word['word-to-delete']" puts "ERROR: Expecting rake posts:delete_word['word-to-delete']"
exit 1 exit 1
elsif type != 'string' && type != 'regex'
puts "ERROR: Expecting rake posts:delete_word[pattern, type] where type is string or regex"
exit 1
else else
confirm_replace = ask("Are you sure you want to remove all occurrences of '#{find}'? (Y/n) ") confirm_delete = ask("Are you sure you want to remove all #{type} occurrences of '#{find}'? (Y/n)")
exit 1 unless (confirm_replace == "" || confirm_replace.downcase == 'y') exit 1 unless (confirm_delete == "" || confirm_delete.downcase == 'y')
end end
puts "Processing" puts "Processing"
total = remap_posts(find) total = remap_posts(find, type)
puts "", "#{total} posts updated!", "" puts "", "#{total} posts updated!", ""
end end

View File

@ -1,18 +1,46 @@
require 'rails_helper' require 'rails_helper'
require 'highline/import'
require 'highline/simulate'
RSpec.describe "Post rake tasks" do RSpec.describe "Post rake tasks" do
before do before do
Rake::Task.clear
Discourse::Application.load_tasks Discourse::Application.load_tasks
IO.any_instance.stubs(:puts) IO.any_instance.stubs(:puts)
end end
describe 'remap' do describe 'remap' do
let!(:tricky_post) { Fabricate(:post, raw: 'Today ^Today') }
it 'should remap posts' do it 'should remap posts' do
post = Fabricate(:post, raw: "The quick brown fox jumps over the lazy dog") post = Fabricate(:post, raw: "The quick brown fox jumps over the lazy dog")
Rake::Task['posts:remap'].invoke("brown", "red") HighLine::Simulate.with('y') do
Rake::Task['posts:remap'].invoke("brown", "red")
end
post.reload post.reload
expect(post.raw).to eq('The quick red fox jumps over the lazy dog') expect(post.raw).to eq('The quick red fox jumps over the lazy dog')
end end
context 'when type == string' do
it 'remaps input as string' do
HighLine::Simulate.with('y') do
Rake::Task['posts:remap'].invoke('^Today', 'Yesterday', 'string')
end
expect(tricky_post.reload.raw).to eq('Today Yesterday')
end
end
context 'when type == regex' do
it 'remaps input as regex' do
HighLine::Simulate.with('y') do
Rake::Task['posts:remap'].invoke('^Today', 'Yesterday', 'regex')
end
expect(tricky_post.reload.raw).to eq('Yesterday ^Today')
end
end
end end
end end