In Rails 8.1, what do db:create, db:setup, db:reset and db:prepare each do, and which of them replays your migrations?
answer
- create makes an empty database
- setup is create, schema load, seed
- reset is drop then setup
- prepare: set up if missing, else migrate
- migrate:reset is the replay
basics
~20 sdb:create makes empty databases; db:setup creates, loads the schema file and seeds; db:reset drops then runs setup; db:prepare sets up a missing database or runs pending migrations on an existing one. None replays all migrations; db:migrate:reset does.
solid answer
~40 s`db:create` creates the configured databases and nothing else (in development it creates the development and test databases). `db:setup` is `db:create` plus `db:schema:load` plus `db:seed`, so it builds from `db/schema.rb` or `db/structure.sql`, not from migrations. `db:reset` is `db:drop` followed by `db:setup`, so all data is lost and the schema file is the source. `db:prepare` is the idempotent one: for each database it creates it if missing and loads the schema file, seeding only a primary database it just initialised; for an existing database it runs only the pending migrations, and it never drops anything. `bin/setup` and the generated Docker entrypoint call `db:prepare` for that reason. To replay every migration from an empty database, Rails has `db:migrate:reset`: drop, create, then migrate from scratch.
code
bash · 5 linesbin/rails db:create # empty databases only
bin/rails db:setup # create + schema load + seed
bin/rails db:reset # drop + setup
bin/rails db:prepare # setup if missing, else migrate
bin/rails db:migrate:reset # drop + create + replay all migrationsgo deeper
Know that db:create makes empty databases, db:setup builds and seeds a new one, and db:reset starts over from the schema file.
Explain what db:prepare does for a missing versus an existing database, why none of these replays migrations, and where db:migrate:reset fits.
Pick the task per context: db:prepare for boots and deploys, db:reset only in development, db:migrate:reset to prove the history still runs.
Decide whether the schema file or the migration history is the team's source of truth for new environments, and how CI keeps the other honest.
## Four tasks that build databases A Rails 8.1 app has several tasks that sound alike. They differ in two things: whether they **drop** what exists, and whether the schema comes from the **schema file** or from **migrations**. | Task | Drops first? | Schema source | Seeds? | Safe to repeat? | |---|---|---|---|---| | `db:create` | no | none: empty database | no | yes; existing databases are reported, not touched | | `db:setup` | no | schema file (`db:schema:load`) | yes | no: loading the schema recreates tables | | `db:reset` | yes (`db:drop`) | schema file, via `db:setup` | yes | destroys data every time | | `db:prepare` | never | schema file for a new database, migrations for an existing one | only a database it just set up | yes | | `db:migrate:reset` | yes | every migration, replayed | no | destroys data every time | ## db:create `db:create` connects to the server and creates the database named in `config/database.yml` for the current environment. Run without `RAILS_ENV` in development, it creates both the development and the test databases. It creates no tables; a database straight out of `db:create` has no `schema_migrations` table yet. ## db:setup and db:reset `db:setup` chains three tasks: `db:create`, `db:schema:load` and `db:seed`. Loading the schema file is the important part: tables come from `db/schema.rb` (or `db/structure.sql`), and every migration version up to the file's version is marked as applied. It is meant for a database that does not exist yet. Run against an existing one, `db:create` just reports it exists, and loading `db/schema.rb` then recreates each table it defines (`create_table ... force: :cascade`), losing their rows. `db:reset` is `db:drop` plus `db:setup`. It is the "start over from the schema file" button for development. Because it drops, it is subject to the protected-environment check and refuses to run against a database last migrated as production. ## db:prepare `db:prepare` is described in Rails as "Run setup if database does not exist, or run migrations if it does". In more detail, for each database of the current environment: 1. If the database does not exist, it creates it. 2. If the database has no `schema_migrations` table and a schema file exists, it loads the schema file. 3. It runs any migrations that are still pending, which covers migrations newer than the schema file. 4. It dumps the schema of each database it migrated, unless `dump_schema_after_migration` is false. 5. It loads seeds only when it just initialised a database whose config allows seeds (the primary database by default). Because it never drops, `db:prepare` is safe to run on every boot or deploy, which is why `bin/setup` calls it and why the Rails 8 Dockerfile entrypoint runs it before starting the server. ## Which one replays migrations None of the four. `db:setup`, `db:reset` and `db:prepare` all prefer the schema file for a new database. Since Rails 8.0, even plain `db:migrate` on a fresh database loads the schema file first and only then runs newer migrations. To rebuild by **replaying every migration**, the task is `db:migrate:reset`: it drops, creates, writes an empty schema file, and runs all migrations from the beginning. That is the way to prove the migration history still works end to end. ## What each one leaves behind - After `db:create`, the database exists but has no tables, not even `schema_migrations`. - After `db:setup`, `db:reset` or a first `db:prepare`, `schema_migrations` lists every migration version up to the schema file's version, because loading the schema file records them; `ar_internal_metadata` records the environment and the schema file's checksum. - After a later `db:prepare`, the database also has the rows of whatever migrations were pending. - After `db:migrate:reset`, every version row comes from actually running the migration, and the schema file is rewritten from the result, which is also how you notice a schema file that drifted from the migrations. ## Choosing in practice - **First checkout of the dental-practice app:** `bin/setup`, which runs `db:prepare`. - **"My development data is a mess":** `bin/rails db:reset`. - **Deploy or container start:** `db:prepare`, never `db:reset` or `db:setup`. - **Do the old migrations still run?** `bin/rails db:migrate:reset` on a throwaway database. - **Only need the empty database:** `db:create`.
- Why is db:prepare safe in a container entrypoint while db:setup is not?`db:prepare` never drops and never reloads the schema over an initialised database; on an existing database it only runs pending migrations. `db:setup` loads the schema file, whose `create_table` calls recreate tables, so running it on every boot would wipe data.
- Does db:prepare run db:seed every time?No. It seeds only when it has just initialised a database (created it or loaded its schema because `schema_migrations` was missing) and that database's config allows seeds, which by default means the primary database. On an existing database it runs pending migrations and stops.
- db:reset replays all migrations from an empty database
- db:prepare drops and recreates the database every time it runs
- db:setup is a safe way to apply new migrations to an existing database
- db:prepare loads the seed file on every run
- db:create also loads db/schema.rb into the new database
Assumes Rails 8.1. db:prepare's set-up-or-migrate behaviour and db:migrate loading the schema file on a fresh database (new in 8.0) are what make the schema file, not migrations, the source for new databases.