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

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

(Makary Gołosz) #4

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


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

(Makary Gołosz) #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


I just opened a PR to fix the issue.

Thanks for letting me know!

(Parinita Sandhu ) #8

I have the following error

root@server-app:/var/www/discourse# rake 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)>'
/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|

(Colin Marshall) #12


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%'")