Save 36% for Black Friday! Learn more

All threads / How do I rollback a database migration and then update it

Ask A Question


You’re not receiving notifications from this thread.

How do I rollback a database migration and then update it

Neil Patel asked in Rails


I have added a column ( t.time "usual_start_time") to an exisitng table called Areas. (create_table "areas",)

create_table "areas", force: :cascade do |t|
t.string "location", limit: 255
t.boolean "active", default: false
t.text "short_info"
t.string "twitter_name", limit: 255
t.boolean "proposed"
t.string "twitter_url", limit: 255
t.string "facebook_url", limit: 255
** t.time "usual_start_time"**

after creating the migiration i forgot to add a default time of 6:45 

how do i rollback and then change the column so it shows 646 as the default time.

I think its db:rollback and then do i have to add the column again with the default time e.g default: "6.45" (as its a time (t.time)

Yes, you can simply do rails db:rollback or (rake db:rollback if rails < 5) to rollback the latest migration. And make changes to the migration as you like.

But note that if you already (git) pushed the faulty migration to a shared repository or deployed, you should make an additional migration.

Hi Jack

thanks for getting back ( its not been deployed but have pushed via git to a shared repo)

so I have done rails db:rollback

== 20170120123129 AddUsualStartTimeToAreas: reverting =========================
-- remove_column(:areas, :usual_start_time, :time)
-> 0.5321s
== 20170120123129 AddUsualStartTimeToAreas: reverted (0.5422s) ================

and it has been removed from the areas table

can i now delete the incorrect migration from the db file which is below

class AddUsualStartTimeToAreas < ActiveRecord::Migration
  def change
    add_column :areas, :usual_start_time, :time

and then create a new migration again but with the default time as 6:45 ?

or can i just update the incorrect db file and then do rake db:migrate

so it would be

def change
add_column :areas, :usual_start_time, :time, default: '6.45'

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 54,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.