How to transfer legacy mysql mysql data to new Rails data model?

I am trying to port an old PHP application to Rails. I dumped the old mysql tables and uploaded them to the server running the new rails application. How can I transfer data from old legacy tables to the new Rails model? I could write a PHP script to spit everything out into a ruby, and then use it to populate seed.rb, but it seems to me that there should be an easier way to achieve this.

The new application is RoR 3.0.9, and the database is still mysql.

+4
source share
1 answer

I would write a rake task to mount old tables and dump them into a new db. Something like that:

# config/database.yml legacy_db: adapter: mysql username: foo password: bar # lib/tasks/import.rake namespace :import do desc 'import the legacy db data' task :legacy => :environment do # connect to legacy db class OldDb < ActiveRecord::Base establish_connection :legacy_db end # define classes for legacy tables class OldUser < OldDb set_table_name 'user' set_primary_key 'user_id' end # ...do this for all your old tables # import from old models to new models OldUser.all.each do |u| User.create( :user_name => u.login_name :created_at => Time.parse(u.account_opened_date) # etc.... ) end end end 

and call with: RAILS_ENV=production bundle exec rake import:legacy

+9
source

Source: https://habr.com/ru/post/1385134/


All Articles