-
-
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
Draft
trichoplax
wants to merge
19
commits into
develop
Choose a base branch
from
trichoplax/separate-new-thread-followers-model
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
19 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 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
Some comments aren't visible on the classic Files Changed page.
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) | ||
| ) | ||
| ThreadFollower.where.not(post_id:nil).delete_all | ||
ArtOfCode- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 | ||
ArtOfCode- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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.
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.
I would add indexes here for
user_idandpost_idseparately as well. We're likely to be querying for thread followers by post or by user as well as by both.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.
Revision after discussion in Discord: let's add an additional index for
:post_id- the composite index will work for searches on:user_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.
Thanks for explaining that the composite index works differently in MySQL. I've now added the index on
post_id(and the corresponding removal for rollback).