Thursday, October 11, 2007

Advanced Rails : Moving an Existing Database to a Rails Application

Assuming that you have an existing application that you wish to rewrite in Rails, how would you prepare your database schema so that you can take advantage of the magick of db:migrate ?

Well, I had to do exactly that when I decided to rewrite an existing 200+tables CRM solution into e-CRM, a Rails application. This is what I did :-

a) Pluralize all the table names

Tip: One legacy table was named customer. To pluralize it to customers whilst retaing the data, I use CREATE TABLE customers SELECT * FROM customer for MySQL

b) Add an id field (auto-increment, integer) to every table

c) Add created_at, updated_at in every table to take advantage of Rails automatic updating of these fields without a single line of code

d) Added another magic column ie lock_version (must default to 0) to invoke optimistic locking

e) To support a belongs_to relationship, add belongs_to_tablename

f) Create a schema_info table with just 1 field ie Version, Integer and add a single record with the value=1

g) Use Rake db:schema:dump to create a schema.rb based on the underlying database schema . See following snapshot of Radrails in action
h) Create an empty migration file say 001_migrate2mysql and copy the contents to the Self.Up Method as shown in the Radrails snapshot as follows :-

That's it !
Updated on 4th November, 2007.

4 comments:

Glenn West said...

Cool article. I'll tag it for future reference.

I did alot of this automatically in
ruby scripts I wrote.

(Dbase/foxpro, and pervasive)
to rails.

http://mentalpagingspace.blogspot.com/2007/10/importing-from-odbc-sources-into-oracle.html
http://mentalpagingspace.blogspot.com/2007/10/import-and-cleanup-dbase-files-into.html

And automatically delete my basic controllers and models from the database, with rolerequirement,
and activescaffold

http://mentalpagingspace.blogspot.com/2007/10/production-generator-for-rails.html

Glenn West said...

By the way,
I'd recommend tabnav as well,
from seesaw. Unless your doing something more elaborate

Chee Chong Hwa said...

Hi Glenn

Thanx for the compliments :-)

Looks as if you have managed to use RoR with dbf/fpt ?

Where can I get the dbf/fpt adpater for testing ?

Glenn West said...

Ok kimberly. Dbase is not "industrial" strength enough to support rails. So what you will find in my blog is a tool that will take a directory of "dbase" "independant" files and convert them all to a dbi sql database. In my case that is oracle (XE) or can be Mysql as well.

It adds the id fields automatically. And cleans up the fields names. It also makes sure the database names are plural, and create the rails sequence number field as well.

In addition "numeric" field names are "concatted" to a master table if they exist, the mastertable is in the command line as well.

Welcome to Rails.. Rails... Rails !

In 1995, I started the popular Clipper...Clipper... Clipper website (no blogs then) which was very popular and linked by virtually every Clipper-related site. When I switched to Windows via Delphi in 1997, I started the Delphi... Delphi... Delphi site. In June 2007, I discovered Ruby on Rails and no prize for guessing what I am gonna name this blog. which I started on 2nd October 2007.

As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam

CCH
10th June 2010, 19:42