0

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 **********
Steve
  • 418
  • 1
  • 4
  • 16
  • 1
    Possible duplicate of [How to rollback a specific migration?](https://stackoverflow.com/questions/3647685/how-to-rollback-a-specific-migration) – zeitnot Feb 23 '19 at 15:52

2 Answers2

1

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.

Amiratak88
  • 1,204
  • 12
  • 18
  • I tried this but the columns and table still show in the schema – Steve Feb 23 '19 at 16:04
  • @Steve Schema is reproduced when u run `rails db:migrate`. So u can delete the schema and then with the right migrations run `rails db:migrate` – Amiratak88 Feb 23 '19 at 16:06
  • I tried deleting all the migration files I didn't want and the schema file and then ran `rails db:migrate VERSION=20180625235156` The schema that is produced still has all the columns and table i don't want. – Steve Feb 23 '19 at 16:20
  • When I run `rails db:migrate:status` it shows the migrations followed by `NO FILE` where I deleted the migration files. – Steve Feb 23 '19 at 16:21
  • Schema is produced by ur migrations. Check them again. U can also drop and create ur db again if u don’t need the data. – Amiratak88 Feb 23 '19 at 16:23
  • It’ll be helpful if u share ur schema and migrations and more details. – Amiratak88 Feb 23 '19 at 16:24
  • I've added the schema code and `rails db:migrate:status`. Anything else? – Steve Feb 23 '19 at 16:33
  • Also what’s the problem. What columns are extra? – Amiratak88 Feb 23 '19 at 16:36
  • the toilets table shouldn't be there and the toilet_id column in the stores table shouldn't be there – Steve Feb 23 '19 at 16:48
  • or anyone else have any ideas? – Steve Feb 23 '19 at 17:21
1

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Thanks. I ended up deleting the column and table via rails console. I'm still getting all the `****** NO FILE ******` messages when I run `rails db:migrate:status` though. Is there any way of getting rid of them? – Steve Feb 23 '19 at 18:17
  • Delete the corresponding records from the `schema_versions` table. – user229044 Feb 24 '19 at 05:16