# 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/mark-a-migration-as-irreversible.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
