2020-02-13 14:26:02 +08:00
|
|
|
# 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"
|
2020-04-22 11:44:19 +08:00
|
|
|
task "bookmarks:sync_to_table" => :environment do |_t, args|
|
2020-04-16 09:32:21 +08:00
|
|
|
bookmarks_to_create = []
|
2020-04-22 11:44:19 +08:00
|
|
|
loop do
|
|
|
|
# post action type id 1 is :bookmark. we do not need to OFFSET here for
|
|
|
|
# paging because the WHERE bookmarks.id IS NULL clause handles this effectively,
|
|
|
|
# because we do not get bookmarks back that have already been inserted
|
|
|
|
post_action_bookmarks = DB.query(
|
|
|
|
<<~SQL, type_id: 1
|
|
|
|
SELECT post_actions.id, post_actions.post_id, posts.topic_id, post_actions.user_id
|
|
|
|
FROM post_actions
|
|
|
|
INNER JOIN posts ON posts.id = post_actions.post_id
|
|
|
|
LEFT JOIN bookmarks ON bookmarks.post_id = post_actions.post_id AND bookmarks.user_id = post_actions.user_id
|
|
|
|
INNER JOIN topics ON topics.id = posts.topic_id
|
2020-05-23 12:56:13 +08:00
|
|
|
INNER JOIN users ON users.id = post_actions.user_id
|
2020-04-22 11:44:19 +08:00
|
|
|
WHERE bookmarks.id IS NULL AND post_action_type_id = :type_id AND post_actions.deleted_at IS NULL AND posts.deleted_at IS NULL
|
|
|
|
LIMIT 2000
|
|
|
|
SQL
|
|
|
|
)
|
|
|
|
break if post_action_bookmarks.count.zero?
|
|
|
|
|
|
|
|
post_action_bookmarks.each do |pab|
|
|
|
|
now = Time.zone.now
|
|
|
|
bookmarks_to_create << "(#{pab.topic_id}, #{pab.post_id}, #{pab.user_id}, '#{now}', '#{now}')"
|
2020-03-17 13:15:22 +08:00
|
|
|
end
|
2020-02-13 14:26:02 +08:00
|
|
|
|
2020-04-22 11:44:19 +08:00
|
|
|
create_bookmarks(bookmarks_to_create)
|
|
|
|
bookmarks_to_create = []
|
|
|
|
end # loop
|
2020-04-16 09:32:21 +08:00
|
|
|
|
2020-04-22 11:44:19 +08:00
|
|
|
puts "Bookmark creation complete!"
|
2020-02-13 14:26:02 +08:00
|
|
|
end
|
2020-04-16 09:32:21 +08:00
|
|
|
|
|
|
|
def create_bookmarks(bookmarks_to_create)
|
2020-04-22 11:44:19 +08:00
|
|
|
return if bookmarks_to_create.empty?
|
|
|
|
|
2020-04-16 09:32:21 +08:00
|
|
|
# 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
|
2020-04-22 11:44:19 +08:00
|
|
|
#
|
|
|
|
DB.exec(
|
|
|
|
<<~SQL
|
|
|
|
INSERT INTO bookmarks (topic_id, post_id, user_id, created_at, updated_at)
|
|
|
|
VALUES #{bookmarks_to_create.join(",\n")}
|
|
|
|
ON CONFLICT DO NOTHING
|
|
|
|
SQL
|
|
|
|
)
|
2020-04-16 09:32:21 +08:00
|
|
|
end
|