FIX: Delete extraneous topic bookmarks (#16926)

Otherwise the migration can fail

Co-authored-by: David Taylor <david@taylorhq.com>
This commit is contained in:
Daniel Waterworth 2022-05-26 11:58:15 -05:00 committed by GitHub
parent 95e7e10417
commit f00e282067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 0 deletions

View File

@ -14,6 +14,17 @@ class BackfillPolymorphicBookmarksAndMakeDefault < ActiveRecord::Migration[7.0]
WHERE NOT bookmarks.for_topic AND bookmarkable_id IS NULL
SQL
DB.exec(<<~SQL)
DELETE FROM bookmarks
WHERE id NOT IN (
SELECT MIN(bookmarks.id)
FROM bookmarks
INNER JOIN posts ON bookmarks.post_id = posts.id
WHERE bookmarks.for_topic
GROUP BY (bookmarks.user_id, posts.topic_id)
)
SQL
DB.exec(<<~SQL)
UPDATE bookmarks
SET bookmarkable_id = posts.topic_id, bookmarkable_type = 'Topic'