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
This rebuilds the databse structure for both test and development
and then run the task
which is not something we always want.
Rails 6 add
a more convenient alternative task
What does the new task do?
In a nutshell
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:
Loads the database configuration for current environment.
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.
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
This command can also be used on its own as a convenient way to truncate data from all the tables.
Populates the seed data.
Throws an error if there are any pending migrations.
rails db:seed:replant and its supporting
rails db:truncate_all add a very convenient way to cleanup development data in Rails applications.