Skip to main content

How do I rollback a database migration and then update it

Rails • Asked by Neil Patel

Hi

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
  end
end

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'
end
end


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.