2

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.

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

I want to generate data scripts also. What command should i use for same?
0

Take a look at Sequel. Its a Ruby library that does all the same things but without tethering you to Rails/ActiveRecord. It might be just what you need if you are thinking really long term.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.