In a Rails 8.1 app, how do you drop the legacy_payload column from a busy events table without errors from servers still running the old code?
answer
- columns cached per process
- INSERT names every known column
- self.ignored_columns += [...] ships first
- safety_assured { remove_column } next release
- NOT NULL column made nullable first
basics
~10 sShip it in two releases: first add self.ignored_columns += ["legacy_payload"] to Event and remove every use, then deploy a migration with safety_assured { remove_column :events, :legacy_payload, :jsonb }, and finally delete the ignore line.
solid answer
~40 sEach Rails process loads a model's column list once and caches it. With `load_defaults` 7.0 and later, `partial_inserts` is false, so every `Event.create` names every known column, including `legacy_payload`. Drop the column while old processes still run and their inserts fail with `ActiveRecord::StatementInvalid`. So release one adds `self.ignored_columns += ["legacy_payload"]` to `Event` and removes the code that reads it: the attribute disappears and Active Record lists explicit columns without it in `SELECT` and `INSERT`. Release two runs `remove_column :events, :legacy_payload, :jsonb`, wrapped in `safety_assured` because strong_migrations stops every `remove_column`. A later commit deletes the ignore line. If the column is `NOT NULL` without a default, make it nullable first, or inserts that leave it out will fail.
code
ruby · 11 lines# Release 1
class Event < ApplicationRecord
self.ignored_columns += ["legacy_payload"]
end
# Release 2
class RemoveLegacyPayloadFromEvents < ActiveRecord::Migration[8.1]
def change
safety_assured { remove_column :events, :legacy_payload, :jsonb }
end
endgo deeper
Recall the order: ignore the column in the model and deploy, then drop it in a migration, then delete the ignore line.
Explain why the order matters: each process caches columns, and with partial_inserts false every insert lists every column it knows.
Cover the traps: NOT NULL columns made nullable first, raw SQL readers, += not =, a short lock timeout for the drop, and renames done as add, copy, drop.
Set the policy that every column removal and rename goes through this sequence, and decide how the team tracks ignore lines that must be cleaned up.
## Why a plain drop breaks a live app The `events` table of an analytics product once stored a raw `legacy_payload` JSON blob. Nothing reads it any more, and it is time to drop it. The tempting migration is one line, `remove_column :events, :legacy_payload`, but during a deploy **old and new processes run side by side**: Puma workers and job workers from the previous release keep serving while the migration runs and the new release boots. Those old processes have already loaded `Event`'s **column list** and cached it. Active Record does not re-read the schema on every query. Two consequences follow: - **Inserts name every column.** Since `load_defaults` 7.0, `partial_inserts` is `false`, so `Event.create(...)` writes an `INSERT` that lists every attribute the model knows, including `legacy_payload` with a `NULL`. After the drop, that statement fails with `ActiveRecord::StatementInvalid` (on PostgreSQL wrapping `PG::UndefinedColumn`). - **Any code that still reads or writes the attribute** fails the same way, now in SQL rather than in Ruby. strong_migrations exists partly for this case: it stops every `remove_column` with a message telling you to ignore the column first. ## The sequence across releases 1. **Release 1, code only.** Add the ignore line and remove every use of the attribute: ```ruby class Event < ApplicationRecord self.ignored_columns += ["legacy_payload"] end ``` Now `Event` has no `legacy_payload` attribute (calling it raises `NoMethodError`), `Event.columns_hash["legacy_payload"]` is `nil`, and because the model has ignored columns, its queries list explicit columns instead of `"events".*`. Inserts no longer name the column. 2. **Wait until no process from the previous release is left.** The ignore line must be live everywhere before the column goes. 3. **Release 2, the migration.** `safety_assured { remove_column :events, :legacy_payload, :jsonb }`. The type argument keeps the migration reversible. Old processes from release 1 already ignore the column, so nothing they send mentions it. 4. **Release 3, or any later commit.** Delete the `ignored_columns` line; leaving it would silently hide a future column of the same name. ## The traps | Trap | What happens | Fix | |---|---|---| | `ignored_columns` and `remove_column` in the same deploy | old processes still have the column cached when it disappears | two releases, in order | | column is `NOT NULL` with no default | once ignored, inserts leave it out and the database rejects them with `ActiveRecord::NotNullViolation` | a migration with `change_column_null :events, :legacy_payload, true` before or with release 1 | | `self.ignored_columns = [...]` | replaces any list a parent class set | use `+=` | | raw SQL or another service reads the column | `ignored_columns` only covers Active Record models | search for the column name, not just the attribute | The `NOT NULL` trap is the one that surprises people: the ignore line removes the column from inserts, so the database fills in the default, and with no default and a `NOT NULL` constraint the insert fails. Making the column nullable first is cheap on PostgreSQL, and strong_migrations does not object to `change_column_null` in that direction. ## Checking you are ready Before release 2, confirm that nothing still uses the attribute. The test suite is the cheapest check: with the ignore line in place, any code that calls `event.legacy_payload` raises `NoMethodError`. A `where(legacy_payload: ...)` hash is not caught that way: it still sends the column name to the database and works until the column is gone, so search for those too. Search the codebase for the column name in raw SQL, Arel and views as well, and ask whether a reporting tool or another service reads the table directly; `ignored_columns` protects only Active Record models in this app. ## Renaming is a drop in disguise `rename_column` breaks old processes the same way, because they still write the old name. strong_migrations stops it outright. The live-safe path is a new column, writes to both, a backfill, reads switched to the new column, and only then the old one dropped with the sequence above. The general theory of those phases belongs to schema-evolution material; the Rails part is that every drop at the end goes through `ignored_columns` first. ## What the migration itself costs On PostgreSQL, dropping a column is quick: it marks the column as dropped in the catalogue instead of rewriting the table. It still needs a brief exclusive lock on `events`, so run it with a short lock timeout, which the strong_migrations initializer sets to 10 seconds, so it gives up rather than queueing every query behind a long-running transaction.
- legacy_payload is NOT NULL with no default. What changes in the plan?Once the column is ignored, `Event.create` leaves it out of the `INSERT`, and the database rejects the row with `ActiveRecord::NotNullViolation`. Ship a migration with `change_column_null :events, :legacy_payload, true` before or alongside the ignore line, then continue with the drop in the next release.
- Why delete the ignored_columns line after the drop?It is dead configuration once the column is gone, and it would hide a future column with the same name: the attribute would not exist and nobody would know why. Removing it in a later commit keeps the model honest.
- Why does strong_migrations not notice that the ignore line has shipped?The checker sees only the migration's method calls, not which release is running in production. It cannot tell whether the ignore line is deployed yet, so it stops every `remove_column`, and `safety_assured` is how you state that the first release is out.
- ship ignored_columns and remove_column in the same deploy
- Active Record re-reads the columns on every query, so a drop is harmless
- use self.ignored_columns = [...] to start a fresh list
- ignored_columns also hides the column from raw SQL and other services
- remove_column on PostgreSQL rewrites the whole events table
Assumes Rails 8.1 with load_defaults 8.1; partial_inserts has been false since the 7.0 defaults, so every INSERT names every column the model knows.