fix: retrieve posts in order by id.

fix: retrieve posts in order by id.

so we can find starting and ending id of the batch.

diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rake
index 9f612a5..c237ebe 100644
--- a/lib/tasks/posts.rake
+++ b/lib/tasks/posts.rake
@@ -399,7 +399,7 @@ task 'posts:missing_uploads' => :environment do
   PostCustomField.where(name: Post::MISSING_UPLOADS).delete_all
   count = 0
 
-  Post.have_uploads.select(:id, :cooked).find_in_batches do |posts|
+  Post.have_uploads.order(:id).select(:id, :cooked).find_in_batches do |posts|
     ids = posts.pluck(:id)
     sha1s = Upload.joins(:post_uploads).where("post_uploads.post_id >= ? AND post_uploads.post_id <= ?", ids.min, ids.max).pluck(:sha1)

GitHub sha: 48cedf3e

I thought order wasn’t applied when using find_each and find_in_batches?

From the doc

NOTE: It’s not possible to set the order. That is automatically set to ascending on the primary key (“id ASC”) to make the batch ordering work. This also means that this method only works with integer-based primary keys.

1 Like

Good to know. Thanks! :heart:

1 Like

It is removed in commit Add more missing upload stats in rake task · discourse/discourse@ba6369e · GitHub.

1 Like