-
-
Notifications
You must be signed in to change notification settings - Fork 71
Split ThreadFollower into separate models for posts and threads #1920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
trichoplax
merged 34 commits into
develop
from
trichoplax/separate-new-thread-followers-model
Jan 29, 2026
Merged
Changes from 19 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
17986cd
Add NewThreadFollower model
trichoplax ffd7791
Amend code to use NewThreadFollower model instead
trichoplax 8882785
Use single quotes for rubocop
trichoplax 9a3e5c0
Include data migration and column removal in migration
trichoplax 97c2649
Fix tests by rearranging fixtures for new table
trichoplax 5da697b
Fix migration to remove moved rows and post reference
trichoplax f7c79f0
Add model tests for ThreadFollower and NewThreadFollower
trichoplax ac4f738
Tidying thanks to rubocop
trichoplax c82fd1f
Fix data insertion in NewThreadFollower migration
trichoplax a2af203
Defend migration against new_thread_followers table already existing
trichoplax 5b77c12
Check if post_id column exists during NewThreadFollower migration
trichoplax 5eb10d1
Add up and down to make NewThreadFollower migration reversible
trichoplax 59ca21c
fixed typo in the create_new_thread_followers migration
Oaphi 8d26314
made create_new_thread_followers more resilient to partial state
Oaphi c0acdf0
drop new_thread_followers table only if it exists
Oaphi b890d38
added thread follower tests for the user merge concern
Oaphi 58848e8
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
Oaphi c1b8251
Include specific post_id index in migration
trichoplax 6d8e9a2
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
trichoplax 74b2c2e
Rename migration to later before merging an earlier one
trichoplax 550bf1f
Merge branch '0valt/1930/comments-last-activity' into trichoplax/sepa…
trichoplax 0304650
Remove post_id condition since column has been removed
trichoplax b030287
Add job for new thread followers duplicates following split
trichoplax c19a3a7
Remove validation now that post_id column has been removed
trichoplax f94f99f
Add new runner script for new thread followers duplicate job
trichoplax 393e1c1
Update schema following migration
trichoplax 0117d76
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
Oaphi 9c7b116
added test for the CleanUpNewThreadFollowersJob job
Oaphi 6a9472b
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
Oaphi 17f88ca
Use same_as? method instead of direct comparisons of users
trichoplax 6e712b7
Add safe navigation consistency caught by rubocop
trichoplax 8ca50ed
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
Oaphi 239aaa7
Merge branch 'develop' into trichoplax/separate-new-thread-followers-…
Oaphi fb23244
rubocop fixes
Oaphi 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
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,4 @@ | ||
| class NewThreadFollower < ApplicationRecord | ||
| belongs_to :user | ||
| belongs_to :post | ||
| 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 |
|---|---|---|
| @@ -1,15 +1,4 @@ | ||
| class ThreadFollower < ApplicationRecord | ||
| belongs_to :comment_thread, optional: true | ||
| belongs_to :post, optional: true | ||
| belongs_to :comment_thread | ||
| belongs_to :user | ||
|
|
||
| validate :thread_or_post | ||
|
|
||
| private | ||
|
|
||
| def thread_or_post | ||
| if comment_thread.nil? && post.nil? | ||
| errors.add(:base, 'Must refer to either a comment thread or a post.') | ||
| end | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| class CreateNewThreadFollowers < ActiveRecord::Migration[7.2] | ||
| def up | ||
| create_table_new_thread_followers | ||
| if column_exists?(:thread_followers, :post_id) | ||
| move_rows_with_non_nil_post_id | ||
| remove_post_id_column_from_thread_followers | ||
| end | ||
| end | ||
|
|
||
| def down | ||
| if !column_exists?(:thread_followers, :post_id) | ||
| add_post_id_column_to_thread_followers | ||
| end | ||
| move_rows_back_from_new_thread_followers | ||
| delete_table_new_thread_followers | ||
| end | ||
|
|
||
| def create_table_new_thread_followers | ||
| create_table :new_thread_followers, if_not_exists: true do |t| | ||
| t.bigint :user_id | ||
| t.bigint :post_id | ||
|
|
||
| t.timestamps | ||
| end | ||
| add_index :new_thread_followers, [:user_id, :post_id], if_not_exists: true | ||
| add_index :new_thread_followers, :post_id, if_not_exists: true | ||
| end | ||
|
|
||
| def move_rows_with_non_nil_post_id | ||
| NewThreadFollower.insert_all( | ||
| ThreadFollower.select(:user_id, :post_id, :created_at, :updated_at) | ||
| .where.not(post_id:nil) | ||
| .to_a | ||
| .map(&:attributes) | ||
| ) | ||
|
Oaphi marked this conversation as resolved.
|
||
| ThreadFollower.where.not(post_id:nil).delete_all | ||
| end | ||
|
|
||
| def remove_post_id_column_from_thread_followers | ||
| remove_reference :thread_followers, :post, index: true, foreign_key: true, if_exists: true | ||
| end | ||
|
|
||
| def add_post_id_column_to_thread_followers | ||
| add_reference :thread_followers, :post, index: true, foreign_key: true, if_not_exists: true | ||
| end | ||
|
|
||
| def move_rows_back_from_new_thread_followers | ||
| ThreadFollower.insert_all( | ||
| NewThreadFollower.select(:user_id, :post_id, :created_at, :updated_at) | ||
| .to_a | ||
| .map(&:attributes) | ||
| ) | ||
| NewThreadFollower.delete_all | ||
| end | ||
|
|
||
| def delete_table_new_thread_followers | ||
| remove_index :new_thread_followers, :post_id, if_exists: true | ||
| remove_index :new_thread_followers, [:user_id, :post_id], if_exists: true | ||
| remove_reference :new_thread_followers, :user_id, foreign_key: true, if_exists: true | ||
| remove_reference :new_thread_followers, :post_id, foreign_key: true, if_exists: true | ||
| drop_table :new_thread_followers, if_exists: true | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
| standard_author_question_one: | ||
| user: standard_user | ||
| post: question_one | ||
|
|
||
| merge_source_question_one: | ||
| user: merge_source | ||
| post: question_one |
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,46 @@ | ||
| require 'test_helper' | ||
|
|
||
| class UserMergeTest < ActiveSupport::TestCase | ||
| test 'merge_into should destroy the old user upon success' do | ||
| merger = users(:global_admin) | ||
| src_usr = users(:merge_source) | ||
| tgt_usr = users(:merge_target) | ||
|
|
||
| src_usr.merge_into(tgt_usr, merger) | ||
|
|
||
| assert_raises ActiveRecord::RecordNotFound do | ||
| src_usr.reload | ||
| end | ||
| end | ||
|
|
||
| test 'merge_info should move followed threads / posts to the target user' do | ||
| merger = users(:global_admin) | ||
| src_usr = users(:merge_source) | ||
| tgt_usr = users(:merge_target) | ||
|
|
||
| src_new_threads_followed = NewThreadFollower.where(user: src_usr) | ||
| src_new_threads_followed_ids = src_new_threads_followed.map(&:id) | ||
| assert src_new_threads_followed_ids.any? | ||
|
|
||
| src_threads_followed = ThreadFollower.where(user: src_usr) | ||
| src_threads_followed_ids = src_threads_followed.map(&:id) | ||
| assert src_threads_followed_ids.any? | ||
|
|
||
| src_usr.merge_into(tgt_usr, merger) | ||
|
|
||
| src_new_threads_followed.reload | ||
| src_threads_followed.reload | ||
|
|
||
| assert src_new_threads_followed.none? | ||
| assert src_threads_followed.none? | ||
|
|
||
| tgt_new_threads_followed = NewThreadFollower.where(id: src_new_threads_followed_ids) | ||
| tgt_threads_followed = ThreadFollower.where(id: src_threads_followed_ids) | ||
|
|
||
| assert tgt_new_threads_followed.any? | ||
| assert(tgt_new_threads_followed.all? { |a| a.user.same_as?(tgt_usr) }) | ||
|
|
||
| assert tgt_threads_followed.any? | ||
| assert(tgt_threads_followed.all? { |a| a.user.same_as?(tgt_usr) }) | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| require 'test_helper' | ||
|
|
||
| class NewThreadFollowerTest < ActiveSupport::TestCase | ||
| test 'save succeeds with user and post' do | ||
| new_thread_follower = NewThreadFollower.new | ||
| new_thread_follower.user = users(:basic_user) | ||
| new_thread_follower.post = posts(:question_one) | ||
| assert new_thread_follower.save | ||
| end | ||
|
|
||
| test 'save fails without user' do | ||
| new_thread_follower = NewThreadFollower.new | ||
| new_thread_follower.post = posts(:question_one) | ||
| assert_not new_thread_follower.save | ||
| end | ||
|
|
||
| test 'save fails without post' do | ||
| new_thread_follower = NewThreadFollower.new | ||
| new_thread_follower.user = users(:basic_user) | ||
| assert_not new_thread_follower.save | ||
| 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.