DEV: Clean up old bookmark code (#15455)
The rake task deleted here was added back in Feb 2020
when bookmarks were first converted from PostAction
records, it is no longer needed. The ignored columns
were removed in ed83d7573e
.
This commit is contained in:
parent
a717c307ce
commit
099b679fc5
|
@ -1,11 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Bookmark < ActiveRecord::Base
|
||||
self.ignored_columns = [
|
||||
"topic_id", # TODO (martin) (2021-12-01): remove
|
||||
"reminder_type" # TODO (martin) (2021-12-01): remove
|
||||
]
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :post
|
||||
has_one :topic, through: :post
|
||||
|
|
|
@ -1,62 +0,0 @@
|
|||
# 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" => :environment do |_t, args|
|
||||
bookmarks_to_create = []
|
||||
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
|
||||
INNER JOIN users ON users.id = post_actions.user_id
|
||||
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.post_id}, #{pab.user_id}, '#{now}', '#{now}')"
|
||||
end
|
||||
|
||||
create_bookmarks(bookmarks_to_create)
|
||||
bookmarks_to_create = []
|
||||
end # loop
|
||||
|
||||
puts "Bookmark creation complete!"
|
||||
end
|
||||
|
||||
def create_bookmarks(bookmarks_to_create)
|
||||
return if bookmarks_to_create.empty?
|
||||
|
||||
# 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
|
||||
#
|
||||
DB.exec(
|
||||
<<~SQL
|
||||
INSERT INTO bookmarks (post_id, user_id, created_at, updated_at)
|
||||
VALUES #{bookmarks_to_create.join(",\n")}
|
||||
ON CONFLICT DO NOTHING
|
||||
SQL
|
||||
)
|
||||
end
|
|
@ -1,57 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "bookmarks tasks" do
|
||||
let(:user1) { Fabricate(:user) }
|
||||
let(:user2) { Fabricate(:user) }
|
||||
let(:user3) { Fabricate(:user) }
|
||||
let(:post1) { Fabricate(:post) }
|
||||
let(:post2) { Fabricate(:post) }
|
||||
let(:post3) { Fabricate(:post) }
|
||||
|
||||
before do
|
||||
Rake::Task.clear
|
||||
Discourse::Application.load_tasks
|
||||
|
||||
create_post_actions_and_existing_bookmarks
|
||||
end
|
||||
|
||||
def invoke_task(args = nil)
|
||||
capture_stdout do
|
||||
Rake::Task['bookmarks:sync_to_table'].invoke(args)
|
||||
end
|
||||
end
|
||||
|
||||
it "migrates all PostActions" do
|
||||
invoke_task
|
||||
|
||||
expect(Bookmark.all.count).to eq(3)
|
||||
end
|
||||
|
||||
it "does not create bookmarks that already exist in the bookmarks table for a user" do
|
||||
Fabricate(:bookmark, user: user1, post: post1)
|
||||
|
||||
invoke_task
|
||||
|
||||
expect(Bookmark.all.count).to eq(3)
|
||||
expect(Bookmark.where(post: post1, user: user1).count).to eq(1)
|
||||
end
|
||||
|
||||
it "skips post actions where the post topic no longer exists and does not error" do
|
||||
post1.topic.delete
|
||||
post1.reload
|
||||
expect { invoke_task }.not_to raise_error
|
||||
end
|
||||
|
||||
it "skips post actions where the post no longer exists and does not error" do
|
||||
post1.delete
|
||||
expect { invoke_task }.not_to raise_error
|
||||
end
|
||||
|
||||
def create_post_actions_and_existing_bookmarks
|
||||
Fabricate(:post_action, user: user1, post: post1, post_action_type_id: PostActionType.types[:bookmark])
|
||||
Fabricate(:post_action, user: user2, post: post2, post_action_type_id: PostActionType.types[:bookmark])
|
||||
Fabricate(:post_action, user: user3, post: post3, post_action_type_id: PostActionType.types[:bookmark])
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue