class ModifyMdAttachmentUrlForMdCotents < ActiveRecord::Migration[5.2]
  def change
    # 更新MarkDown图片的URL
    homework_commons = HomeworkCommon.all
    homework_commons.find_each do |hc|
      hc.update_column(:description, hc.description.gsub("![](/attachments/download", "![](/api/attachments")) if hc.description.present?
    end

    challenges = Challenge.all.unscoped
    challenges.find_each do |c|
      c.update_column(:task_pass, c.task_pass.gsub("![](/attachments/download", "![](/api/attachments")) if c.task_pass.present?
    end

    challenge_answers = ChallengeAnswer.all.unscoped
    challenge_answers.find_each do |ca|
      ca.update_column(:contents, ca.contents.gsub("![](/attachments/download", "![](/api/attachments")) if ca.contents.present?
    end

    shixun_infos = ShixunInfo.all
    shixun_infos.find_each do |si|
      si.update_column(:propaedeutics, si.propaedeutics.gsub("![](/attachments/download", "![](/api/attachments")) if si.propaedeutics.present?
      si.update_column(:description, si.description.gsub("![](/attachments/download", "![](/api/attachments")) if si.description.present?
    end

    subjects = Subject.all
    subjects.find_each do |s|
      s.update_column(:description, s.description.gsub("![](/attachments/download", "![](/api/attachments")) if s.description.present?
    end

    plats = PlatformSample.where(samples_type: ['introduction', 'knowledge'])
    plats.find_each do |p|
      p.update_column(:contents, p.contents.gsub("![](/attachments/download", "![](/api/attachments")) if p.contents.present?
    end
  end
end
