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:
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.
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
rails db:seed:replant
and its supporting rails db:truncate_all
add a very convenient way to cleanup development data in Rails applications.