0

I have app that uses sqlite db and there are entries in that table. Now i have to change sqlite to postgres. And the table design is also changed.

If it is the same table design then i would have gone to use taps or dump the data using yaml-db and then push the data to postgres, but in my scenario the table design also changes but i want to move the data from the sqlite to postgres according to the new table.

So i thought of exporting the data from sqlite to a csv file and then move the data from csv to postgres. Is this is the way i have to do or is there is any other way for doing it? If this is the way then how can i export to csv and import to postgres?

Another thing is, after exporting the data from sqlite, is there is a way to push the data through migration? please help me.

1 Answer 1

1

To export in a CSV format just add to a controller action a respond_to for csv and then in the matching view folder create a .csv.erb file to create the csv. This can then be called by just adding .csv to the URL.

So your controller would be something like this:

def index
  @people = Person.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @people }
    format.csv
  end
end

And you view which would be saved in a file (something like /app/views/people/index.csv.erb) would contain:

First Name,Last Name
<% @people.each do |p| %>
  <%= raw "\"#{p.first_name}\"" %>,<%= raw "\"#{p.last_name}\"" %>
<% end %>

This way creating you CSV is not dependent on the database in use.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.