Rebake all posts matching a pattern


(Arpit Jalan) #1

Want to rebake all the posts matching a string or regular expression? Let’s do it!

Access Your Site

First connect to your Droplet via SSH, and enter the Docker container for your Discourse instances:

cd /var/discourse
./launcher enter app

Rebake All Posts Containing a Specific String

Run this command:

rake posts:rebake_match["pattern"]

Replace pattern with the string you want to match. Note that the match performed will be case insensitive.

rake posts:rebake_match[":slight_smile:"]

The above command will rebake all posts which contains :slight_smile: in post raw body.

Rebake All Posts Matching a Regular Expression

To use regular expressions instead of specific string matches:

rake posts:rebake_match["pattern",regex]

PostgreSQL uses POSIX Regular Expressions to perform regex matching. The matching will be case-sensitive.

rake posts:rebake_match["(:slight_smile:|Discourse)",regex]

Rebake all posts containing :slight_smile: or Discourse in post raw body.

rake posts:rebake_match["^Today",regex]

Rebake all posts which starts with Today.

rake posts:rebake_match["^(Today|Yesterday)",regex]

Rebake all posts which starts with Today or Yesterday.

Optional: Add a delay between successive rebakes

In case you want to add a 5 seconds delay between successive rebakes, do:

rake posts:rebake_match["pattern",string,5]

Replace 5 with n (seconds) as per your requirement.


Replace a string in all posts
Some emojis broken after 1.5 -> 1.6.1 update
URL rewrite for domain change in permalinks
Migrating to new server quoted post avatar image 404
Onebox is broken for some old links
Proxy avatar urls broken in quotes with sub folder install
Fix quotes after phpBB import
Force remote image download after removing disabled domain
How to regenerate the cooked column from the raw
Moving files to different bucket S3 [AWS]
Unable to undo "censoring a word"
Rebake all posts?
(Arpit Jalan) #3

A post was split to a new topic: Fixing data after import


#4

Didn’t work for me, untill I reverted latest changes to posts.rake
(Sorry, couldn’t find any issue board on Github)


#5

I’ll take a look later today. Were there any error messages?


#6
root@<not relevant>:/var/www/discourse# rake posts:rebake_match["<img.*>",regex]
rake aborted!
NameError: undefined local variable or method `search' for main:Object
/var/www/discourse/lib/tasks/posts.rake:62:in `block in <top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => posts:rebake_match
(See full trace by running task with --trace)
root@<not relevant>:/var/www/discourse# rake posts:rebake_match[":slight_smile:",regex] -t
** Invoke posts:rebake_match (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute posts:rebake_match
rake aborted!
NameError: undefined local variable or method `search' for main:Object
/var/www/discourse/lib/tasks/posts.rake:62:in `block in <top (required)>'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:251:in `block in execute'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:251:in `each'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:251:in `execute'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:195:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:188:in `invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/task.rb:181:in `invoke'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:153:in `invoke_task'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:109:in `block (2 levels) in top_level'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:109:in `each'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:109:in `block in top_level'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:118:in `run_with_threads'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:103:in `top_level'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:81:in `block in run'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:179:in `standard_exception_handling'
/var/www/discourse/vendor/bundle/ruby/2.4.0/gems/rake-12.1.0/lib/rake/application.rb:78:in `run'
bin/rake:4:in `<top (required)>'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli/exec.rb:74:in `load'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli/exec.rb:74:in `kernel_load'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli/exec.rb:27:in `run'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli.rb:362:in `exec'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli.rb:22:in `dispatch'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/cli.rb:13:in `start'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/exe/bundle:30:in `block in <top (required)>'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/lib/bundler/friendly_errors.rb:121:in `with_friendly_errors'
/usr/local/lib/ruby/gems/2.4.0/gems/bundler-1.15.4/exe/bundle:22:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => posts:rebake_match

#7

I just opened a PR to fix the issue.

Thanks for letting me know!


(pari) #8

I have the following error

root@server-app:/var/www/discourse# rake posts:rebake_match["https://google.com"]
rake aborted!
NameError: undefined local variable or method `search' for main:Object
/var/www/discourse/lib/tasks/posts.rake:62:in `block in <top (required)>'
/usr/local/bin/bundle:22:in `load'
/usr/local/bin/bundle:22:in `<main>'
Tasks: TOP => posts:rebake_match
(See full trace by running task with --trace)



(Arpit Jalan) #9

Are you running on latest Discourse version? As you can see just above your post @xrav3nz fixed this and that PR is merged.


(Colin Marshall) #10

Is it possible to rebake all posts from a specific user?


(Jay Pfaffman) #11

Not with that rake task. You could do it from the rails console.
Something like

posts = Post.where (user_id: 123)

posts.each do |p|
  p.rebake!
end

(Colin Marshall) #12

Thanks!

One more situation. I migrated a forum from SMF to discourse and there are all these posts that say “Sent from my (device name) using Tapatalk.” I ran a query and got rid of that line from all posts that contained it.

I tired running rake posts:rebake_match["Tapatalk”] but that didn’t work because I’m assuming it matches from the raw post data and that query removed the word I’m trying to match.

Is there a way to I can match against the built html from the rails console?


(Jay Pfaffman) #13
posts = Post.where ("cooked like '%Tapatalk%'")