DEV: ensure 'posts:reorder_posts' rake task work on single topic

when the topic_id argument is provided.
This commit is contained in:
Régis Hanol 2020-08-04 14:57:49 +02:00
parent 8a0478b97d
commit ef92b72ff6
1 changed files with 16 additions and 10 deletions

View File

@ -338,8 +338,8 @@ task 'posts:reorder_posts', [:topic_id] => [:environment] do |_, args|
p.post_number <> o.new_post_number p.post_number <> o.new_post_number
SQL SQL
builder.where("topic_id = :topic_id") if args[:topic_id] builder.where("topic_id = ?", args[:topic_id]) if args[:topic_id]
builder.exec(topic_id: args[:topic_id]) builder.exec
[ [
["notifications", "post_number"], ["notifications", "post_number"],
@ -349,19 +349,22 @@ task 'posts:reorder_posts', [:topic_id] => [:environment] do |_, args|
["topic_users", "highest_seen_post_number"], ["topic_users", "highest_seen_post_number"],
["topic_users", "last_emailed_post_number"], ["topic_users", "last_emailed_post_number"],
].each do |table, column| ].each do |table, column|
DB.exec <<~SQL builder = DB.build <<~SQL
UPDATE UPDATE
#{table} AS x #{table} AS x
SET SET
#{column} = p.sort_order * -1 #{column} = p.sort_order * -1
FROM FROM
posts AS p posts AS p
WHERE /*where*/
p.post_number < 0 AND
x.topic_id = p.topic_id AND
x.#{column} = ABS(p.post_number)
SQL SQL
builder.where("p.topic_id = ?", args[:topic_id]) if args[:topic_id]
builder.where("p.post_number < 0")
builder.where("x.topic_id = p.topic_id")
builder.where("x.#{column} = ABS(p.post_number)")
builder.exec
DB.exec <<~SQL DB.exec <<~SQL
UPDATE UPDATE
#{table} #{table}
@ -372,15 +375,18 @@ task 'posts:reorder_posts', [:topic_id] => [:environment] do |_, args|
SQL SQL
end end
DB.exec <<~SQL builder = DB.build <<~SQL
UPDATE UPDATE
posts posts
SET SET
post_number = sort_order post_number = sort_order
WHERE /*where*/
post_number < 0
SQL SQL
builder.where("topic_id = ?", args[:topic_id]) if args[:topic_id]
builder.where("post_number < 0")
builder.exec
end end
puts "", "Done.", "" puts "", "Done.", ""