You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

55 lines
1.7 KiB

  1. namespace :migration do
  2. desc '旧掲示板からデータを移行する.'
  3. task import: :environment do
  4. stats = { }
  5. sql = LegacyResponse.select('thread_id, MIN(date) AS first_date, MAX(date) AS last_date, COUNT(*) AS cnt')
  6. .group('thread_id')
  7. .to_sql
  8. LegacyResponse.connection.select_all(sql).each do |r|
  9. stats[r['thread_id'].to_i] = {
  10. first_date: r['first_date'],
  11. last_date: r['last_date'],
  12. count: r['cnt'].to_i }
  13. end
  14. ActiveRecord::Base.record_timestamps = false
  15. date = '1900-01-01 00:00:00'
  16. LegacyThread.find_each do |lt|
  17. date = stats[lt.id]&.[](:first_date) || date
  18. thread = Topic.find_or_create_by!(
  19. id: lt.id + 1,
  20. name: lt.title,
  21. description: lt.explain.gsub('<p>', '').gsub('</p>', ''),
  22. created_at: date,
  23. updated_at: lt.latest)
  24. end
  25. ActiveRecord::Base.record_timestamps = true
  26. LegacyResponse.find_each do |lp|
  27. post = Post.new(
  28. thread_id: lp.thread_id + 1,
  29. post_no: lp.response_id,
  30. name: lp.name == '名なしさん' ? nil : lp.name,
  31. message: lp.message.presence,
  32. created_at: lp.date,
  33. updated_at: lp.date,
  34. held: lp.held,
  35. deleted_at: lp.deleted ? lp.date : nil,
  36. good: lp.good,
  37. bad: lp.bad,
  38. sensitive: false)
  39. post.password = lp.pass.presence
  40. if lp.image.present?
  41. path = ("/var/www/kekec/bbs/images/#{ lp.image }")
  42. if File.exist?(path)
  43. post.image.attach(io: File.open(path), filename: lp.image)
  44. end
  45. end
  46. post.save!
  47. end
  48. end
  49. end