[15 min fix] Document data update scripts best practices (#13432)
* Add data update scripts best practices * Remove leftover counter
This commit is contained in:
parent
9d270fda16
commit
f5691dbb5e
1 changed files with 22 additions and 0 deletions
|
|
@ -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
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue