All the Database Tasks in Rails
Tags: rails
Published: 2021-12-09 04:22:16

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

After programming for a while, you stumble upon `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 `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 `db:reset` 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 db tasks**

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

```shell
➜  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 [database_tasks.rb](https://github.com/rails/rails/blob/fe1f4b2ad56f010a4e9b93d547d63a15953d9dc2/activerecord/lib/active_record/tasks/database_tasks.rb) file in the Rails repository and the `databases.rake` file under the `ActiveRecord/Railties` directory. The `ActiveRecord::Tasks::DatabaseTasks` utility class encapsulates the logic behind the common tasks used to manage the database and migrations. 

---

Here's a brief summary of each database task. 

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

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

**db:environment:set**: Fixes the `EnvironmentMismatchError` or `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 `test/fixtures/` 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 `up` method by running the `down` 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 `up` method for a given migration. 

**db:prepare**: Runs `setup` 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 `db:drop`, `db:create`, `db:migrate` tasks. 

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

**db:schema:cache:clear**: Clears the db/schema_cache.yml file generated by the `db:schema:cache:dump` task. 

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

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

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

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

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

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

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

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

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

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

---


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

Until next time!

> Original Post: [All the Database Tasks in Rails
](https://akshaykhot.com/rails-database-tasks/)