March 11, 2008

Migration For Legacy Tables

Filed under: migration, rails — oomoo @ 1:15 pm

If you already have an application with a database full of tables/data, you can easily create a “migration” that will contain all those tables.  

Please note that if you are going to be actually using Ruby/Rails with the existing database, you should not create a migration.  Just use the existing database.  Migrations are great for moving changes you make during development into a production database, but since you already have a production database that is being managed (I assume) by a legacy application, you’d best leave it alone.

But, if you are moth-balling the old application or creating a parallel application in Ruby/Rails, then you can use the following technique to kick-start your migration file.  It’s also just a cool education about how the migrate code works 🙂 

(I copied this technique from a blog that I cannot recall at this time.  If I can find it again, I will give the proper credit)

Legacy db:Migration Steps:

  1. Edit the <yourapp>\config\database.yml to use the legacy database (the one full of tables/data)
  2. Opening a ruby command line (and go to your rails application directory).
  3. Type the command:  rake db:schema:dump 
    This will “dump” the schema from the existing database into the file <yourapp>\db\schema.rb.
  4. Then create your new migration file by:
  5. Typing in the command line :  ruby script/generate migration your_schema.
  6. Cut and paste the data from the \db\schema.rb file into your new db\migrate01_your_schema.rb file.
  7. Edit the \config\database.yml back to your normal (rails) database.  (Don’t forget to do this!!!)
  8. You will need to add the following piece of code to each of your create_table lines: :force => true.
  9. Your create_table lines should look like the following: create_table “table_name”, :force => true do |t|.   
       This will force the new migration to take place within your db.
  10. Now, you just run your rake migrate command from the command line and your db will be updated.   (rake db:migrate)

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: