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