-
Notifications
You must be signed in to change notification settings - Fork 83
FIX: Multiple topics may have the same post as its solution #348
Conversation
ROW_NUMBER() OVER (PARTITION BY tc.topic_id ORDER BY tc.created_at ASC) AS rn_topic, | ||
ROW_NUMBER() OVER (PARTITION BY CAST(tc.value AS INTEGER) ORDER BY tc.created_at ASC) AS rn_answer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this partition because one post can be the answer to many topics. The migration after this one 20250318025147_add_index_for_discourse_solved_topics
was adding an index and failing.
PG::UniqueViolation: ERROR: could not create unique index \"index_discourse_solved_solved_topics_on_answer_post_id\"
DETAIL: Key (answer_post_id)=(13006) is duplicated.\n">
@@ -0,0 +1,31 @@ | |||
# frozen_string_literal: true | |||
|
|||
class RemoveDuplicatesFromDiscourseSolvedSolvedTopics < ActiveRecord::Migration[7.2] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're adding this new migration in between the copy and the failed add-index migration to remove the duplicate entries.
ActiveRecord::Base.connection.execute( | ||
"DROP INDEX IF EXISTS index_discourse_solved_solved_topics_on_topic_id;", | ||
) | ||
ActiveRecord::Base.connection.execute( | ||
"DROP INDEX IF EXISTS index_discourse_solved_solved_topics_on_answer_post_id;", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To create the duplicate entries below to test the migration, we needed to drop the index first.
We are seeing some errors when migrating and adding indexes on
answer_post_id
.This PR modifies the earlier migration, and also adds one before the addition of indexes to remove duplicates.