An online-auction Rails app has 900 migration files going back years; how do you prune old migrations safely, and what changes afterwards?
answer
- the schema file is the rebuild source
- every environment must have run them
- old rows stay in schema_migrations
- rollback cannot enter the pruned range
- keep engine migrations and revert targets
basics
~20 sDelete migration files only after every environment has run them and the committed schema file is correct, because new databases load that file. Old schema_migrations rows stay and show as NO FILE, and rollbacks can no longer reach the pruned range.
solid answer
~50 sPruning works because the schema file, not the migration history, rebuilds databases: `db:prepare`, `db:setup` and, since Rails 8.0, `db:migrate` on an empty database load it. Before deleting files, check that **every** environment and active branch has applied them; any database that has not would never get those changes. Choose a cut-off well below anything still pending anywhere, confirm `db/schema.rb` (or `structure.sql`) matches a migrated database, delete the old files, and commit. Afterwards the old versions stay in `schema_migrations` and `db:migrate:status` lists them as up with `********** NO FILE **********`, which is harmless. You lose the ability to roll back into the pruned range and to replay history with `db:migrate:reset`. Keep migrations copied from engines, because the install task would copy them again with new timestamps, and keep any file a later migration loads for `revert`.
code
bash · 3 linesRAILS_ENV=production bin/rails db:migrate:status | grep down # nothing pending
git rm db/migrate/2023*.rb db/migrate/2024*.rb
bin/rails db:migrate:status | head # pruned versions now show NO FILEgo deeper
Know that the schema file, not the migration history, is what new databases are built from.
Explain what db:migrate:status shows for deleted files and why loading the schema still works afterwards.
Run a pruning safely: verify every environment and branch, keep engine and revert-target files, and accept the loss of rollback depth and full replay.
Decide whether and how often to prune, weighing a lean history against the replay check and rollback depth the team gives up.
## Why prune at all The auction app's `db/migrate` holds 900 files. Most of them created tables that were later changed, renamed or dropped. They slow down searches, they mention models that no longer exist, and some no longer run at all. The Rails migrations guide says it plainly: the schema file "is the authoritative source for rebuilding that database. This makes it possible to delete or prune old migration files." ## Preconditions Pruning is safe only when nothing will ever need to **run** the deleted files again: 1. **Every database has applied them.** Production, staging, CI, and every developer database. Check `db:migrate:status` or `schema_migrations` in each shared environment. A database that has not run a deleted migration will never get its change. 2. **No open branch depends on them.** Pick a cut-off older than any migration still pending on a long-lived branch. 3. **The schema file is right.** Regenerate it from a migrated database and confirm it is committed, because every new database will come from it. If the app relies on triggers or views, that means `structure.sql`. 4. **Reference data lives elsewhere.** Rows that old migrations inserted must come from seeds, since the schema file carries structure only. ## A pruning recipe 1. Pick a cut-off version, for example everything before 2025. 2. Confirm the preconditions above for that version. 3. Delete the files below the cut-off in one commit, with a message naming the cut-off. 4. Run the test suite on a database built from the schema file, which is what new environments will get. 5. Leave `schema_migrations` alone; the old rows do no harm. ## What changes afterwards - **Status output.** `db:migrate:status` lists each pruned version as `up` with `********** NO FILE **********`, because the row exists and the file does not. - **New databases.** Unchanged: they load the schema file, and loading marks every remaining file below its version as applied. - **Rollback range.** `db:rollback` and `db:migrate:down` can only run migrations whose files exist, so you cannot roll back into the pruned range. Never prune the newest migrations: if the file of the current highest version is gone, rollback raises `ActiveRecord::UnknownMigrationVersionError`. - **Full replay.** `db:migrate:reset` replays only the remaining files from an empty database. The first remaining migration probably alters a table that a deleted one created, so the replay fails. After pruning, the schema file is the only rebuild path. ## Checking that it worked 1. Build a database from scratch with `bin/rails db:prepare` on an empty machine or container, and run the test suite against it. 2. Run `bin/rails db:migrate:status` on a long-lived database and confirm the pruned versions appear as NO FILE while nothing shows as down. 3. Generate a new migration and run it, to confirm the remaining history still migrates forward. 4. Deploy as usual: production's `db:migrate` finds nothing new to run from the pruned range, because those rows already exist. ## Files to keep | Keep | Why | |---|---| | Migrations copied from engines | the guide warns that re-running an engine's install task copies missing ones again with new timestamps, and `db:migrate` would try to run them | | Files loaded by a later `revert` | a migration that does `require_relative` on an old file fails to load once that file is gone | | Anything still pending somewhere | it has not run everywhere yet | Engine migrations carry a comment such as `# This migration comes from blorgh (originally 20210621082949)`, which makes them easy to spot. ## What you give up - **History in the working tree.** Git still has it, which is usually enough for archaeology. - **Rollback depth.** Only migrations above the cut-off can be undone. - **Replay as a check.** You can no longer prove the full history still runs, because it no longer exists. That is acceptable when the schema file is treated as the record. Teams often prune once a year or at a major upgrade, when they are rewriting old migrations that no longer load anyway.
- Why does db:migrate:reset stop working after pruning?It drops the database, writes an empty schema file and replays the remaining migration files from scratch. The oldest remaining migration usually changes a table that a deleted migration created, so it fails on the empty database. After pruning, new databases must come from the schema file.
- Should you delete the pruned versions from schema_migrations too?There is no need. The rows only make `db:migrate:status` print NO FILE lines. Deleting them by hand on shared databases risks removing the wrong rows, which would make Rails treat a migration that still exists as pending.
- After pruning you must also delete the matching rows from schema_migrations
- New environments still need the old migration files to build their database
- It is safe to prune a migration that has not yet run in production
- Engine migrations can be pruned freely because the install task skips them
- Pruning removes the pruned migrations' tables from db/schema.rb