This blog is part of our Rails 6 series.
Rails 6 adds rails db:prepare to migrate or setup a database if it doesn't exist.
Before Rails 6, we had to run the following tasks to set up the database.
# create the database
rails db:create
# run the migrations
rails db:migrate
# prepopulate the database with initial/default data
rails db:seed
Rails 6
Rails 6, adds
rails db:prepare
to get rid of running all the above tasks individually. rails db:prepare first
calls the
migrate
to run the migrations, but if the database doesn't exist, migrate throws an
ActiveRecord::NoDatabaseError. Once it is
catched,
it performs the following operations:
- Creates the database.
- Loads the schema.
- Seeds the database.
Thus, rails db:prepare saves a lot of time spent on running database tasks
individually while setting up an application and finishes it with just one
command.
Here is the relevant pull request.

Related blogs
- 01Rails 6 adds support to persist timezones of Active JobSeptember 1, 2020 · Chetan Gawai
- 02Rails 6 adds including & excluding method on EnumerablesAugust 18, 2020 · Akhil Gautam
- 03Rails 6 fixes after_commit callback invocation bugFebruary 25, 2020 · Amit Choudhary
- 04Rails 6 adds guard against DNS Rebinding attacksNovember 5, 2019 · Midhun Krishna