Topic: running two schema in parallel (originally posted to App Theory forum)

I am just learning RoR, planning to redevelop a PHP application using RoR to provide a REST API for an Html5/Javascript client.

The project is going to take some time to complete and I would like to implement it one module at a time. The PHP application will run in parallel with the new RoR modules, updating the same mySQL database.

One initial problem is that I would like to change the mySQL schema to RoR's naming convention, but don't want to change the PHP code. Can I use two separate schemas in mySQL? Any other ideas on solving this? - I'd like to avoid adapting the RoR code to fit the legacy schema.

I'd be grateful for any advice.


Re: running two schema in parallel (originally posted to App Theory forum)

After doing some more research I found a useful blog post that has helped - … y-database (the post is not up-to-date for the latest version of Rails).

What I now plan to do is:

  • Where my current application is split between two databases, connect to an alternative/second database with establish_connection e.g.

establish_connection "legacy_database_#{Rails.env}"
  • Use the legacy table name by specifying the table name for the model:

self.table_name = 'MyLegacyTableName'
  • Specify the legacy primary key (instead of "id"):

self.primary_key = 'MyTableKey'
  • Add an alias for each of the old column names:

alias_attribute :rails_column_name, :LegacyColumnName
  • When adding associations / relationships between the models, specify the columns to be used as keys (as these don't conform to Rails standard):

belongs_to :parent_table, :foreign_key => :OldColumnKey, :primary_key => :OldColumnKey

I should now be able to program using Rails naming conventions, run the application alongside my PHP application, then once all the development is completed migrate the database, remove the aliases and update associations to remove the old key names.

Does anyone see any problems with this approach?