Reputation: 464
I'm working on a Rails 5 project and I've added various columns to some of my tables and created a new table as well. This obviously has created a few migration files and I would like to revert back to a certain migration and delete all the changes in the schema that were made after this migration. How do I do this?
Schema.rb:
ActiveRecord::Schema.define(version: 20180814220216) do
create_table "stores", force: :cascade do |t|
t.string "name"
t.string "address"
t.float "beer_cost"
t.text "facilities"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "latitude"
t.float "longitude"
t.boolean "toilet"
t.string "address_line2"
t.string "address_line3"
t.integer "user_id"
t.integer "toilet_id"
t.string "toilet_available"
end
create_table "toilets", force: :cascade do |t|
t.string "toilet_available"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end
Output after running rails db:migrate:status
Status Migration ID Migration Name
--------------------------------------------------
up 20180610144440 Create stores
up 20180611145310 Change datatype beer cost
up 20180611150425 Delete cig cost column
up 20180611231832 Add longitude and latitude
up 20180612194032 Add toilets column
up 20180614125348 Add address line2 column
up 20180614133234 Add address line3 column
up 20180625201708 Devise create users
up 20180625235156 Add user id to stores
up 20180626124327 ********** NO FILE **********
up 20180626124742 ********** NO FILE **********
up 20180627115344 ********** NO FILE **********
up 20180627115710 ********** NO FILE **********
up 20180810102513 ********** NO FILE **********
up 20180811094301 ********** NO FILE **********
up 20180814220216 ********** NO FILE **********
Upvotes: 2
Views: 3171
Reputation: 239270
You should not have deleted the migrations, once you've deleted them, you cannot roll them back. The correct sequence of steps is to rake db:rollback
, then delete the migration file.
At this point the simplest solution is to use rails db
to open an SQL terminal, and manually drop the columns/tables you don't want, and then rake db:schema:dump
to update your schema.rb
.
Upvotes: 1
Reputation: 1509
You can use:
rails db:rollback STEP=<put the number of migrations you want to go back
However the migrations are still there and if you run rails db:migrate
you are still gonna see all those changes.
So either write new migrations that undo the previous migrations, delete the table for instance.
Or delete the unwanted migration files manually.
Upvotes: 2