A Rails 8.1 courier app shipped AddLegacyBarcodeToShipments to production; how do you undo it with a new migration using revert, and where do reversible and up_only fit?
answer
- a new migration, not an edit
- revert ClassName runs it inverted
- require_relative the old file
- dir.up and dir.down pick a direction
- up_only skips on rollback
basics
~20 sWrite a new migration whose change calls revert AddLegacyBarcodeToShipments after require_relative of that file; db:migrate runs the old migration inverted. reversible lets one step pick SQL per direction, and up_only runs a step only on the way up.
solid answer
~40 sA migration that already ran in production is undone with a **new** migration, so every environment picks it up through `db:migrate`. Inside its `change`, `revert AddLegacyBarcodeToShipments` runs that migration in the opposite direction: an old `change` is replayed through the command recorder and inverted, and an old `up`/`down` pair has its `down` called. Migration files are not autoloaded, so the new file needs `require_relative "20260901101500_add_legacy_barcode_to_shipments"`. The new migration is reversible itself: rolling it back runs the old one forwards again, re-adding the column, empty. `revert` also takes a block to invert a few inline calls. `reversible do |dir| dir.up { ... }; dir.down { ... } end` handles SQL that the recorder cannot invert, and `up_only { ... }` runs a backfill only when migrating up.
code
ruby · 10 linesrequire_relative "20260901101500_add_legacy_barcode_to_shipments"
class RevertLegacyBarcode < ActiveRecord::Migration[8.1]
def change
revert AddLegacyBarcodeToShipments
add_column :shipments, :carrier_code, :string
up_only { execute "UPDATE shipments SET carrier_code = 'LOCAL'" }
end
endgo deeper
Know that a migration that already ran is undone by a new migration rather than by editing the old file, and that up_only runs only on the way up.
Explain what reversible's dir.up and dir.down do on migrate and on rollback, and how a revert block inverts the calls inside it.
Plan the revert of a shipped migration: require the file, confirm the old migration is reversible, accept the data loss, and know that rolling back re-adds an empty column.
Decide when the team reverts in a new migration and when it accepts a column it no longer needs, weighing data loss, deploy risk and how much history to keep.
## The situation A courier app added a `legacy_barcode` column in `AddLegacyBarcodeToShipments`. It has run in production, staging and every developer database, and the team now wants it gone. Editing the old file changes nothing on databases that already ran it, and a rollback on one machine does not reach the others. The Rails tool for this is a **new migration that reverts the old one**. ## revert with a migration class ```ruby require_relative "20260901101500_add_legacy_barcode_to_shipments" class RevertLegacyBarcode < ActiveRecord::Migration[8.1] def change revert AddLegacyBarcodeToShipments end end ``` What happens: 1. **On `db:migrate`**, `revert` runs the old migration in the down direction. If the old class defines `change`, Rails replays it through the command recorder and runs the inverses; if it defines `up` and `down`, Rails calls its `down`. 2. **The version table** gains a row for the new migration. The old migration's row stays, because history is only ever added to. 3. **On rollback of the new migration**, Rails reverses the revert and runs the old migration up again, re-adding `legacy_barcode`, empty. Things to check before relying on it: - **The old migration must be reversible.** If its `change` contains `change_column` or `execute`, inverting it raises `ActiveRecord::IrreversibleMigration`, and this time the error comes on the way up, during deploy. - **The old file must be loaded.** Rails loads a migration file only when it is about to run it, so the class constant is undefined unless you `require_relative` the file. Pruning that old file later breaks this migration anywhere it still has to run. - **Data goes with the column.** Reverting drops the column and its values; rolling back brings back an empty column. ## revert with a block When only part of an old change should go, the block form inverts the calls written inside it: ```ruby def change revert { add_index :shipments, :legacy_barcode } add_index :shipments, [:courier_id, :status] end ``` The block is inverted on the way up (the old index is removed) and replayed forwards on rollback. The calls inside must be invertible for the same reason as in `change`: Rails builds their opposites with the command recorder, so `revert { change_column ... }` raises. `revert` calls can also be nested, which is how the class form is implemented when the reverting migration is itself rolled back. ## reversible for steps the recorder cannot invert Raw SQL has no automatic inverse. `reversible` yields a helper whose `up` and `down` blocks run only in the matching direction, and Rails places the whole block at the right point in the reversed sequence: ```ruby reversible do |dir| dir.up { execute "CREATE VIEW delivered_shipments AS SELECT * FROM shipments WHERE status = 'delivered'" } dir.down { execute "DROP VIEW delivered_shipments" } end ``` On `db:migrate` only the `dir.up` SQL runs; on rollback only the `dir.down` SQL runs, after the later commands in `change` have been undone. ## up_only for one-way data steps `up_only { ... }` runs its block when migrating up and skips it entirely on rollback. It suits a backfill that gives existing rows a value for a new column. Rolling back drops the column, so there is nothing to undo: ```ruby add_column :shipments, :carrier_code, :string up_only { execute "UPDATE shipments SET carrier_code = 'LOCAL'" } ``` ## A worked timeline 1. In September, `AddLegacyBarcodeToShipments` adds the column and runs everywhere. 2. In October, `RevertLegacyBarcode` is generated, given `require_relative` and `revert AddLegacyBarcodeToShipments`, and merged. 3. The deploy runs `db:migrate`; the new migration drops `legacy_barcode` and records its own version. 4. Developers pull and migrate; their databases lose the column the same way, with no manual step. 5. If the deploy must be undone, rolling back `RevertLegacyBarcode` brings back an empty `legacy_barcode` column. Any values it held are gone unless they were copied elsewhere first. ## Choosing the tool | Need | Tool | |---|---| | Undo a whole migration that already shipped | `revert OldMigrationClass` in a new migration | | Undo a few earlier calls inline | `revert { ... }` block | | SQL or a step with no automatic inverse | `reversible` with `dir.up` / `dir.down` | | A data step that should never run backwards | `up_only` | | Directions that differ throughout | separate `up` and `down` methods |
- What happens if the migration you revert used execute in its change method?`revert` replays that `change` through the command recorder, which has no inverse for `execute`, so `ActiveRecord::IrreversibleMigration` is raised while the new migration runs up, which is during deploy rather than during a rollback. Either revert only the invertible parts with a `revert { ... }` block, or write the undo yourself with `reversible` or `up`/`down`.
- Why does a model call inside change behave differently from up_only?Ruby code that is not a migration command is not recorded, so it runs every time `change` runs, including when Rails replays `change` to build a rollback. `up_only` checks the direction and runs its block only when migrating up, which is what a one-way backfill needs.
- revert removes the old migration's row from schema_migrations
- revert works only when the old migration used change, not up and down
- Code inside dir.down also runs when migrating up
- An up_only block runs in reverse when the migration is rolled back
- Rails autoloads old migration classes, so no require_relative is needed