Force Rails Database Migration

Problem

Let’s say you create the following Rails migration forgetting to add code to the change method. You then issue the rails migration command rails migrate

class AddAwesomeToStuff< ActiveRecord::Migration[6.0]
  def change
  end
end

You then update the migration with your desired changes and try issuing the migrate command again. Nothing happens. It is because each time a migration runs the version number is stored in a table that prevents the same migration running more than once.

class AddAwesomeToStuff< ActiveRecord::Migration[6.0]
  def change
    change_table :stuff do |t|
      t.string :awesome
    end
  end
end

Solution

Force a migration to run in the Rails console

irb(main)> require "#{Rails.root.to_s}/db/migrate/20211004223636_add_awesome_to_stuff.rb"
irb(main)> AddAwesomeToStuff.migrate(:up)

Leave a Reply

Your email address will not be published. Required fields are marked *

Up Next:

Clean up RSpec Global Namespace

Clean up RSpec Global Namespace