DEV Community

Cover image for Rails "db:...." commands explained!
Davide Santangelo
Davide Santangelo

Posted on • Updated on

Rails "db:...." commands explained!

When you run the rails command inside a Rails project you will get the list of all commands that can be launched in a Rails application.

rails
The most common rails commands are:
 generate     Generate new code (short-cut alias: "g")
 console      Start the Rails console (short-cut alias: "c")
 server       Start the Rails server (short-cut alias: "s")
 test         Run tests except system tests (short-cut alias: "t")
 test:system  Run system tests
 dbconsole    Start a console for the database specified in config/database.yml
              (short-cut alias: "db")
 new          Create a new Rails application. "rails new my_app" creates a
              new application called MyApp in "./my_app"
 plugin new   Create a new Rails railtie or engine

All commands can be run with -h (or --help) for more information.
In addition to those commands, there are:

about
action_mailbox:ingress:exim
action_mailbox:ingress:postfix
action_mailbox:ingress:qmail
action_mailbox:install
action_mailbox:install:migrations
action_text:install
action_text:install:migrations
active_storage:install
app:template
app:update
assets:clean[keep]
assets:clobber
assets:environment
assets:precompile
cache_digests:dependencies
cache_digests:nested_dependencies
credentials:diff
credentials:edit
credentials:show
db:create
db:drop
db:encryption:init
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:system:change
db:version
destroy
dev:cache
encrypted:edit
encrypted:show
importmap:install
initializers
log:clear
middleware
notes
restart
routes
runner
secret
secrets:edit
secrets:setup
secrets:show
stats
stimulus:install
stimulus:install:importmap
stimulus:install:node
tailwindcss:build
tailwindcss:clobber
tailwindcss:install
tailwindcss:watch
test:all
test:db
time:zones[country_or_offset]
tmp:clear
tmp:create
turbo:install
turbo:install:importmap
turbo:install:node
turbo:install:redis
version
yarn:install
zeitwerk:check
Enter fullscreen mode Exit fullscreen mode

in this article we will only analyze the db commands. you can get them by running:

rails | grep db:
Enter fullscreen mode Exit fullscreen mode
db:drop
db:encryption:init
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:system:change
db:version
Enter fullscreen mode Exit fullscreen mode
  • db:create: This command is used to create a new database with the same name as the current environment (e.g. development, production) specified in your Rails application.

  • db:drop: This command is used to delete an existing database with the same name as the current environment specified in your Rails application.

  • db:encryption:init: This command is used to encrypt the database credentials in your Rails application.

  • db:environment:set: This command is used to set the current environment for your Rails application.

  • db:fixtures:load: This command is used to load fixtures (pre-defined data) into the database for your Rails application.

  • db:migrate: This command is used to run any pending database migrations for your Rails application.

  • db:migrate:down: This command is used to rollback the last migration for your Rails application.

  • db:migrate:redo: This command is used to rollback the last migration and run it again for your Rails application.

  • db:migrate:status: This command is used to check the status of migrations for your Rails application.

  • db:migrate:up: This command is used to run a specific migration for your Rails application.

  • db:prepare: This command is used to prepare the database for the current environment in your Rails application.

  • db:reset: This command is used to drop and create the database with the same name as the current environment specified in your Rails application.

  • db:rollback: This command is used to rollback the last migration for your Rails application.

  • db:schema:cache:clear: This command is used to clear the schema cache for your Rails application.

  • db:schema:cache:dump: This command is used to dump the schema cache to a file for your Rails application.

  • db:schema:dump: This command is used to dump the current schema of the database as a Ruby file for your Rails application.

  • db:schema:load: This command is used to load a schema file into the database for your Rails application.

  • db:seed: This command is used to load seed data into the database for your Rails application.

  • db:seed:replant: This command is used to reload seed data into the database for your Rails application.

  • db:setup: This command is used to create, load the schema and seed data into the database for your Rails application.

  • db:system:change: This command is used to change the database system for your Rails application.

  • db:version: This command is used to check the version of the migrations for your Rails application.

In conclusion, the various Rails commands that were discussed, such as db:create, db:migrate, db:seed are used to manage and interact with the database of a Rails application. They allow developers to create, modify, and seed the database, as well as generate boilerplate code for different parts of the application.

These commands are powerful tools that can greatly simplify the development process, but it is important to use them with caution and make sure to have proper backups or version control in place before making any changes to the application. Understanding and utilizing these commands can help developers to more efficiently and effectively build and maintain their Rails applications.

Top comments (4)

Collapse
 
gabrielsclimaco profile image
Coffee

"db:prepare: This command is used to prepare the database for the current environment in your Rails application." what does this mean? how is this different from setup?

Collapse
 
daviducolo profile image
Davide Santangelo

Here's a breakdown of db:prepare and db:setup in Rails, highlighting their differences:

db:prepare

Purpose:

  • Ensures the database is ready for use in the current environment.
  • Handles both database creation and updates efficiently.

Key Features:

  • Idempotent: Can be run multiple times without adverse effects.
  • Adaptive: Works seamlessly regardless of whether the database exists or not.
  • Steps:
    1. Attempts to run db:migrate to apply any pending migrations.
    2. If the database doesn't exist, falls back to db:setup to create and seed it.

When to Use:

  • Ideal for continuous integration and deployment (CI/CD) pipelines where database setup needs to be automated and repeatable.
  • Convenient for development environments where you might frequently reset the database.

db:setup

Purpose:

  • Explicitly creates, loads the schema, and seeds the database.

Key Features:

  • Database Creation: Ensures the database exists.
  • Schema Loading: Applies the current schema definitions to the database.
  • Seeding: Populates the database with initial data (if any).

When to Use:

  • Use for initial setup of a new database.
  • Use when you explicitly want to create a fresh database with schema and seed data.

Key Differences:

Feature db:prepare db:setup
Idempotent Yes No
Adaptability Works with existing or new databases Requires a new database
Steps Migrates or falls back to setup Creates, loads schema, seeds
CI/CD suitability Ideal Less suitable

In summary:

  • Use db:prepare for most cases, as it handles both database creation and updates gracefully.
  • Use db:setup specifically when you need to force the creation of a new database.
Collapse
 
gabrielsclimaco profile image
Coffee

and congrats on the article btw

Collapse
 
gabrielsclimaco profile image
Coffee

what a great answer! thank you so much