From d602553e93dfbe058449f1add7ad4666c2fd00f2 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 18 May 2019 15:15:52 -0500 Subject: [PATCH] Add Mark A Migration As Irreversible as a rails til --- README.md | 3 ++- rails/mark-a-migration-as-irreversible.md | 29 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 rails/mark-a-migration-as-irreversible.md diff --git a/README.md b/README.md index b848c99..ed8c4ae 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/rails/mark-a-migration-as-irreversible.md b/rails/mark-a-migration-as-irreversible.md new file mode 100644 index 0000000..0d6675a --- /dev/null +++ b/rails/mark-a-migration-as-irreversible.md @@ -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.