---
title: "Rails 6 adds rails db:prepare to migrate or setup a database"
description:
  "Rails 6 adds rails db:prepare to migrate or setup a database if it doesn't
  exist"
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-rails-db-prepare-to-migrate-or-setup-a-database"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-rails-db-prepare-to-migrate-or-setup-a-database.md"
---

# Rails 6 adds rails db:prepare to migrate or setup a database

Rails 6 adds rails db:prepare to migrate or setup a database if it doesn't exist

- Author: Akhil Gautam
- Published: December 10, 2019
- Categories: Rails 6, Rails

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.

```ruby
# 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](https://github.com/rails/rails/blob/98754de1412870d7dae9eba1c7bac944b9b90093/activerecord/lib/active_record/railties/databases.rake#L298)
to get rid of running all the above tasks individually. `rails db:prepare` first
calls the
[`migrate`](https://github.com/rails/rails/blob/98754de1412870d7dae9eba1c7bac944b9b90093/activerecord/lib/active_record/railties/databases.rake#L306)
to run the migrations, but if the database doesn't exist, `migrate` throws an
`ActiveRecord::NoDatabaseError`. Once it is
[catched](https://github.com/rails/rails/blob/98754de1412870d7dae9eba1c7bac944b9b90093/activerecord/lib/active_record/railties/databases.rake#L311),
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](https://github.com/rails/rails/pull/35768).

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-rails-db-prepare-to-migrate-or-setup-a-database)
