banditKing
banditKing

Reputation: 9579

Best way to import data into my new rails app?

Hi I have a sqlite3 database full of data from my previous version of the web app which WASN'T written in rails. Im now rewriting the web app in rails from scratch. But Id like to use the data from my previous app in the new rails app. Whats the best way to accomplish this?

This is what Ive tried so far and it didn't work very well: 1) I created a new rails app

2) replaced my sqlite3 database in the new app with the sqlite3 database from the old app

3) Created a model with the same schema as the old DB.

4) changed the databse.yml file with the updated DB file details

5) un my model added the "establish_connection" method

6) I could thus get it to show me all the details of the old database in my browser @"index.html"

7) However, I ran into problems when wanting to insert/edit records into the DB. Since the DB did not have a primary key column for each row, it didnt work.

8) So I tried to do a migration to add a column with a primary key . It didnt work

9) suddenly a new development.sqlite3 database had shown up in the app and it was trying to add a primary key to the NEW DB.

10) So I just deleted the new DB that had popped up and after that the app wasn't working

11) Now I want to start from scratch and so my question: "Whats the best way to import data from a previous non-rails app (in a sqlite3 DB format) into a new rails app"

Upvotes: 4

Views: 1272

Answers (1)

JohnColvin
JohnColvin

Reputation: 2509

You want to be working with 2 databases, the old and the new. So, your database.yml needs to have 2 connections in it. One should be your normal connection called development or production and the other should be called legacy. Fill out their different connection information. By default, your ActiveRecord models will pull from the one not called legacy.

Create a model called LegacyBase. In the model put establish_connection "legacy". Now create a directory at app/models/legacy. Put all of the models represent data from the old DB inside this directory. All of those models should extend from LegacyBase. This will mean they're all reading from the old DB.

Create your new models. Do you want them to have a primary key column? If not, see this answer. Create an ActiveRecord database table with no :id column?. They will by default have an id column, though and I recommend leaving it that way.

For each of the legacy models, write a method called to_model. In this method, write the code that creates a new object and populate it with the old data and save it. Something like this:

class OldUser < LegacyBase

    establish_connection "legacy"

    def to_model
        User.create!(self.attributes)
    end
end

You can do any logic that needs to happen to make the old data fit your new app. Call this method on all of the old records like OldUser.all.map(&:to_model).

Do this for all of the tables that you want to move over.

Upvotes: 5

Related Questions