1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00

Add Ensure A Rake Task Cannot Write Data as a Rails TIL

This commit is contained in:
jbranchaud
2023-02-03 10:44:55 -06:00
parent ead1ba4c12
commit 7212785cb3
2 changed files with 43 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1281 TILs and counting..._ _1282 TILs and counting..._
--- ---
@@ -789,6 +789,7 @@ _1281 TILs and counting..._
- [Demodulize A Class Name](rails/demodulize-a-class-name.md) - [Demodulize A Class Name](rails/demodulize-a-class-name.md)
- [Different Ways To Add A Foreign Key Reference](rails/different-ways-to-add-a-foreign-key-reference.md) - [Different Ways To Add A Foreign Key Reference](rails/different-ways-to-add-a-foreign-key-reference.md)
- [Disambiguate Where In A Joined Relation](rails/disambiguate-where-in-a-joined-relation.md) - [Disambiguate Where In A Joined Relation](rails/disambiguate-where-in-a-joined-relation.md)
- [Ensure A Rake Task Cannot Write Data](rails/ensure-a-rake-task-cannot-write-data.md)
- [Ensure Migrations Use The Latest Schema](rails/ensure-migrations-use-the-latest-schema.md) - [Ensure Migrations Use The Latest Schema](rails/ensure-migrations-use-the-latest-schema.md)
- [Find Or Create A Record With FactoryBot](rails/find-or-create-a-record-with-factory-bot.md) - [Find Or Create A Record With FactoryBot](rails/find-or-create-a-record-with-factory-bot.md)
- [Find Records With Multiple Associated Records](rails/find-records-with-multiple-associated-records.md) - [Find Records With Multiple Associated Records](rails/find-records-with-multiple-associated-records.md)

View File

@@ -0,0 +1,41 @@
# Ensure A Rake Task Cannot Write Data
Let's say we are writing a substantially complex rake task for aggregating,
arranging, and exporting data from our app's database. The idea is to run this
rake task against production.
There is no part of this rake task that needs to write data. It should act as a
read-only script. And in fact, we can ensure it isn't able to write any data.
We can do that by putting `ApplicationRecord` in read-only mode.
```ruby
task big_export_rake_task: :environment do
class ApplicationRecord
def readonly?
true
end
end
# a bunch of logic ...
puts 'this gets executed'
# Call method that inadvertently writes data
User.update(email: 'readonly@email.com')
# more logic ...
puts 'this does not get executed'
end
```
Because we have made all of `ApplicationRecord` read-only, when we run this
task, it is immediately going to rollback the changes that were attempted and
then raise an error which aborts the rest of the rake task.
We'll see some messaging like this:
```
rake aborted!
ActiveRecord::ReadOnlyRecord: User is marked as readonly
```
h/t [Dillon Hafer](https://dillonhafer.com/)