In this lesson, we are going to explore an essential aspect of managing databases in Ruby on Rails: making schema changes using database migrations. Specifically, we will focus on adding a new column to an existing table. The goal of this lesson is to add a file
column to the todos
table using a database migration. This will enhance the functionality of your ToDo app by allowing it to handle file uploads associated with your tasks.
By understanding and mastering this process, you'll be able to make various types of changes to your database schema confidently. Let's dive into it!
Incremental migrations are essential for managing database changes efficiently in Ruby on Rails. Rather than modifying previous migrations, such as the initial create_todos
migration, we create separate migration files for each new change. This approach offers several benefits:
Clarity and Organization: Isolating each change in its own migration file helps in understanding the specific purpose without confusion.
Reversibility: Easily roll back migrations if needed, maintaining database integrity during development and troubleshooting.
Collaboration: Team members can review and understand changes independently, reducing conflict risks when working on different schema parts.
Version Control: Tracking individual migration files in version control systems helps identify the history and rationale behind each change.
For instance, adding a file
column to the todos
table is handled in a separate AddFileToTodo
migration file, not by modifying the initial create_todos
migration. This ensures a clear and traceable evolution of your database schema alongside the development of your app.
Let’s create a migration to add a file
column to the todos
table:
Generate the Migration File:
Run the following command to generate a migration file to define a migration class AddFileToTodo
that inherits from ActiveRecord::Migration
:
Shell Session1rails generate migration AddFileToTodo
Write the Migration Code:
Open the generated migration file located in the db/migrate
directory and add the following code to define the changes:
Ruby1class AddFileToTodo < ActiveRecord::Migration[7.1] 2 def change 3 add_column :todos, :file, :string 4 end 5end
Inside the change
method, we define the actual change we want to make: adding a file
column of type string
to the todos
table.
Run the Migration: With the migration file created and configured, the next step is to apply the changes to the database:
Shell Session1rails db:migrate
In this lesson, we covered database migrations in Ruby on Rails, focusing on adding a new column to an existing table. Specifically, we generated a migration to add a file
column to our todos
table, ran the migration, and confirmed the changes.
Now it's time for you to put this into practice. Approach the upcoming practice exercises with confidence, knowing you've learned the steps to modify your database schema effectively. Don’t hesitate to experiment further and explore more use cases for database migrations.
Happy coding!