1

So, say I have 10 models that have been evolving over the course of 100 migration files. Is there some sort of utility that could look my schema and build 10 'clean' migration files?

migration:

class CreateFoos < ActiveRecord::Migration
  def change
    create_table :foos do |t|
      t.belongs_to  :bar
      t.string :baz
      t.integer :qux, default: 0
    end

    add_index :foos, :bar_id
  end
end

schema:

ActiveRecord::Schema.define(:version => 20140610225017) do
  create_table "foos", :force => true do |t|
    t.integer  "bar_id"
    t.string   "baz"
    t.integer  "qux",         :default => 0
  end

  add_index "foos", ["bar_id"], :name => "index_foos_on_bar_id"
end

I just feel like... if it knows how to go from the migration to the schema, then vice versa would be easy. Does this sound stupid?

Dudo
  • 4,002
  • 8
  • 32
  • 57
  • Both code is a part of migration.. What do you mean by *schema* ? – Arup Rakshit Jun 13 '14 at 17:02
  • the second block of code there is from schema.rb – Dudo Jun 13 '14 at 17:06
  • 1
    suppose you have 10 migration files . Now you want to change or replace the 5th migration file. Then do `rake db:rollback STEP=5` and then edit the *5th* one and save it. Then run `rake db:migrate`. ( But you will loose the data ) :) It is on your risk. – Arup Rakshit Jun 13 '14 at 17:06
  • or `rake db:migrate:down VERSION=235634534534534` to rollback a specific migration. I don't wont to change anything, per se. I just want to clean up my migrations. – Dudo Jun 13 '14 at 17:09
  • http://stackoverflow.com/questions/5224827/generate-a-migration-file-from-schema-rb – Bot Jun 13 '14 at 17:09
  • I saw that q/a, @Bot... I didn't like the answer. I can't see why it's a bad idea to 'rebuild' your migrations. – Dudo Jun 13 '14 at 17:11
  • 2
    It's because you really don't need them. These two commands will suffice, rake db:create and rake db:schema:load. And I am saying this because I had the same feeling a year back and it was really to hard to digest this. – Bot Jun 13 '14 at 17:26

2 Answers2

1

I find you can delete your migrations after they have all been applied to all the databases, development through production. If you want to populate a new development or production database from scratch you can either (a) backup production and restore to the new database, or (b) load from the schema.rb file using rake db:schema:load.

If you really want the migrations for some documentation or clarity, create a new schema rails g migration schema2014. After the migration has been applied to production, delete all the old migrations files and copy schema.rb into the new migration.

Marlin Pierce
  • 9,931
  • 4
  • 30
  • 52
0

If you don't care about the actual data and you're dealing with a new installation where you want to just create the DB structure using the schema.rb, you should use rake db:schema:load.

More details:

rake db:schema:load vs. migrations

Generate a migration file from schema.rb

Community
  • 1
  • 1
tirdadc
  • 4,603
  • 3
  • 38
  • 45