Forcing a migration in Rails



Recently, I was working on building unit tests for a set of new endpoints that I created for a backend application at work. After running the migration in the test environment I decided to modify the code a bit in the migration and rerun it again. Unfortunately, rolling back or undoing the latest migration wasn't working for some reason, I tried different methods to get the schema back to its previous state but nothing was working until I found a solution online that saved my life, so I thought I should share it with everyone on my blog as well.

The following is what I did to force the down action on my migration.

$ rails c
irb(main):001:0> require "#{Rails.root.to_s}/db/migrate/<file_name>.rb"
=> true
irb(main):002:0> <class_name>.migrate(:down)
==  <class_name>: reverting ===================================
-- drop_table("<table_name>")
   (4.1ms)  DROP TABLE "<table_name>"
   -> 0.0045s
==  <class_name>: reverted (0.0046s) ==========================
=> nil



Done


Leave a comment


Post


Comments


Hasan Hey, I tried this and it worked. Thanks for sharing! ;)

Posted 7 months ago · Edited 7 months ago.