is a utility built into , which provides an efficient way for managing database changes. You can easily migrate database changes to servers by only using a command line! Rake Ruby and Rails You might be asking yourself during your application development: when using ? What happens rake database commands should I use them? When Let’s take a look at how we can use these commands to change our database while developing an application! Creating When you 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. $ rake db:create create Just like it’s recommended to use for each environment, you should also create three databases, each for , , and environment. You can configure them in your file. different gems development testing production config/database.yml 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 setup the tables in the database. When you run the migration command, it will look in for any ruby files and execute them starting with the oldest. There is a at the beginning of each migration filename. rake db:migrate Migrations db/migrate/ timestamp 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. List of Database Rake Tasks in a Rails Application Migrations are created when you run commands like , , or . rails generate scaffold rails generate model rails generate migration Here is an example how you can use when . Be sure to not have data creation in the migration files! rake db:migrate uploading images Initializing Unlike that runs migrations that have not run yet, loads the schema that is already generated in into the database. rake db:schema:load rake db:migrate rake db:schema:load db/schema.rb Always use this command when: You run the application for the . first time When you and you need to create it again. drop the database ! If you run on a production server, you'll end up deleting all your production data. Beware rake db:schema:load Seeding We always have default data that we want to have in our application for testing purposes. The exists to automate this process. rake db:seed seed command : Create an admin user and store its data in the file. When you run it will load all the admin data into your application. Example db/seed.rb rake db:seed Admin.create!(email: '[xxx@example.com]', password: 'password', password_confirmation: 'password') Rails seeding is generally for and/or environments, there are only a few uses in production. You don’t want your production application to seed dummy users! development staging Rolling Back Did you create a migration without wanting it or you simply changed your mind about it? When you run this command, it will look at the last migration created and undo it! rake db:rollback Fear not! : Let’s start off by creating a with as an integer and . 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 will fail. Example new migration :role run the migration tests and factories 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 endend Only the last created migration is run with command. This means that no changes will be made by editing an already existing migration. To make this work, you will need to run instead. This will tell Rails to do two things: Why isn’t this working? rake db:migrate rake db:rollback Undo the last changes you just made to the database. Update the migration timestamp. Dropping Sometimes we want to delete all of the data and tables and start from fresh. That's what is for. If you want to keep the data you have, be sure to back it up before running this command. rake db:drop rake db:drop Dropping the database will also remove any or . Once the database is dropped, you’ll want to start the process over again by re-creating the database, running migrations, and seeding the data. Be sure that your are passing after remaking your database! schema conflicts bad data RSpec tests Make sure you don’t have connections to the database or it won’t drop. Resetting You might sometimes need to drop the local database and start fresh with data loaded from . This is a useful command when you are still figuring out your schema, and often need to add fields to existing models. rake db:reset db/seeds.rb Once the reset command is used it will do the following: Drop the database: rake db:drop Load the schema: rake db:schema:load Seed the data: rake db:seed is much faster than , because it loads the schema that we’ve already generated from instead of going through all the migrations again. Why db:schema:load and not db:migrate? rake db:schema:load rake db:migrate db/schema.rb Hope this helped you to understand the main difference between rake database commands! Originally published at kolosek.com on March 22, 2018.