mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Mark A Migration As Irreversible as a rails til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||
|
||||
_811 TILs and counting..._
|
||||
_812 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -497,6 +497,7 @@ _811 TILs and counting..._
|
||||
- [Hash Slicing](rails/hash-slicing.md)
|
||||
- [Ignore Poltergeist JavaScript Errors](rails/ignore-poltergeist-javascript-errors.md)
|
||||
- [List The Enqueued Jobs](rails/list-the-enqueued-jobs.md)
|
||||
- [Mark A Migration As Irreversible](rails/mark-a-migration-as-irreversible.md)
|
||||
- [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md)
|
||||
- [Mark For Destruction](rails/mark-for-destruction.md)
|
||||
- [Migrating Up Down Up](rails/migrating-up-down-up.md)
|
||||
|
||||
29
rails/mark-a-migration-as-irreversible.md
Normal file
29
rails/mark-a-migration-as-irreversible.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Mark A Migration As Irreversible
|
||||
|
||||
It is in your best interest to, as much as is possible, write your Rails
|
||||
migrations in a way that they can be safely and reliably rolledback. You want
|
||||
your `down` to mirror your `up`, in case anything goes wrong.
|
||||
|
||||
This isn't always possible though. There are some migrations, in particular
|
||||
data migrations, that cannot be undone. Something is being changed or destroyed
|
||||
in an unrecoverable way. When this is the case, you should, by convention,
|
||||
raise an `IrreversibleMigration` exception.
|
||||
|
||||
```ruby
|
||||
class DestructiveMigration < ActiveRecord::Migration[5.2]
|
||||
def up
|
||||
execute "-- some destructive SQL"
|
||||
end
|
||||
|
||||
def down
|
||||
raise ActiveRecord::IrreversibleMigration
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
If anyone ever tries to rollback this migration, they will see the exception.
|
||||
It will be a signal that some manual work is needed to continue rolling back.
|
||||
|
||||
See the
|
||||
[docs](https://api.rubyonrails.org/classes/ActiveRecord/Migration.html#class-ActiveRecord::Migration-label-Irreversible+transformations)
|
||||
for more details.
|
||||
Reference in New Issue
Block a user