Rails "db:...." commands explained!
Davide Santangelo
Posted on January 26, 2023
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
in this article we will only analyze the db commands. you can get them by running:
rails | grep db:
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
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.
Posted on January 26, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024