0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 11:52:32 +08:00
discourse/db/migrate/20121204183855_fix_link_post_id.rb
Jarek Radosz fbb3bf3fe8
DEV: Enable Style/RedundantBegin rubocop rule (#40096)
(to be enabled in the shared config)

best reviewed with whitespace disabled
2026-05-19 18:44:54 +02:00

25 lines
685 B
Ruby
Vendored

# frozen_string_literal: true
class FixLinkPostId < ActiveRecord::Migration[4.2]
def up
to_remove = []
TopicLink
.where("internal = TRUE AND link_post_id IS NULL")
.each do |tl|
parsed = URI.parse(tl.url)
route = Rails.application.routes.recognize_path(parsed.path)
if route[:topic_id].present?
post = Post.find_by(topic_id: route[:topic_id], post_number: route[:post_number] || 1)
tl.update_column(:link_post_id, post.id) if post.present?
end
rescue ActionController::RoutingError
to_remove << tl.id
end
TopicLink.where("id in (?)", to_remove).delete_all
end
def down
end
end