What does bin/rails db:seed:replant do in Rails 8.1, and what stops it from wiping a production database?
answer
- truncate_all, then seed
- keeps schema_migrations and ar_internal_metadata
- every database of the environment
- ActiveRecord::ProtectedEnvironmentError
- DISABLE_DATABASE_ENVIRONMENT_CHECK=1 overrides
basics
~10 sdb:seed:replant empties every table of the current environment's databases, except Rails' own schema_migrations and ar_internal_metadata, and then runs db/seeds.rb. It is a protected task: against a database last used as production it raises ActiveRecord::ProtectedEnvironmentError.
solid answer
~40 s`bin/rails db:seed:replant` is `db:truncate_all` followed by `db:seed`. The truncate step runs `TRUNCATE` on every table of every database configured for the current environment, with referential integrity switched off so foreign keys do not get in the way, and it skips `schema_migrations` and `ar_internal_metadata`, so the schema and its version stay put. Then the seeds load into empty tables. Use it in development when the seeds changed and you want a clean, seeded database without dropping and reloading the schema. Before truncating, Rails checks the environment stored in `ar_internal_metadata`: if it is listed in `ActiveRecord::Base.protected_environments` (by default `["production"]`) the task raises `ActiveRecord::ProtectedEnvironmentError`, and if it differs from the current one it raises `ActiveRecord::EnvironmentMismatchError`. The override the error message documents is `DISABLE_DATABASE_ENVIRONMENT_CHECK=1`. Rails 8.1's `config/ci.rb` runs it under `RAILS_ENV=test`.
code
bash · 4 linesbin/rails db:seed:replant
# production database:
# ActiveRecord::ProtectedEnvironmentError: You are attempting to run
# a destructive action against your 'production' database.go deeper
Know that db:seed:replant empties the tables and reseeds, and that it refuses a production database.
Explain the steps: truncate_all on every table except schema_migrations and ar_internal_metadata, then db:seed, versus db:reset rebuilding the schema.
Explain why the check reads the environment stored in ar_internal_metadata, how DISABLE_DATABASE_ENVIRONMENT_CHECK overrides it, and why protecting staging can be worth it.
Decide which environments count as protected and how shared databases are named and tagged so a destructive task can never reach real data.
## Three ways to get seeded data again While building the language-learning app, the team keeps changing `db/seeds.rb`: more countries, a new demo learner with streaks and lessons, a renamed plan. Running `bin/rails db:seed` again adds what is missing but leaves stale demo rows behind. Rails offers three ways to start over: | Command | Steps | Schema | Speed | |---|---|---|---| | `bin/rails db:seed` | load seeds | untouched | fast, but keeps old rows | | `bin/rails db:seed:replant` | truncate all tables, load seeds | untouched | fast | | `bin/rails db:reset` | drop database, create, load schema, load seeds | rebuilt from `db/schema.rb` | slower | `db:seed:replant` is the middle option: the tables stay, their **rows go**, and the seeds go back in. ## What the task does In `databases.rake` the task is defined as `replant: [:load_config, :truncate_all, :seed]`: 1. **`db:truncate_all`** first runs the protected-environment check, then for every database configuration of the current environment empties every table. 2. It leaves out **`schema_migrations`** and **`ar_internal_metadata`**, the two tables Rails uses to track which migrations ran and which environment owns the database. The schema version survives, so no migration reruns. 3. Truncation runs with **referential integrity disabled**, so foreign keys between `learners`, `countries` and `plans` do not block it. 4. **`db:seed`** then checks for pending migrations and loads `db/seeds.rb` into the empty tables. In a multi-database app, "every database of the environment" is literal: in development that is usually one database, but an environment with separate `cache`, `queue` and `cable` databases would have those emptied too. ## The safety check `ar_internal_metadata` records the environment that last ran migrations against the database. Before a destructive task such as `db:truncate_all`, `db:drop` or `db:purge`, Rails compares it with two things: - If the stored environment is in **`ActiveRecord::Base.protected_environments`**, which defaults to `["production"]`, the task raises **`ActiveRecord::ProtectedEnvironmentError`**: "You are attempting to run a destructive action against your 'production' database." - If the stored environment differs from the one you are running, it raises **`ActiveRecord::EnvironmentMismatchError`**, which catches a development shell pointed at a staging `DATABASE_URL`. The documented override for the first check is the environment variable **`DISABLE_DATABASE_ENVIRONMENT_CHECK=1`**; the second suggests `bin/rails db:environment:set` when the stored value is wrong. Both checks protect the database by what it says about itself, not by what `RAILS_ENV` you typed, which is why they also catch a mistaken connection string. You can add more names, such as `staging`, to `protected_environments` in an initializer. Plain `bin/rails db:seed` has no such check: it is not destructive, and it may legitimately run in production. ## What replant does not reset Truncation removes rows, nothing else. On PostgreSQL Rails issues a plain `TRUNCATE TABLE` for all tables in one statement, without `RESTART IDENTITY`, so primary-key sequences keep counting: after a replant, the first seeded country may get id 241, not 1. That is one more reason for seeds and code to look rows up by a natural key such as `code`, never by a hard-coded id. Database objects outside the tables, such as views, functions and the schema itself, are untouched, and so is anything stored outside the database, like files in Active Storage's service, which can leave orphaned uploads behind in development. ## Replant in CI Rails 8.1 introduced `bin/ci` and a generated **`config/ci.rb`**. One of its default steps is: ```ruby step "Tests: Seeds", "env RAILS_ENV=test bin/rails db:seed:replant" ``` It empties the test database and loads the seeds, failing the build if `db/seeds.rb` no longer runs, for example after a migration renamed a column the seeds still use. Since the test database stores `test` as its environment, the protection check passes. ## When to use which - **Changed seeds, same schema, want a clean slate:** `db:seed:replant`. - **Added seeds and want to keep local rows:** `db:seed`, relying on idempotency. - **Schema is in a strange state or a migration was edited:** `db:reset`, which rebuilds from the schema file. - **Any of these against production:** none of them; production data changes go through migrations or deliberate tasks.
- Why does Rails check the environment stored in the database instead of RAILS_ENV?Because the dangerous mistake is usually the connection, not the variable: a shell with `RAILS_ENV=development` but a `DATABASE_URL` pointing at production. The database's own record in `ar_internal_metadata` says production, so the destructive task refuses no matter what the shell claims.
- When would db:seed:replant be the wrong tool in development?When your local rows matter, for instance data you created by hand to reproduce a bug, because replant empties every table. Then run `db:seed` and rely on idempotency, or reset only specific tables yourself.
- db:seed:replant only deletes rows the seeds file created
- replant drops and recreates the schema like db:reset
- replant also empties schema_migrations, so migrations rerun
- RAILS_ENV=development is enough to make replant safe
- db:seed is blocked in production just like replant
Assumes Rails 8.1: the generated config/ci.rb and its Tests: Seeds step that runs db:seed:replant under RAILS_ENV=test are new in 8.1.