This repository was archived by the owner on Jul 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
FIX: Multiple topics may have the same post as its solution #348
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
db/migrate/20250318024954_remove_duplicates_from_discourse_solved_solved_topics.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
class RemoveDuplicatesFromDiscourseSolvedSolvedTopics < ActiveRecord::Migration[7.2] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
disable_ddl_transaction! | ||
|
||
def up | ||
puts "before" | ||
puts DiscourseSolved::SolvedTopic.count | ||
# remove duplicates on answer_post_id based on earliest created_at | ||
DB.exec(<<~SQL) | ||
DELETE FROM discourse_solved_solved_topics | ||
WHERE id NOT IN ( | ||
SELECT id FROM ( | ||
SELECT id, ROW_NUMBER() OVER (PARTITION BY answer_post_id ORDER BY created_at) as row_num | ||
FROM discourse_solved_solved_topics | ||
) t WHERE row_num = 1 | ||
) | ||
SQL | ||
|
||
# remove duplicates on topic_id based on earliest created_at | ||
DB.exec(<<~SQL) | ||
DELETE FROM discourse_solved_solved_topics | ||
WHERE id NOT IN ( | ||
SELECT id FROM ( | ||
SELECT id, ROW_NUMBER() OVER (PARTITION BY topic_id ORDER BY created_at) as row_num | ||
FROM discourse_solved_solved_topics | ||
) t WHERE row_num = 1 | ||
) | ||
SQL | ||
|
||
puts "done" | ||
puts DiscourseSolved::SolvedTopic.count | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../db/migrate/20250318024953_copy_solved_topic_custom_field_to_discourse_solved_solved_topics" | ||
|
||
RSpec.describe CopySolvedTopicCustomFieldToDiscourseSolvedSolvedTopics, type: :migration do | ||
let(:migration) { described_class.new } | ||
|
||
describe "handling duplicates" do | ||
it "ensures only unique topic_id and answer_post_id are inserted" do | ||
topic = Fabricate(:topic) | ||
post1 = Fabricate(:post, topic: topic) | ||
Fabricate( | ||
:topic_custom_field, | ||
topic: topic, | ||
name: "accepted_answer_post_id", | ||
value: post1.id.to_s, | ||
) | ||
# explicit duplicate | ||
Fabricate( | ||
:topic_custom_field, | ||
topic: topic, | ||
name: "accepted_answer_post_id", | ||
value: post1.id.to_s, | ||
) | ||
|
||
second_topic = Fabricate(:topic) | ||
post2 = Fabricate(:post, topic: second_topic) | ||
Fabricate( | ||
:topic_custom_field, | ||
topic: second_topic, | ||
name: "accepted_answer_post_id", | ||
value: post2.id.to_s, | ||
) | ||
|
||
migration.up | ||
expected_count = DiscourseSolved::SolvedTopic.count | ||
|
||
expect(expected_count).to eq(2) | ||
|
||
expect(DiscourseSolved::SolvedTopic.where(topic_id: topic.id).count).to eq(1) | ||
expect(DiscourseSolved::SolvedTopic.where(answer_post_id: post1.id).count).to eq(1) | ||
end | ||
end | ||
end |
52 changes: 52 additions & 0 deletions
52
spec/models/remove_duplicates_from_discourse_solved_solved_topics_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../db/migrate/20250318024954_remove_duplicates_from_discourse_solved_solved_topics" | ||
|
||
RSpec.describe RemoveDuplicatesFromDiscourseSolvedSolvedTopics, type: :migration do | ||
let(:migration) { described_class.new } | ||
|
||
before do | ||
# temp drop unique constraints to allow testing 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;", | ||
) | ||
Comment on lines
+10
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
end | ||
|
||
after do | ||
DiscourseSolved::SolvedTopic.delete_all | ||
|
||
# reapply unique indexes | ||
ActiveRecord::Base.connection.execute( | ||
"CREATE UNIQUE INDEX index_discourse_solved_solved_topics_on_topic_id ON discourse_solved_solved_topics (topic_id);", | ||
) | ||
ActiveRecord::Base.connection.execute( | ||
"CREATE UNIQUE INDEX index_discourse_solved_solved_topics_on_answer_post_id ON discourse_solved_solved_topics (answer_post_id);", | ||
) | ||
end | ||
|
||
describe "removal of duplicate answer_post_ids" do | ||
it "keeps only the earliest record for each answer_post_id" do | ||
topic1 = Fabricate(:topic) | ||
post1 = Fabricate(:post, topic: topic1) | ||
topic2 = Fabricate(:topic) | ||
post2 = Fabricate(:post, topic: topic2) | ||
|
||
earlier = Fabricate(:solved_topic, topic: topic1, answer_post: post1, created_at: 2.days.ago) | ||
Fabricate(:solved_topic, topic: topic1, answer_post: post1, created_at: 1.day.ago) | ||
Fabricate(:solved_topic, topic: topic1, answer_post: post1, created_at: Date.today) | ||
another = Fabricate(:solved_topic, topic: topic2, answer_post: post2, created_at: Date.today) | ||
|
||
expect(DiscourseSolved::SolvedTopic.count).to eq(4) | ||
migration.up | ||
|
||
expect(DiscourseSolved::SolvedTopic.count).to eq(2) | ||
expect(DiscourseSolved::SolvedTopic.pluck(:id, :answer_post_id)).to contain_exactly( | ||
[earlier.id, post1.id], | ||
[another.id, post2.id], | ||
) | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.