Search code examples
mysqlsqlruby-on-railsdumplegacy

Rails 4. Migrating (some) legacy DB data from old db to rails existing DataBase


I have a task to import old data from old DB to new Rails app. Old program was created in Delphi and is outdated so customer decided create rails app for replacing it. I have dump file, and I am creating local MySQL db now and inserting there data for last 8 years (mysql is working for over an hour already) :)

Now I am not sure about the way old data should be imported. The problem is that old db tables and columns are named differently from what I have in my App and there are many of unused and unneeded columns/tables there. So I need select only some of them and insert into App DB. Could You please suggest the best solution for this case?

Thank you in advance!


Solution

  • You can adjust old db to rails app and work with it

    Let's imagine you have old DB with the following table old_posts

    old_posts
    ________________________________________
    sysid | name         | description      |
    _______________________________________
          |              |                  |
    1     | The best post| Some description |
    _______________________________________
          |              |                  |
    2     | Another  post| Another descrip  |
    ________________________________________
    

    In rails you can define table_name with More information in doc:

    http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-table_name

    Also you can redifine attributes with alias_attribute More information in doc: http://api.rubyonrails.org/classes/Module.html#method-i-alias_attribute

    To change primary key primary_key More information in doc: http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/PrimaryKey/ClassMethods.html#method-i-primary_key-3D

    The example will be seems as

    class Post < ActiveRecord::Base
      self.primary_key = 'sysid'
      self.table_name  = 'old_posts`
    
      alias_attribute :name, :title
      alias_attribute :description, :content
    end
    

    Then you can invoke:

    p = Post.find(1)
    p.title #= >  The best post
    

    Also you can use legacy database separately from rails app database

    http://ilikestuffblog.com/2012/09/21/establishing-a-connection-to-a-non-default-database-in-rails-3-2-2/