Rails 6 adds db:seed:replant task

2 minute read

Why is the task needed?

There are lot of times when we need to repopulate the seed data during development or staging applications and more.

This maybe for different reasons like updating of seed data, the develoment data is corrupted and so forth. Before we can repopulate the seeds it is common to want to wipe everything in the database. The way we would do it till now, would be to run rails db:setup . This rebuilds the databse structure for both test and development and then run the task rails db:seed , which is not something we always want.

Rails 6 add a more convenient alternative task rails db:seed:replant .

What does the new task do?

In a nutshell rails db:seed:replant , first truncates all the database tables for the current environment and then loads the seeds.

Lets run the task with --trace option and see in detail what all it does:

$ rails db:seed:replant --trace ** Invoke db:seed:replant (first_time) ** Invoke db:load_config (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute db:load_config ** Invoke db:truncate_all (first_time) ** Invoke db:load_config ** Invoke db:check_protected_environments (first_time) ** Invoke db:load_config ** Execute db:check_protected_environments ** Execute db:truncate_all ** Invoke db:seed (first_time) ** Invoke db:load_config ** Execute db:seed ** Invoke db:abort_if_pending_migrations (first_time) ** Invoke db:load_config ** Execute db:abort_if_pending_migrations ** Execute db:seed:replant

From the logs we can see that command executes following major sub tasks in sequence:

db:load_config

Loads the database configuration for current environment.

db:check_protected_environments

This provides protection from accidentally running the job on non test/development environments. For instance this prevents us from accidentally wiping all the production data. The task is aborted if we try to run it in production environment.

$ RAILS_ENV = production rails db:seed:replant rails aborted! ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database. If you are sure you want to continue, run the same command with the environment variable: DISABLE_DATABASE_ENVIRONMENT_CHECK=1 /Users/puneetsutar/workspace/chat/bin/rails:9:in `<top (required)> ' /Users/puneetsutar/workspace/chat/bin/spring:15:in `<top (required)> ' bin/rails:3:in `load' bin/rails:3:in `<main> ' Tasks: TOP => db:seed:replant => db:truncate_all => db:check_protected_environments (See full trace by running task with --trace)

db:truncate_all

This is a new rails task added to support seed replant functionality. This task will wipe out data from all the tables for the current environment except the tables used internally by rails i.e schema_migrations and ar_internal_metadata . This command can also be used on its own as a convenient way to truncate data from all the tables.

db:seed

Populates the seed data.

db:abort_if_pending_migrations

Throws an error if there are any pending migrations.

Summary