# All the Database Tasks in Rails

*Published on December 08, 2021*

---


Once you start programming in Rails, you inevitably come across&nbsp;`db:migrate`, which applies the migration to your database. Then you make a mistake and learn about&nbsp;`db:rollback`&nbsp;to revert the previous migration.

After programming for a while, you stumble upon&nbsp;`db:reset`, to drop and recreate your database, running all the migrations on this fresh database. Finally, you realize that you lost all your sample data after the last reset, and discover&nbsp;`db:seed`, which inserts the sample data in the database, so your rails app looks realistic, and you can go back to a clean slate by running&nbsp;`db:reset`&nbsp;frequently.

Now it’s easy to be a competent Rails programmer and get by with these four tasks. However, if you truly want to master Rails (which is one of my goals as a new Rails developer) and make the most use of its magic to make your life easy, it’s worth knowing about the remaining database tasks Rails provides to manage the database and migrations.

## How to find all database tasks?

You can find all the database tasks Rails provides by running the following command from a Rails application directory.

```bash
➜  blog (main) ✗ bin/rails help | grep db:

  db:create
  db:drop
  db:environment:set
  db:fixtures:load
  db:migrate
  db:migrate:down
  db:migrate:redo
  db:migrate:status
  db:migrate:up
  db:prepare
  db:reset
  db:rollback
  db:schema:cache:clear
  db:schema:cache:dump
  db:schema:dump
  db:schema:load
  db:seed
  db:seed:replant
  db:setup
  db:structure:dump
  db:structure:load
  db:system:change
  db:version
```

You can also find the source code for these tasks in the&nbsp;[database\_tasks.rb](https://github.com/rails/rails/blob/fe1f4b2ad56f010a4e9b93d547d63a15953d9dc2/activerecord/lib/active_record/tasks/database_tasks.rb)&nbsp;file in the Rails repository and the&nbsp;`databases.rake`&nbsp;file under the&nbsp;`ActiveRecord/Railties`&nbsp;directory. The&nbsp;`ActiveRecord::Tasks::DatabaseTasks`&nbsp;utility class encapsulates the logic behind the common tasks used to manage the database and migrations.

Her'es a short summary of each task. Hope you learn something new.

**db:create**: Creates the database unless it already exists.

**db:drop**: Drops the database if it exists.

**db:environment:set**: Fixes the&nbsp;`EnvironmentMismatchError`&nbsp;or&nbsp;`NoEnvironmentInSchemaError`, raised if the environment data is not found in the schema, by setting the current environment in the internal table.

**db:fixtures:load**: It loads the fixtures, i.e., the sample data that you want to test against. They are stored in the YAML file under the&nbsp;`test/fixtures/`&nbsp;directory.

**db:migrate**: Runs all the migrations that have not run yet, for the current environment.

**db:migrate:down**: Reverts the transformations performed by the last migration’s&nbsp;`up`&nbsp;method by running the&nbsp;`down`&nbsp;method.

**db:migrate:redo**: Rolls back the database one migration and re-migrates up.

**db:migrate:status**: Displays the status of migrations.

**db:migrate:up**: Runs the&nbsp;`up`&nbsp;method for a given migration.

**db:prepare**: Runs&nbsp;`setup`&nbsp;if the database does not exist. Otherwise, it runs the migrations.

**db:reset**: Resets your database using your migrations for the current environment. It does this by running the&nbsp;`db:drop`,&nbsp;`db:create`,&nbsp;`db:migrate`&nbsp;tasks.

**db:rollback**: Rolls the schema back to the previous version, undoing the migration that you just ran. If you want to undo previous&nbsp;`n`&nbsp;migrations, pass&nbsp;`STEP=n`&nbsp;to this task.

**db:schema:cache:clear**: Clears the db/schema\_cache.yml file generated by the&nbsp;`db:schema:cache:dump`&nbsp;task.

**db:schema:cache:dump**: Creates a db/schema\_cache.yml file.

**db:schema:dump**: Creates a database schema file (either&nbsp;`db/schema.rb`&nbsp;or&nbsp;`db/structure.sql`, depending on&nbsp;`config.active_record.schema_format`).

**db:schema:load**: Loads a database schema file (either&nbsp;`db/schema.rb`&nbsp;or&nbsp;`db/structure.sql`, depending on&nbsp;`config.active_record.schema_format`) into the database.

**db:seed**: Loads the seed data from&nbsp;`db/seeds.rb`&nbsp;file.

**db:seed:replant**: Truncates tables of each database for the current environment and loads the seeds

**db:setup**: Creates all databases&nbsp;`db:create`, loads all schemas&nbsp;`db:schema:load`, and initializes with the seed data&nbsp;`db:seed`. However, it won’t drop the database first if it exists. Use&nbsp;`db:reset`&nbsp;to also drop all databases first.

**db:structure:dump**: Deprecated. It was used to dump the&nbsp;`structure.sql`&nbsp;file.&nbsp;**db:structure:load**: Deprecated. It was used to load the&nbsp;`structure.sql`&nbsp;file.

> For some history behind why these tasks were deprecated, check out this&nbsp;[wtf](https://discuss.rubyonrails.org/t/db-schema-vs-db-structure/75406). No,&nbsp;[seriously](https://weblog.rubyonrails.org/2020/5/7/A-May-of-WTFs/).

**db:system:change**: Running&nbsp;`rails new`&nbsp;generator without specifying a database sets your app with&nbsp;`sqlite`. It’s a hassle to change the database later. This task helps you easily change the database by delegating to the&nbsp;`rails db:change SYSTEM=postgresql|mysql|whatever`&nbsp;generator.

**db:version**: Prints the current schema version number.

There you go. Rails gives you so much power. Use it wisely.

Until next time!

—

I hope you liked this article and you learned something new. I sure did. If you have any questions or feedback, please&nbsp;[send me an email](mailto:akshay.khot@hey.com). I look forward to hearing from you.

If you liked this post, and would like to receive future posts directly in email, please subscribe below.

You might also enjoy these articles:

- [A brief introduction to Rack](/definitive-guide-to-rack)
- [Understanding Authenticity Tokens in Rails](/how-rails-authenticity-tokens-protect-against-csrf)
- [Building a to-do list using Hotwire](/building-to-do-list-using-hotwire-and-stimulus)
- [Concerns in Rails: Everything You Need to Know](/how-rails-concerns-work-and-how-to-use-them)

