问题描述:

I'm working with a legacy database. The naming conventions are all over the place and inconsistent. We're writing a Ruby on Rails app, but obviously the database is not playing nicely with Rails built in features. Here is our strategy and wanted to get some opinions:

  1. Create a new database and point the rails app to this database
  2. Map old database tables/columns to new, rails-friendly table names/column names
  3. Use map to generate views so that the rails app "thinks" these tables are the real thing
  4. Develop
  5. Circle back when rails app is ready to take over for old UI and migrate actual data into real tables instead of views

The issue I'm running into is how to fit this MySQL View generation script into the rails app. Where would something like this fit into the file structure? As a plugin of some kind?

Table Mapping:

+--------------------+------------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+--------------------+------------------+------+-----+---------+----------------+

| id | int(11) unsigned | NO | PRI | NULL | auto_increment |

| database | varchar(64) | NO | | | |

| current_name | varchar(64) | NO | | | |

| current_pk | varchar(64) | YES | | NULL | |

| new_system_prefix | varchar(255) | YES | | NULL | |

| new_format | varchar(255) | YES | | NULL | |

| new_vendor | varchar(255) | YES | | NULL | |

| new_name | varchar(64) | YES | | NULL | |

| new_pk | varchar(64) | YES | | NULL | |

| notes | varchar(255) | YES | | NULL | |

+--------------------+------------------+------+-----+---------+----------------+

Column Mapping:

+-----------------+------------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+-----------------+------------------+------+-----+---------+----------------+

| id | int(11) unsigned | NO | PRI | NULL | auto_increment |

| table_id | int(11) unsigned | YES | MUL | NULL | |

| current_name | varchar(255) | YES | | NULL | |

| current_type | varchar(255) | YES | | NULL | |

| current_default | varchar(255) | YES | | NULL | |

| new_name | varchar(255) | YES | | NULL | |

| new_type | varchar(255) | YES | | NULL | |

| new_default | varchar(255) | YES | | NULL | |

| extra | varchar(255) | YES | | NULL | |

| notes | varchar(255) | YES | | NULL | |

+-----------------+------------------+------+-----+---------+----------------+

网友答案:

I would suggest you read a bit about Anticorruption layer, which is one of the DDD patterns. The main principle of this pattern is to protect your model from legacy systems it interacts with. One of the options is to create a Repository and encapsulate all of the queries that deal with the legacy database there. The repository will act as a translator between legacy database and your new model. The mapping of database fields to entities will be done by repository as well.

相关阅读:
Top