Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Rails migrations from a schema

I am creating a new Rails application which will work with an existing schema. I have been given the schema SQL but I want to create Rails migrations to populate the database in development. The schema is not overly complicated, with around 20 tables, however I don't want to waste time and risk typos by manually creating the migrations.

Is there a way to generate Rails migrations given a schema's SQL?

like image 245
AaronThomson Avatar asked Apr 01 '11 00:04

AaronThomson


1 Answers

Sure, connect your application to your database, then run

rake db:schema:dump

This will give you a db/schema.rb ready with all of your definitions. Now that you have that db/schema.rb, simply copy the contents within the declaration into a new migration. I've done this before, and it works just great.

like image 198
François Beausoleil Avatar answered Sep 27 '22 21:09

François Beausoleil