2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
class FixLinkPostId < ActiveRecord::Migration[4.2]
|
2013-02-06 03:16:51 +08: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?
|
2014-05-06 21:41:59 +08:00
|
|
|
post = Post.find_by(topic_id: route[:topic_id], post_number: (route[:post_number] || 1))
|
2013-02-06 03:16:51 +08:00
|
|
|
tl.update_column(:link_post_id, post.id) if post.present?
|
2023-01-09 19:59:41 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
rescue ActionController::RoutingError
|
|
|
|
to_remove << tl.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
TopicLink.where("id in (?)", to_remove).delete_all
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
end
|
|
|
|
end
|