2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
class AddExcerptToTopics < ActiveRecord::Migration[4.2]
|
2014-03-19 01:40:40 +08:00
|
|
|
def up
|
|
|
|
add_column :topics, :excerpt, :string, limit: 1000
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
topic_ids = execute("SELECT id FROM topics WHERE pinned_at IS NOT NULL").map { |r| r['id'].to_i }
|
2014-03-19 01:40:40 +08:00
|
|
|
topic_ids.each do |topic_id|
|
|
|
|
cooked = execute("SELECT cooked FROM posts WHERE topic_id = #{topic_id} ORDER BY post_number ASC LIMIT 1")[0]['cooked']
|
|
|
|
if cooked
|
|
|
|
excerpt = ExcerptParser.get_excerpt(cooked, 220, strip_links: true)
|
|
|
|
execute "UPDATE topics SET excerpt = #{ActiveRecord::Base.sanitize(excerpt)} WHERE id = #{topic_id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
remove_column :topics, :excerpt
|
|
|
|
end
|
|
|
|
end
|