From d29eddc40135865a21ca2b2a85f72d4d18617a20 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 5 Dec 2024 17:12:26 +0100 Subject: [PATCH] Fix tag trend migration failing due to duplicate rows (#33189) --- .../20241123160722_move_tag_trends_to_table.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/db/post_migrate/20241123160722_move_tag_trends_to_table.rb b/db/post_migrate/20241123160722_move_tag_trends_to_table.rb index 3e1f1b5440..629592ba8a 100644 --- a/db/post_migrate/20241123160722_move_tag_trends_to_table.rb +++ b/db/post_migrate/20241123160722_move_tag_trends_to_table.rb @@ -6,12 +6,8 @@ class MoveTagTrendsToTable < ActiveRecord::Migration[7.2] disable_ddl_transaction! def up - redis.zrange('trending_tags:all', 0, -1, with_scores: true).each do |(tag_id, score)| - TagTrend.create( - tag_id: tag_id, - score: score, - allowed: redis.zscore('trending_tags:allowed', tag_id).present? - ) + redis.zrange('trending_tags:all', 0, -1, with_scores: true).each_slice(1_000) do |data| + TagTrend.upsert_all(data.map { |(tag_id, score)| { tag_id: tag_id, score: score, language: '', allowed: redis.zscore('trending_tags:allowed', tag_id).present? } }, unique_by: %w(tag_id language)) end TagTrend.recalculate_ordered_rank