How to generate a Rails migration class automatically from the MYSQL database instance?

advertisements

I have database scripts which create database with more than 100 tables and lot of data. It is a tedious task for me to create Rails Migration classes for whole database. But i see Rails Migration as a good option in long term database change management. Please suggest some way to generate Rails Migrate classes automatically from MYSQL database instance.


This can be done in three simple steps:

  1. write config/database.yml to reference your database.
  2. Run "rake db:schema:dump" to generate db/schema.rb. Here's the documentation:

    $ rake -T db:schema:dump ... rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR

  3. Convert schema.rb into db/migrate/XXXXXX_create_migration.rb:

class CreateMigration < ActiveRecord::Migration
  def self.up
    # insert schema.rb here
  end

  def self.down
    # drop all the tables if you really need
    # to support migration back to version 0
  end
end