discourse/db/migrate/20121204183855_fix_link_pos...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

28 lines
729 B
Ruby
Raw Normal View History

# frozen_string_literal: true
class FixLinkPostId < ActiveRecord::Migration[4.2]
2013-02-05 14:16:51 -05:00
def up
to_remove = []
TopicLink
.where("internal = TRUE AND link_post_id IS NULL")
.each do |tl|
begin
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))
2013-02-05 14:16:51 -05:00
tl.update_column(:link_post_id, post.id) if post.present?
end
2013-02-05 14:16:51 -05:00
rescue ActionController::RoutingError
to_remove << tl.id
end
end
TopicLink.where("id in (?)", to_remove).delete_all
2013-02-05 14:16:51 -05:00
end
def down
end
end