DEV Community

Ben D.
Ben D.

Posted on

Creating and Using Migrations in Ruby/Sinatra/ActiveRecord

As a web developer, it's essential to have a strong understanding of databases and how to manipulate them to create and store data. In Ruby, Sinatra, and ActiveRecord, creating and using migrations is a critical aspect of building robust, scalable applications. Migrations allow you to manage your database schema, define relationships between your tables, and modify data stored in your database. In this blog post, we'll dive into the basics of migrations and provide examples of how to create and use them effectively.

What is a Migration?

A migration is a Ruby file that defines a set of changes to your database schema. It specifies what changes should be made to your database, such as adding a new column, renaming a table, or creating a new table. Migrations can also specify any necessary changes to the data in your database, such as populating a new column with default values or modifying existing data.

Why Use Migrations?

Migrations are an essential tool for managing your database schema because they allow you to version control your database structure. This means you can keep track of changes to your database over time and roll back to a previous version if needed. Migrations also make it easier to collaborate with other developers on the same project by providing a standardized way of updating and modifying the database.

Creating a Migration

To create a migration in Ruby/Sinatra/ActiveRecord, you'll need to use the rake command-line tool. The rake tool is a build automation tool that's commonly used in Ruby applications to automate tasks. Here are the steps to create a migration:

Open your terminal and navigate to your project directory.
Enter the following command to create a new migration file:

rake db:create_migration NAME=migration_name
Enter fullscreen mode Exit fullscreen mode

Replace "migration_name" with the name of your migration. The rake tool will create a new file in the db/migrations directory with the name timestamp_migration_name.rb, where timestamp is the current timestamp.

Open the new migration file in your preferred text editor. The migration file will look something like this:


class MigrationName < ActiveRecord::Migration[6.1]
  def change
    # Add your migration code here
  end
end
Enter fullscreen mode Exit fullscreen mode

Modify the change method to define your migration. For example, if you wanted to add a new column to a table, your migration might look like this:


class AddNewColumnToTable < ActiveRecord::Migration[6.1]
  def change
    add_column :table_name, :column_name, :string
  end
end
Enter fullscreen mode Exit fullscreen mode

In this example, :table_name is the name of the table you want to modify, :column_name is the name of the new column you want to add, and :string is the data type of the new column.

Save your changes and exit the text editor.

Running a Migration
Enter fullscreen mode Exit fullscreen mode

Once you've created a migration, you'll need to run it to apply the changes to your database. Here are the steps to run a migration:

Open your terminal and navigate to your project directory.
Enter the following command to run your migration:

rake db:migrate
Enter fullscreen mode Exit fullscreen mode

This command will apply all the pending migrations to your database.

Rolling Back a Migration

If you need to roll back a migration, you can use the rake command-line tool. Here are the steps to roll back a migration:

Open your terminal and navigate to your project directory.
Enter the following command to roll back the last migration:

rake db:rollback
Enter fullscreen mode Exit fullscreen mode

Top comments (0)