Skip to content
Import CSV and keep primary key
Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
README.txt
csv_import.rake

README.txt

I created this task to overcome the lack of PK consistency in my import.
I wanted to pick and choose data from csv files and import it in the db and keep the primary key.

This rake task does just that with the following parameters
param1 => csv file location ie. some_backup_file.csv
param2 => Model to update ie. User
param3 => CSV Column Primary key to keep ie. userid
param4 => Create rails migration code ie. true
param5 => Create blank record in the DB ie. true

rake 'csvimport:model_keep_id[param1,param2,param3,param4,param5]'

I figured that if the model had the same column name as the csv header column name, then import it!
So, to import a column, rename it the model attribute name, the task will create 2 arrays.
The first array is the CSV column keys.
The second array is the Model Column keys.
That task will import the common fields and create a rails migration with the difference of keys.

To create a migration, run with the migration attribute set to true
rake 'csvimport:model_keep_id[some_backup.csv,User,userid,true]'
Then copy the printed migration to the cli and run it.

To create blank records before updating the data, run with the create record attribute set
rake 'csvimport:model_keep_id[some_backup.csv,User,userid,false,true]'


Let's take an example,

Sample CSV:
"userid","firstname","lastname","phone_number","email","address"
1,"john","doe","786-9866","john@doe.com","24 nw 5 street Rocky Mount, CO"
...

I want to import the data into the User model

my User model has only a name attribute. 
First, let's create the migration
rake 'csvimport:model_keep_id[some_backup.csv,User,userid,true]'

In the output, copy the rails migration command and copy to cli.
Trim as desired and Execute to create the missing fields in the model.
Next run rake db:migrate

Next we need to update the attributes but first do you need to create the blank records? 
YES, create blank records in the database:
rake 'csvimport:model_keep_id[some_backup.csv,User,userid,false,true]'

NO, just update, the records already exist.
rake 'csvimport:model_keep_id[some_backup.csv,User,userid,false,false]'

There it is. Hope that works for you...

Jean-Luc

Something went wrong with that request. Please try again.