Skip to content

A platform for community discussion. Free, open, simple.

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

Régis Hanolef92b72ff667
diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rake
index a491497380c..51aed6c96e5 100644
--- a/lib/tasks/posts.rake
+++ b/lib/tasks/posts.rake
@@ -338,8 +338,8 @@ task 'posts:reorder_posts', [:topic_id] => [:environment] do |_, args|
p.post_number <> o.new_post_number
SQL
- builder.where("topic_id = :topic_id") if args[:topic_id]
- builder.exec(topic_id: args[:topic_id])
+ builder.where("topic_id = ?", args[:topic_id]) if args[:topic_id]
+ builder.exec
[
["notifications", "post_number"],
@@ -349,19 +349,22 @@ task 'posts:reorder_posts', [:topic_id] => [:environment] do |_, args|
["topic_users", "highest_seen_post_number"],
["topic_users", "last_emailed_post_number"],
].each do |table, column|
- DB.exec <<~SQL
+ builder = DB.build <<~SQL
UPDATE
#{table} AS x
SET
#{column} = p.sort_order * -1
FROM
posts AS p
- WHERE
- p.post_number < 0 AND
- x.topic_id = p.topic_id AND
- x.#{column} = ABS(p.post_number)
+ /*where*/
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
UPDATE
#{table}
@@ -372,15 +375,18 @@ task 'posts:reorder_posts', [:topic_id] => [:environment] do |_, args|
SQL
end
- DB.exec <<~SQL
+ builder = DB.build <<~SQL
UPDATE
posts
SET
post_number = sort_order
- WHERE
- post_number < 0
+ /*where*/
SQL
+ builder.where("topic_id = ?", args[:topic_id]) if args[:topic_id]
+ builder.where("post_number < 0")
+ builder.exec
+
end
puts "", "Done.", ""