PERF: Split loading of posts to speed up user renames
This commit is contained in:
parent
abddb48260
commit
c3b6811651
|
@ -24,33 +24,31 @@ module Jobs
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_posts
|
def update_posts
|
||||||
Post.with_deleted.where(post_conditions("posts.id"), post_condition_args).find_each do |post|
|
updated_post_ids = Set.new
|
||||||
begin
|
|
||||||
post.raw = update_raw(post.raw)
|
|
||||||
post.cooked = update_cooked(post.cooked)
|
|
||||||
|
|
||||||
# update without running validations and hooks
|
Post.with_deleted
|
||||||
post.update_columns(raw: post.raw, cooked: post.cooked)
|
.joins(mentioned("posts.id"))
|
||||||
|
.where("a.user_id = :user_id", user_id: @user_id)
|
||||||
|
.find_each do |post|
|
||||||
|
|
||||||
SearchIndexer.index(post, force: true) if post.topic
|
update_post(post)
|
||||||
rescue => e
|
updated_post_ids << post.id
|
||||||
Discourse.warn_exception(e, message: "Failed to update post with id #{post.id}")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Post.with_deleted
|
||||||
|
.joins(quoted("posts.id"))
|
||||||
|
.where("p.user_id = :user_id", user_id: @user_id)
|
||||||
|
.find_each { |post| update_post(post) unless updated_post_ids.include?(post.id) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_revisions
|
def update_revisions
|
||||||
PostRevision.where(post_conditions("post_revisions.post_id"), post_condition_args).find_each do |revision|
|
PostRevision.joins(mentioned("post_revisions.post_id"))
|
||||||
begin
|
.where("a.user_id = :user_id", user_id: @user_id)
|
||||||
if revision.modifications.key?("raw") || revision.modifications.key?("cooked")
|
.find_each { |revision| update_revision(revision) }
|
||||||
revision.modifications["raw"]&.map! { |raw| update_raw(raw) }
|
|
||||||
revision.modifications["cooked"]&.map! { |cooked| update_cooked(cooked) }
|
PostRevision.joins(quoted("post_revisions.post_id"))
|
||||||
revision.save!
|
.where("p.user_id = :user_id", user_id: @user_id)
|
||||||
end
|
.find_each { |revision| update_revision(revision) }
|
||||||
rescue => e
|
|
||||||
Discourse.warn_exception(e, message: "Failed to update post revision with id #{revision.id}")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_notifications
|
def update_notifications
|
||||||
|
@ -95,28 +93,41 @@ module Jobs
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def post_conditions(post_id_column)
|
def update_post(post)
|
||||||
|
post.raw = update_raw(post.raw)
|
||||||
|
post.cooked = update_cooked(post.cooked)
|
||||||
|
|
||||||
|
post.update_columns(raw: post.raw, cooked: post.cooked)
|
||||||
|
|
||||||
|
SearchIndexer.index(post, force: true) if post.topic
|
||||||
|
rescue => e
|
||||||
|
Discourse.warn_exception(e, message: "Failed to update post with id #{post.id}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_revision(revision)
|
||||||
|
if revision.modifications.key?("raw") || revision.modifications.key?("cooked")
|
||||||
|
revision.modifications["raw"]&.map! { |raw| update_raw(raw) }
|
||||||
|
revision.modifications["cooked"]&.map! { |cooked| update_cooked(cooked) }
|
||||||
|
revision.save!
|
||||||
|
end
|
||||||
|
rescue => e
|
||||||
|
Discourse.warn_exception(e, message: "Failed to update post revision with id #{revision.id}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def mentioned(post_id_column)
|
||||||
<<~SQL
|
<<~SQL
|
||||||
EXISTS(
|
JOIN user_actions AS a ON (a.target_post_id = #{post_id_column} AND
|
||||||
SELECT 1
|
a.action_type = #{UserAction::MENTION})
|
||||||
FROM user_actions AS a
|
|
||||||
WHERE a.target_post_id = #{post_id_column} AND
|
|
||||||
a.action_type = :mentioned AND
|
|
||||||
a.user_id = :user_id
|
|
||||||
) OR EXISTS(
|
|
||||||
SELECT 1
|
|
||||||
FROM quoted_posts AS q
|
|
||||||
JOIN posts AS p ON (q.quoted_post_id = p.id)
|
|
||||||
WHERE q.post_id = #{post_id_column} AND
|
|
||||||
p.user_id = :user_id
|
|
||||||
)
|
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_condition_args
|
def quoted(post_id_column)
|
||||||
{ mentioned: UserAction::MENTION, user_id: @user_id }
|
<<~SQL
|
||||||
|
JOIN quoted_posts AS q ON (q.post_id = #{post_id_column})
|
||||||
|
JOIN posts AS p ON (q.quoted_post_id = p.id)
|
||||||
|
SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_raw(raw)
|
def update_raw(raw)
|
||||||
|
|
Loading…
Reference in New Issue