skip to content

In Rails 8.1, why build a new database with db:schema:load instead of replaying every migration, and what does loading record?

level: middleimportance: should knowfreq 45%

answer

  1. old migrations rot with the code
  2. force: :cascade recreates tables
  3. versions below the line marked applied
  4. environment and schema_sha1 recorded
  5. 8.0: fresh db:migrate loads first

basics

~20 s

Loading the schema file is one fast pass that cannot trip over old migrations whose models or gems have since changed. It recreates every table, then marks the file's version and every older migration as applied, and stores the environment and schema checksum.

solid answer

~40 s

Replaying years of migrations is slow and fragile: old migrations may call model classes, gems or helpers that changed or no longer exist. The schema file is a snapshot of the result, so `bin/rails db:schema:load` builds the same structure in one pass. For `schema.rb`, every `create_table ... force: :cascade` drops and recreates its table, so existing rows in those tables are lost, and the task refuses a database marked as production. Loading then creates `schema_migrations`, inserts the file's `version:` **and every migration file's version below it**, so `db:migrate` later runs only newer files; it also writes the environment and a `schema_sha1` checksum of the file to `ar_internal_metadata`. Since Rails 8.0, `db:migrate` on an empty database loads the schema first; `db:migrate:reset` is the way to replay everything.

code

bash · 4 lines
bash
bin/rails db:create
bin/rails db:schema:load      # structure + schema_migrations rows
bin/rails db:migrate          # only migrations newer than the file
bin/rails db:migrate:reset    # drop, create, replay every migration

go deeper

for a junior

Know that new databases are built by loading the schema file, and that loading wipes the tables it defines.

for a middle

Explain why replay is fragile, what loading writes to schema_migrations and ar_internal_metadata, and the Rails 8.0 change to db:migrate on an empty database.

for a senior

Keep the schema file trustworthy: regenerate rather than hand-merge, keep reference data in seeds, and periodically replay migrations to catch drift.

for a principal

Decide what the team treats as the source of truth for new environments and how the migration history and the dump are kept consistent.

## Two ways to get a database A new environment for the online-auction app needs every table. There are two routes: 1. **Replay the migrations**, all of them, in version order, from an empty database. 2. **Load the schema file**, `db/schema.rb` or `db/structure.sql`, which describes the end result. Rails prefers the second, and its own `schema.rb` header says why: loading "tends to be faster and is potentially less error prone than running all of your migrations from scratch. Old migrations may fail to apply correctly if those migrations use external dependencies or application code." ## Why replaying goes wrong - **Application code drifts.** A 2022 migration that called `Lot.update_all(...)` breaks once `Lot` is renamed to `Auction` or deleted. - **Gems and helpers disappear.** A migration that used a gem's method fails after the gem is removed from the Gemfile. - **Data assumptions expire.** A backfill written for the data of its day may raise on an empty table or on today's constraints. - **Time.** Hundreds of migrations, each in its own transaction, take far longer than one schema load. None of these problems exist in the schema file, which was written by inspecting a working database. ## What db:schema:load does 1. **Checks the environment.** The task depends on the protected-environment check, so it refuses a database whose `ar_internal_metadata` says production. 2. **Builds the structure.** For `schema.rb`, Rails evaluates the file: each `create_table "bids", force: :cascade` drops the table if it exists and creates it again. For `structure.sql`, the SQL is run through the database's client. 3. **Records migrations.** `schema_migrations` is created; the `version:` from the file is inserted, and so is **every migration file in `db/migrate` with a lower version** that is not there yet. With `structure.sql`, the file carries its own `INSERT INTO schema_migrations` lines. 4. **Records metadata.** `ar_internal_metadata` gets the current environment and a `schema_sha1` checksum of the file, which lets Rails tell later whether the test database is already up to date. Step 3 is what makes the next `db:migrate` run only migrations newer than the file. Step 2 is why loading into a database with data is destructive: every table the file defines comes back empty. ## Rails 8.0 and later: db:migrate loads first Before 8.0, running `db:migrate` against an empty database replayed every migration. Since 8.0: - `db:migrate` creates the database if it is missing. - If the database has no `schema_migrations` table and a schema file exists, it **loads the schema file**. - It then runs only the migrations newer than that file. `db:prepare` behaves the same way for a new database. To get the old full replay, the release notes point to `db:migrate:reset`, which drops, recreates, writes an empty schema file and runs every migration. ## What loading cannot give you | Expected | Reality | |---|---| | Rows that old migrations inserted | not restored; the schema file holds structure only, so reference data belongs in seeds | | Objects the Ruby dump omits (triggers, views, functions) | missing unless the app uses `structure.sql` | | Protection from a stale or hand-edited file | none: whatever the file says is what every new database gets | | A check that old migrations still run | none; only `db:migrate:reset` exercises them | ## A worked example A new developer joins and runs `bin/setup` on an empty laptop. The committed `db/schema.rb` says `version: 2026_10_05_093000`, and `db/migrate` holds 214 files, one of them newer: `20261006080000`. 1. `db:prepare` creates the database and loads `schema.rb`: every table appears in one pass. 2. `schema_migrations` receives `20261005093000` plus the 212 older versions found in `db/migrate`. 3. `20261006080000` has no row, so it runs as an ordinary pending migration. 4. The seed file loads, because the database was just initialised. Not one of the 212 older migrations ran, yet the database matches production's structure. ## Keeping the file trustworthy - Commit the regenerated schema file in the same change as the migration that altered it. - Never resolve a schema-file merge conflict by hand-picking lines; regenerate it by migrating. - Run `db:migrate:reset` on a scratch database now and then, and diff the schema file it writes against the committed one. A difference means the file and the migration history have drifted. - Keep reference data in seeds, not in migrations, so a database built from the schema file still gets it.

  • After db:schema:load, why does db:migrate not rerun migrations older than the file's version?
    Loading inserts the file's version and every migration file with a lower version into `schema_migrations`. `db:migrate` treats a migration as pending only when its version has no row, so the older ones are skipped and only newer files run.
  • Since Rails 8.0, how do you rebuild a development database by replaying every migration?
    Run `bin/rails db:migrate:reset`. It drops and recreates the database, writes an empty schema file, and then migrates, so every migration runs from the first. Plain `db:migrate` on an empty database now loads the schema file instead.

Loading the schema is building a new house from the current floor plan instead of repeating every renovation since the first foundation: faster, and immune to materials no longer sold, but the plan shows the rooms, not the furniture inside them.

  • db:schema:load runs the migration files in order, only faster
  • After db:schema:load, db:migrate runs every old migration again
  • db:schema:load adds missing tables to an existing database without touching its rows
  • Since Rails 8.0, db:migrate on an empty database replays every migration
  • Rows inserted by old data migrations come back with db:schema:load

Assumes Rails 8.1. Since Rails 8.0, db:migrate on an empty database loads the schema file first and runs only newer migrations; db:migrate:reset gives the old run-everything behaviour.