Gugubaight Gugubaight - 25 days ago 7
Ruby Question

Rails 4: Re-Migrating all Migrations to Schema

I am currently developing an Rails 4 app and over the last few months I collected a lot of unique migration files, which reverse their own changes, etc.

Since I have no problem to reset the db at this stage of development, I thought I could might clean up this mess a bit.

Is there a way of redoing all migration files, while also allowing to delete certain migration-files and to expand others?

I appreciate each answer!




Example (Edit)

I have two migration files:


  1. 20160911071103_create_items.rb

  2. 20160918085621_add_cached_votes_to_items.rb



Both are already migrated.<

1st file before

class CreateItems < ActiveRecord::Migration
def change
create_table :items do |t|

t.integer :user_id

t.timestamps null: false
end
end
end


My goal is to include the added column by the second file directly in the first file and deleting the second.

1st file after

class CreateItems < ActiveRecord::Migration
def change
create_table :items do |t|

t.integer :user_id
t.integer :cached_votes

t.timestamps null: false
end
end
end

dkp dkp
Answer

You need to run

rake db:migrate:down VERSION=20160918085621

rake db:migrate:down VERSION=20160911071103

You can now check the migration status by rake db:migrate:status and you should expect to get migration status as follows:

down 20160911071103
down 20160918085621

Now, you can remove the migration file 20160918085621_add_cached_votes_to_items.rb

& edit the migration file 20160911071103_create_items.rb as you need.

And, finally run:

rake db:migrate:up VERSION=20160911071103