DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Recovering an MS SQL Database From Suspect Mode: Step-By-Step Guide
  • Are You Facing an Error When You Run the StartDagServerMaintenance.ps1 Script?
  • Schema Change Management Tools: A Practical Overview
  • Providing Enum Consistency Between Application and Data

Trending

  • 5 Subtle Indicators Your Development Environment Is Under Siege
  • Testing SingleStore's MCP Server
  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Automatic Code Transformation With OpenRewrite
  1. DZone
  2. Data Engineering
  3. Data
  4. Rake Database Commands

Rake Database Commands

Get an understanding of rake database commands like creating, migrating, initializing, seeding, rolling back, dropping, and resetting.

By 
Kristina Garcia Francisco user avatar
Kristina Garcia Francisco
·
Mar. 29, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
15.6K Views

Join the DZone community and get the full member experience.

Join For Free

Rake is a utility built into Ruby and Rails that provides an efficient way to manage database changes. You can easily migrate database changes to servers by only using a command line!

You might be asking yourself during your application development:

  • What happens when using rake database commands?
  • When should I use them?

Let's take a look at how we can use these commands to change our database while developing an application!

Creating

$ rake db:create

When you create your Rails application for the first time, it will not have a database yet. In order for it to start, you will need to make sure the database is up and running.

Just like it's recommended to use different gems for each environment, you should also create three databases: each for development, testing, and production environment. You can configure them in your config/database.yml file.

default: &default
  adapter: postgresql
  encoding: unicode
  username: username
  password: password
  host: localhost
  port: 5432

development:
  <<: *default
  database: story_dev

test:
  <<: *default
  database: story_test

production:
  <<: *default
  database: story

Migrating

rake db:migrate

Migrations set up the tables in the database. When you run the migration command, it will look in db/migrate/ for any Ruby files and execute them starting with the oldest. There is a timestamp at the beginning of each migration filename.

Every time you migrate a database or make any change to it such as adding a row or a column, adding a table, or changing the data type, you have to run this command for the changes to reflect in your database. Here's a list of database rake tasks in a Rails application.

Migrations are created when you run commands like rails generate scaffold, rails generate model, or rails generate migration.

Here is an example how you can use rake db:migrate when uploading images. Be sure to not have data creation in the migration files!

Initializing

rake db:schema:load

Unlike rake db:migrate, which runs migrations that have not run yet, rake db:schema:load loads the schema that is already generated in db/schema.rb into the database.

Always use this command when:

  • You run the application for the first time.
  • When you drop the database and you need to create it again.

Beware! If you run rake db:schema:load on a production server, you'll end up deleting all your production data.

Seeding

rake db:seed

We always have default data that we want to have in our application for testing purposes. The seed command exists to automate this process.

Example: Create an admin user and store its data in the db/seed.rb file. When you run rake db:seed, it will load all the admin data into your application.

Admin.create!(email: 'admin@kolosek.com', 
              password: 'password', 
              password_confirmation: 'password')

Rails seeding is generally for development and/or staging environments; there are only a few uses in production. You don't want your production application to seed dummy users!

Rolling Back

rake db:rollback

Did you create a migration without wanting it or you simply changed your mind about it? Fear not! When you run this command, it will look at the last migration created and undo it!

Example: Let's start off by creating a new migration with :role as an integer and run the migration. Then, we decided to make it a string instead. So, we will edit the newly created migration and run it again, but nothing happens and your tests and factories will fail.

class CreateRoles < ActiveRecord::Migration
  def change
    create_table :roles do |t|
      t.integer :role # we will change this to t.string :role
      t.references :user
      t.timestamps
    end
  end
end

Why isn't this working?

Only the last created migration is run with rake db:migrate command. This means that no changes will be made by editing an already existing migration. To make this work, you will need to run rake db:rollback instead. This will tell Rails to do two things:

  1. Undo the last changes you just made to the database.
  2. Update the migration timestamp.

Dropping

rake db:drop

Sometimes, we want to delete all of the data and tables and start from fresh. That's what rake db:drop is for. If you want to keep the data you have, be sure to back it up before running this command.

Dropping the database will also remove any schema conflicts or bad data. Once the database is dropped, you'll want to start the process over again by recreating the database, running migrations, and seeding the data. Be sure that your RSpec tests are passing after remaking your database! Make sure you don't have connections to the database or it won't drop.

Resetting

rake db:reset

You might sometimes need to drop the local database and start fresh with data loaded from db/seeds.rb. This is a useful command when you are still figuring out your schema, and often need to add fields to existing models.

Once the reset command is used, it will do the following:

  1. Drop the database: rake db:drop
  2. Load the schema: rake db:schema:load
  3. Seed the data: rake db:seed

Why db:schema:load and not db:migrate?

rake db:schema:load is much faster than rake db:migrate because it loads the schema that we've already generated from db/schema.rb instead of going through all the migrations again.

I hope this helped you to understand the main difference between rake database commands!

Database Command (computing) application Data (computing)

Published at DZone with permission of Kristina Garcia Francisco, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Recovering an MS SQL Database From Suspect Mode: Step-By-Step Guide
  • Are You Facing an Error When You Run the StartDagServerMaintenance.ps1 Script?
  • Schema Change Management Tools: A Practical Overview
  • Providing Enum Consistency Between Application and Data

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: