discourse/db/migrate/20140318150412_add_excerpt_to_topics.rb
Sam Saffron 30990006a9 DEV: enable frozen string literal on all files
This reduces chances of errors where consumers of strings mutate inputs
and reduces memory usage of the app.

Test suite passes now, but there may be some stuff left, so we will run
a few sites on a branch prior to merging
2019-05-13 09:31:32 +08:00

21 lines
696 B
Ruby

# frozen_string_literal: true
class AddExcerptToTopics < ActiveRecord::Migration[4.2]
def up
add_column :topics, :excerpt, :string, limit: 1000
topic_ids = execute("SELECT id FROM topics WHERE pinned_at IS NOT NULL").map { |r| r['id'].to_i }
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