Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Conversation

nattsw
Copy link
Contributor

@nattsw nattsw commented Mar 26, 2025

We are seeing some errors when migrating and adding indexes on answer_post_id.

#<StandardError:"An error has occurred, all later migrations canceled:\n\nPG::UniqueViolation: ERROR:  could not create unique index \"index_discourse_solved_solved_topics_on_answer_post_id\"\nDETAIL:  Key (answer_post_id)=(13006) is duplicated.\n">

This PR modifies the earlier migration, and also adds one before the addition of indexes to remove duplicates.

Comment on lines +41 to +42
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
Copy link
Contributor Author

@nattsw nattsw Mar 26, 2025

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]
Copy link
Contributor Author

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.

Comment on lines +10 to +15
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;",
)
Copy link
Contributor Author

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.

@nattsw nattsw merged commit 1805184 into main Mar 26, 2025
6 checks passed
@nattsw nattsw deleted the fix-migration branch March 26, 2025 14:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants