-
-
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
Open
trichoplax
wants to merge
28
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.
Open
Changes from all commits
Commits
Show all changes
28 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 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,24 @@ | ||
| class CleanUpNewThreadFollowersJob < ApplicationJob | ||
| queue_as :default | ||
|
|
||
| def perform | ||
| sql = File.read(Rails.root.join('db/scripts/posts_with_duplicate_new_thread_followers.sql')) | ||
| posts = ActiveRecord::Base.connection.execute(sql).to_a | ||
|
|
||
| posts.each do |post| | ||
| user_id, post_id = post | ||
|
|
||
| followers = NewThreadFollower.where(post_id: post_id, user_id: user_id) | ||
|
|
||
| next unless followers.many? | ||
|
|
||
| duplicate = followers.first | ||
| result = duplicate.destroy | ||
|
|
||
| unless result | ||
| puts "failed to destroy new thread follower duplicate \"#{duplicate.id}\"" | ||
| duplicate.errors.each { |e| puts e.full_message } | ||
| end | ||
| 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
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,22 +1,13 @@ | ||
| class ThreadFollower < ApplicationRecord | ||
| belongs_to :comment_thread, optional: true | ||
| belongs_to :post, optional: true | ||
| belongs_to :comment_thread | ||
| belongs_to :user | ||
|
|
||
| after_create :bump_thread_last_activity | ||
| before_destroy :bump_thread_last_activity | ||
|
|
||
| validate :thread_or_post | ||
|
|
||
| private | ||
|
|
||
| def bump_thread_last_activity | ||
| comment_thread&.bump_last_activity(persist_changes: true) | ||
| end | ||
|
|
||
| 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| select | ||
| user_id, | ||
| post_id, | ||
| count(*) as count | ||
| from | ||
| new_thread_followers | ||
| group by | ||
| user_id, | ||
| post_id | ||
| having | ||
| count > 1; |
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 @@ | ||
| CleanUpNewThreadFollowersJob.perform_later |
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 |
|---|---|---|
|
|
@@ -208,3 +208,17 @@ post_scorer: | |
| sign_in_count: 1337 | ||
| username: post_scorer | ||
| confirmed_at: 2020-01-01T00:00:00.000000Z | ||
|
|
||
| merge_source: | ||
| email: [email protected] | ||
| encrypted_password: '$2a$11$roUHXKxecjyQ72Qn7DWs3.9eRCCoRn176kX/UNb/xiue3aGqf7xEW' | ||
| sign_in_count: 1337 | ||
| username: merge_source | ||
| confirmed_at: 2020-01-01T00:00:00.000000Z | ||
|
|
||
| merge_target: | ||
| email: [email protected] | ||
| encrypted_password: '$2a$11$roUHXKxecjyQ72Qn7DWs3.9eRCCoRn176kX/UNb/xiue3aGqf7xEW' | ||
| sign_in_count: 1337 | ||
| username: merge_target | ||
| confirmed_at: 2020-01-01T00:00:00.000000Z | ||
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).