From ef92b72ff667ff19ba4a9bf8cd56b338c65a6c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 4 Aug 2020 14:57:49 +0200 Subject: [PATCH] DEV: ensure 'posts:reorder_posts' rake task work on single topic when the topic_id argument is provided. --- lib/tasks/posts.rake | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) 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.", ""