discourse-yearly-review/db/migrate/20230208123631_backfill_yearly_review_custom_fields.rb

23 lines
758 B
Ruby

# frozen_string_literal: true
class BackfillYearlyReviewCustomFields < ActiveRecord::Migration[6.1]
def change
2017.upto(2022) do |year|
topic_title = I18n.t("yearly_review.topic_title", year: year)
DB.exec(
<<~SQL,
INSERT INTO post_custom_fields (post_id, name, value, created_at, updated_at)
SELECT posts.id, :name, :value, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
FROM posts
INNER JOIN topics ON topics.id = posts.topic_id
WHERE posts.user_id = :user_id AND topics.title = :title
ON CONFLICT DO NOTHING
SQL
title: topic_title,
user_id: Discourse::SYSTEM_USER_ID,
name: YearlyReview::POST_CUSTOM_FIELD,
value: year.to_s,
)
end
end
end