Search code examples
sqlruby-on-railsdatabaserails-migrations

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?


Solution

  • 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.