2020-02-13 01:26:02 -05: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-21 23:44:19 -04:00
|
|
|
task "bookmarks:sync_to_table" => :environment do |_t, args|
|
2020-04-15 21:32:21 -04:00
|
|
|
bookmarks_to_create = []
|
2020-04-21 23:44:19 -04: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 00:56:13 -04:00
|
|
|
INNER JOIN users ON users.id = post_actions.user_id
|
2020-04-21 23:44:19 -04: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
|
2021-09-14 20:16:54 -04:00
|
|
|
bookmarks_to_create << "(#{pab.post_id}, #{pab.user_id}, '#{now}', '#{now}')"
|
2020-03-17 01:15:22 -04:00
|
|
|
end
|
2020-02-13 01:26:02 -05:00
|
|
|
|
2020-04-21 23:44:19 -04:00
|
|
|
create_bookmarks(bookmarks_to_create)
|
|
|
|
bookmarks_to_create = []
|
|
|
|
end # loop
|
2020-04-15 21:32:21 -04:00
|
|
|
|
2020-04-21 23:44:19 -04:00
|
|
|
puts "Bookmark creation complete!"
|
2020-02-13 01:26:02 -05:00
|
|
|
end
|
2020-04-15 21:32:21 -04:00
|
|
|
|
|
|
|
def create_bookmarks(bookmarks_to_create)
|
2020-04-21 23:44:19 -04:00
|
|
|
return if bookmarks_to_create.empty?
|
|
|
|
|
2020-04-15 21:32:21 -04: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-21 23:44:19 -04:00
|
|
|
#
|
|
|
|
DB.exec(
|
|
|
|
<<~SQL
|
2021-09-14 20:16:54 -04:00
|
|
|
INSERT INTO bookmarks (post_id, user_id, created_at, updated_at)
|
2020-04-21 23:44:19 -04:00
|
|
|
VALUES #{bookmarks_to_create.join(",\n")}
|
|
|
|
ON CONFLICT DO NOTHING
|
|
|
|
SQL
|
|
|
|
)
|
2020-04-15 21:32:21 -04:00
|
|
|
end
|