« Define: tree | Main | Rails callbacks from Google Maps events »

Merging Rails migrations

I'm building my first Ruby on Rails site and as the doctor orders, I've created a number of migration scripts along the way as the database schema evolves. Migrations are cool and all, but at certain milestones the growing list of migrations is no longer useful. I'd like to roll up all the migrations up to that point and start from the current schema.

Rafe Colburn wrote a post asking whether a plan existed to do just that. The responses he received were not particularly helpful, so I came up with my own plan that seems to work well:

  1. rake db:schema:dump
  2. rake db:data:dump (requires the yaml_db plugin I believe)
  3. script/generate migration create_schema
  4. copy contents of db/schema.rb into the newly created migration file in db/migrate and edit it as necessary (eg. reorder fields, remove :force option, add t.timestamps, change strings to symbols, create drop_table’s for self.down)
  5. delete each of the migrations leading up to the newly created one
  6. rake db:drop
  7. rake db:migrate
  8. rake db:data:load (requires the yaml_db plugin I believe)

That should leave you with an up to date database, and all the incremental migrations replaced by a single complete migration.

TrackBack

TrackBack URL for this entry:
http://heath.hrsoftworks.net/cgi-bin/mt-tracker.cgi/204

Post a comment