[15 min fix] Document data update scripts best practices (#13432)

* Add data update scripts best practices

* Remove leftover counter
This commit is contained in:
rhymes 2021-04-19 16:11:10 +02:00 committed by GitHub
parent 9d270fda16
commit f5691dbb5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -79,3 +79,25 @@ DataUpdateScripts are also run automatically when a production deploy goes out.
However, to ensure the new code they need to use has been deployed we use a
[`DataUpdateWorker`](https://github.com/forem/forem/blob/main/app/workers/data_update_worker.rb)
via Sidekiq and set it to run 10 minutes after the deploy script has completed.
## Best practices
### Working with large collections of rows
From time to time, scripts need to operate on a large amount of rows; in those
cases we encourage:
- adding explicit logging to the script
- reversing the order, to start processing the most recent records first
For example:
```ruby
def run
Article.find_each(order: :desc).with_index do |article, index|
Rails.logging.info("...") if index % 1000 == 0 # this will log every 1000 articles
article.save
end
end
```