benetech / Miradi-Online

Miradi Online - main Rails repo.
7 stars 1 forks source link

Create models from existing schema #12

Open gcapiel opened 12 years ago

gcapiel commented 12 years ago

As soon as I get the MySQL SQL scripts that create the schema, I will post the link to them.

There are a few basic approaches:

  1. Migrate the schema to one that is rails-conformant, naming-wise for columns and tables
  2. You can setup your model classes with custom table-names and column-names to go with the existing scheme
  3. You could use views to make the underlying tables more conformant. I've used views with rails before and it's a pain to recreate the views whenever schema changes to the underlying tables occur so I wouldn't really recommend that if it's avoidable
  4. You could build a new schema from scratch and pull over functionality to the rails app piecemeal, using a bridge to talk to the old app where necessary. That's a good approach if it is impossible to do a greenfield app because you need the system to be up. It also reduces risk but at the cost of extra development time and complexity.

The one part that I'd definitely recommend migrating is the primary key. You want an autoincrement (or similar) id column. Name could be something other than ID if necessary. If the legacy schema uses composite keys you'd be highly advised to move away from that.

If there is not a huge # of tables I'd also recommend migrating most of it.

Also look at: http://www.engineyard.com/blog/2011/using-datamapper-and-rails-with-legacy-schemas/ http://stackoverflow.com/questions/7616390/rails-models-legacy-schema-table-inheritance http://stackoverflow.com/questions/4119659/rails-3how-to-generate-models-for-existing-database-tables http://stackoverflow.com/questions/1612095/model-from-existing-table-in-rails-2