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.
Advanced: From the Rails Console
For some tasks the rake task isn’t enough and you have to go to the Rails console. To get to the rails console, you do this from within the docker container:
rails c
Using rebake uncooked rake task
You jump into the rails console just to set the target posts to ‘uncooked’.
Then you jump back out and run the rake posts:rebake_uncooked_posts
command.
For example, to rebake a whole category:
rails c
Post.joins(:topic).where('topics.category_id = 136').update_all('baked_version = NULL')
exit
rake posts:rebake_uncooked_posts
Via a loop in the Rails Console
To get really granular, you can select anything as an array and then use a simple loop to apply it rebake each post directly
Posts from a particular user
user = User.find_by_username('user123')
posts = Post.where (user_id: user.id)
posts.each do |p|
p.rebake!
end
Posts with a string in the cooked post
posts = Post.where ("cooked like '%OldCDN%'")
posts.each do |p|
p.rebake!
end