discourse/lib/tasks/bookmarks.rake
Martin Brennan 16799da580
FIX: Improve bookmark modal on mobile and bookmark sync rake task (#9221)
* Improve the bookmark mobile on modal so it doesn't go all the way to the edge and the custom datetime input is easier to use
* Improve the rake task for syncing so it does not error for topics that no longer exist and batches 2000 inserts at a time, clearing the array each time
2020-03-17 15:15:22 +10:00

64 lines
2.3 KiB
Ruby

# frozen_string_literal: true
require_dependency "rake_helpers"
##
# This will create records in the new bookmarks table from PostAction
# records. The task is idempotent, it will not create additional bookmark
# records for PostActions that have already been created in the new table.
# You can provide a sync_limit for a smaller batch run.
#
desc "migrates old PostAction bookmarks to the new Bookmark model & table"
task "bookmarks:sync_to_table", [:sync_limit] => :environment do |_t, args|
sync_limit = args[:sync_limit] || 0
post_action_bookmarks = PostAction
.select('post_actions.id', 'post_actions.post_id', 'posts.topic_id', 'post_actions.user_id')
.where(post_action_type_id: PostActionType.types[:bookmark])
.joins(:post)
.where(deleted_at: nil)
.joins('LEFT JOIN bookmarks ON bookmarks.post_id = post_actions.post_id AND bookmarks.user_id = post_actions.user_id')
.joins('INNER JOIN topics ON topics.id = posts.topic_id')
.where('bookmarks.id IS NULL')
# if sync_limit is provided as an argument this will cap
# the number of bookmarks that will be created in a run of
# this task (for huge bookmark count communities)
if sync_limit > 0
post_action_bookmarks = post_action_bookmarks.limit(sync_limit)
end
post_action_bookmark_count = post_action_bookmarks.count('post_actions.id')
i = 0
post_action_bookmarks.find_each(batch_size: 2000) do |pab|
# clear at start of each batch to only insert X at a time
bookmarks_to_create = []
Bookmark.transaction do
RakeHelpers.print_status_with_label("Creating post new bookmarks.......", i, post_action_bookmark_count)
now = Time.zone.now
bookmarks_to_create << {
topic_id: pab.topic_id,
post_id: pab.post_id,
user_id: pab.user_id,
created_at: now,
updated_at: now
}
i += 1
# this will ignore conflicts in the bookmarks table so
# if the user already has a post bookmarked in the new way,
# then we don't error and keep on truckin'
#
# we shouldn't have duplicates here at any rate because of
# the above LEFT JOIN but best to be safe knowing this
# won't blow up
Bookmark.insert_all(bookmarks_to_create)
end
end
RakeHelpers.print_status_with_label("Bookmark creation complete! ", i, post_action_bookmark_count)
puts ""
end