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:
rake db:schema:dumprake db:data:dump(requires the yaml_db plugin I believe)script/generate migration create_schema- copy contents of db/schema.rb into the newly created migration file in db/migrate and edit it as necessary (eg. reorder fields, remove
:forceoption, addt.timestamps, change strings to symbols, createdrop_table’s forself.down) - delete each of the migrations leading up to the newly created one
rake db:droprake db:migraterake 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.