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
DEV: Move solved custom fields into a table #342
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
65c972c
DEV: Move the post and topic custom fields into a table
Lhcfl 9c583d5
Update badges sql
Lhcfl e569959
Batch migration due to sites having > 200k rows of solved
nattsw 8036f5b
Move all custom field usage to tables
nattsw 7d6611d
Split create and copy
nattsw 81647dc
Annotations and test update
nattsw 920d08c
Preloading and missing validation
nattsw 9071d56
transaction
nattsw 75ad157
Migration spec
nattsw 248020b
Discourse compat
nattsw 991acbd
Remove migration spec
nattsw da38b7e
Remove stable pin
nattsw 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
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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module DiscourseSolved | ||
class SolvedTopic < ActiveRecord::Base | ||
self.table_name = "discourse_solved_solved_topics" | ||
|
||
belongs_to :topic, class_name: "Topic" | ||
belongs_to :answer_post, class_name: "Post", foreign_key: "answer_post_id" | ||
belongs_to :accepter, class_name: "User", foreign_key: "accepter_user_id" | ||
belongs_to :topic_timer | ||
|
||
validates :topic_id, presence: true | ||
validates :answer_post_id, presence: true | ||
validates :accepter_user_id, presence: true | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: discourse_solved_solved_topics | ||
# | ||
# id :bigint not null, primary key | ||
# topic_id :integer not null | ||
# answer_post_id :integer not null | ||
# accepter_user_id :integer not null | ||
# topic_timer_id :integer | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_discourse_solved_solved_topics_on_answer_post_id (answer_post_id) UNIQUE | ||
# index_discourse_solved_solved_topics_on_topic_id (topic_id) UNIQUE | ||
# |
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
13 changes: 13 additions & 0 deletions
13
db/migrate/20250318024824_create_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,13 @@ | ||
# frozen_string_literal: true | ||
# | ||
class CreateDiscourseSolvedSolvedTopics < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table :discourse_solved_solved_topics do |t| | ||
t.integer :topic_id, null: false | ||
t.integer :answer_post_id, null: false | ||
t.integer :accepter_user_id, null: false | ||
t.integer :topic_timer_id | ||
t.timestamps | ||
end | ||
end | ||
end |
48 changes: 48 additions & 0 deletions
48
...igrate/20250318024953_copy_solved_topic_custom_field_to_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,48 @@ | ||
# frozen_string_literal: true | ||
# | ||
class CopySolvedTopicCustomFieldToDiscourseSolvedSolvedTopics < ActiveRecord::Migration[7.2] | ||
nattsw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
disable_ddl_transaction! | ||
|
||
BATCH_SIZE = 5000 | ||
|
||
def up | ||
last_id = 0 | ||
loop do | ||
rows = DB.query(<<~SQL, last_id: last_id, batch_size: BATCH_SIZE) | ||
INSERT INTO discourse_solved_solved_topics ( | ||
topic_id, | ||
answer_post_id, | ||
topic_timer_id, | ||
accepter_user_id, | ||
created_at, | ||
updated_at | ||
) | ||
SELECT DISTINCT ON (tc.topic_id) | ||
tc.topic_id, | ||
CAST(tc.value AS INTEGER), | ||
CAST(tc2.value AS INTEGER), | ||
COALESCE(ua.acting_user_id, -1), | ||
tc.created_at, | ||
tc.updated_at | ||
FROM topic_custom_fields tc | ||
LEFT JOIN topic_custom_fields tc2 | ||
ON tc2.topic_id = tc.topic_id | ||
AND tc2.name = 'solved_auto_close_topic_timer_id' | ||
LEFT JOIN user_actions ua | ||
ON ua.target_topic_id = tc.topic_id | ||
AND ua.action_type = 15 | ||
WHERE tc.name = 'accepted_answer_post_id' | ||
AND tc.id > :last_id | ||
ORDER BY tc.topic_id, ua.created_at DESC | ||
LIMIT :batch_size | ||
SQL | ||
|
||
break if rows.length == 0 | ||
last_id += BATCH_SIZE | ||
end | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
db/migrate/20250318025147_add_index_for_discourse_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,24 @@ | ||
# frozen_string_literal: true | ||
# | ||
class AddIndexForDiscourseSolvedTopics < ActiveRecord::Migration[7.2] | ||
disable_ddl_transaction! | ||
|
||
def change | ||
remove_index :discourse_solved_solved_topics, | ||
:topic_id, | ||
algorithm: :concurrently, | ||
unique: true, | ||
if_exists: true | ||
remove_index :discourse_solved_solved_topics, | ||
:answer_post_id, | ||
algorithm: :concurrently, | ||
unique: true, | ||
if_exists: true | ||
|
||
add_index :discourse_solved_solved_topics, :topic_id, unique: true, algorithm: :concurrently | ||
add_index :discourse_solved_solved_topics, | ||
:answer_post_id, | ||
unique: true, | ||
algorithm: :concurrently | ||
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
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
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module DiscourseSolved::TopicExtension | ||
extend ActiveSupport::Concern | ||
|
||
prepended { has_one :solved, class_name: "DiscourseSolved::SolvedTopic", dependent: :destroy } | ||
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
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.