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:
41
rails/ensure-a-rake-task-cannot-write-data.md
Normal file
41
rails/ensure-a-rake-task-cannot-write-data.md
Normal 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/)
|
||||
Reference in New Issue
Block a user