FIX: Topic user bookmarked column is out of sync after post moves (#12612)

When posts are moved from one topic to another, the `topic_user.bookmarked` column for all users in the new and the old topic needs to be resynced, for example because a user bookmarks post 12 in topic 1, then it is moved to topic 2, the topic_user record for topic 1 should no longer be bookmarked. A background job has been added to sync the column for a specified topic, or for no topic at all, which does it for all topics like the migration.

Also includes a migration that we have run in the past to fix bad data.

----

This has been addressed in other places in the past:

https://github.com/discourse/discourse/pull/10211
https://github.com/discourse/discourse/pull/10188
This commit is contained in:
Martin Brennan
2021-04-14 09:10:53 +10:00
committed by GitHub
parent 71475a1a01
commit 66d17fdd6b
5 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
class FixTopicUserBookmarkedSyncIssuesAgain < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def up
DB.exec(
<<~SQL
UPDATE topic_users SET bookmarked = true
FROM bookmarks AS b
WHERE NOT topic_users.bookmarked AND topic_users.topic_id = b.topic_id AND topic_users.user_id = b.user_id
SQL
)
DB.exec(
<<~SQL
UPDATE topic_users SET bookmarked = false
WHERE topic_users.bookmarked AND (SELECT COUNT(*) FROM bookmarks WHERE topic_id = topic_users.topic_id AND user_id = topic_users.user_id) = 0
SQL
)
end
def down
raise ActiveRecord::IrreversibleMigration
end
end