Update URLs inside a poll after changing CDN

I have created a poll on my forum like:

[poll name=freestyle type=multiple results=always min=1 max=14 public=true chartType=bar]
* **#1** ![DSC_2281.jpg|332x500, 33%](upload://ujMDcnd4syevNGdEN2O1qUTeWeK)
* **#2** ![IMG_3248.jpg|690x459, 33%](upload://tsecEjlruXEpg3faidWmENBupM4)
[/poll]

I migrated from a CDN to no CDN, and the URLs were changed from cdn.unicyclist.com to unicyclist.com.

However, the images URLs here remains broken, despite appearing in the post preview:

Rebaking posts and running rake 'posts:remap[cdn.unicyclist.com,unicyclist.com]' didn’t fix it.

ask.discourse.org advises:

What’s probably happening

Your poll options are not only in the post raw. On your site schema, polls also store rendered option HTML in:

  • polls
  • poll_options.html

So the post preview can look fine, but the poll renderer may still be using stale HTML with the old CDN hostname.

Related docs:

Important warning

Take a full backup first.

Also, avoid “just editing” the poll if it has votes, because poll edits normally clear votes:

Safer approach: inspect one broken poll first

In the container:

cd /var/discourse
./launcher enter app
rails c

Then find the poll for that post:

poll = Poll.find_by(post_id: POST_ID, name: "freestyle")
PollOption.where(poll_id: poll.id).pluck(:id, :html)

If you see cdn.unicyclist.com in that HTML, that confirms it.

Fix that one poll

poll = Poll.find_by(post_id: POST_ID, name: "freestyle")

PollOption.where(poll_id: poll.id).find_each do |opt|
  new_html = opt.html.gsub("cdn.unicyclist.com", "unicyclist.com")
  opt.update_columns(html: new_html, updated_at: Time.zone.now)
end

Then rebake that post too:

Post.find(POST_ID).rebake!

If it works, bulk-fix all poll options

PollOption.find_each do |opt|
  next unless opt.html&.include?("cdn.unicyclist.com")
  opt.update_columns(
    html: opt.html.gsub("cdn.unicyclist.com", "unicyclist.com"),
    updated_at: Time.zone.now
  )
end

Why posts:remap didn’t help

Because that rake task is for post content, while your broken image references are probably sitting in the poll option HTML cache instead:

Recommendation

  1. test on one broken poll
  2. confirm the images load
  3. only then run the bulk fix

If you want, I can write you a copy-paste script where you only replace POST_ID and run it safely on a single poll first.

Does that look good to you? I’ll do a backup first obviously, but I prefer to avoid having to restore my forum.

I need to keep all poll results :slight_smile:

Bonus question: should Discourse natively handle those cases?