DEV Community

Breon White
Breon White

Posted on

Phase 3 - Updating and Dropping Databases using Active Record

For this project, we had to build an application that uses Active Record to interact with a database. I decided to go with a task management application for Podcast planning titled Podtask. This tool will allow users to document their podcast topics, featured guests, and tasks to complete for a successful episode release.

One requirement for this project is that the database has to have at least two models with a one-to-many relationship.

One-to-Many Relationships

For context, in relational databases, a one-to-many relationship occurs when a parent record in one table can potentially reference several child records in another table. When I started this project, I created 3 database migrations:

  • a table for Podcasts (Parent Record*)
  • a table for Tasks (Child Record*)
  • and a table for Lists, which was associated with the Podcast ID and a task ID

However, while working through my project, I quickly noticed that I didn't need to use the List table in the database. This is when I had to refresh my memory on how to delete a table, as well as deleting columns from a table.

Delete Column from Database Table

To get started, you have to run a new migration for removing the column. The Tasks table we'll be working with first has the following build in it's migration:

class CreateTasks < ActiveRecord::Migration[6.1]
  def change
    create_table :tasks do |t|
      t.integer :podcast_id
      t.integer :list_id
      t.string :to_do
      t.string :todo_status
      t.timestamps
    end
  end
end  
Enter fullscreen mode Exit fullscreen mode

The column we will need to remove is list_id.
I ran the following code to start the migration, and made sure the name included details about the action we wanted to take place:

bundle exec rake db:create_migration NAME=remove_column_from_tasks
Enter fullscreen mode Exit fullscreen mode

Once the migration was created, I followed the instructions found on the Ruby on Rails - Active Record Migrations online guide. To remove a column, we will be using the remove_column statement.

remove_columns(table_name, *column_names, type: nil)

With our statement in place, this is what our migration looks like:

class RemoveColumnFromTasks < ActiveRecord::Migration[6.1]
  def change
    remove_column :tasks, :list_id, :integer
  end
end
Enter fullscreen mode Exit fullscreen mode

Alright, with everything in place we can run this migration using the following command:

bundle exec rake db:migrate

Success! By checking the schema.rb file, we can confirm the column was removed.

Delete Column from Database Table

For deleting an existing table, we can pretty much follow the same steps above except we would be using the drop_table statement instead of the remove_column statement.

After creating a migration...
bundle exec rake db:create_migration NAME=drop_lists_table

...we can set up our migration with the following build:

class DropListsTable < ActiveRecord::Migration[6.1]
  def change
    drop_table :lists
  end
end
Enter fullscreen mode Exit fullscreen mode

Lastly, I ran the bundle exec rake db:migrate command and checked the schema.rb file - success again! Our table was removed.

Migrations Are Our Friends

I'll admit, I was a little comprehensive about making adjustments to the database - especially after already building most of project.

However, I learned that migrations aren't scary. Even with proper planning before starting a project, things happen and there are plenty of resources are your fingertips to guide you through.

Top comments (0)