Loading data in migrations

I found migrations to be very useful tool to maintain data structures in a rails project. It is clean, easy to understand and fast when it comes to recreate a database.

During develoment, I faced several times the same problem: “How to import static data into the database?” Of course, one can create fixture and load it. But sometimes one needs more powerful tool.

So, I tried to import the data in the migrations themselves. Since the data to import was huge (tens of MBs), I did try several methods and approaches. Here is the result.

Fixtures in Migrations

This is method that I found on the web in an article of Adam Christensen Loading Fixtures in a Migration. It shows how to load the fixtures file into database.

Active record way

If your ActiveRecord object already exists, you can use the following code. It is obviously not the way to import thousands of records, but for ten objects it is good enough.

External CSV

If the data came in the CSV format then a fastercsv library could be used.

First of all, the library needs to be installed.

then create the migration

And place the CSV file (example below) into db/data folder.

Embedded CSV

Sometimes it is useful to bundle the data directly with the migration. In this example, the data are provided to the load_articles method from articles_data.

ActiveRecord – native commands

Last, but not least is the method that uses native functions of MySQL database. This could work only if the rails application sits on the same machine as the MySQL.

and the corresponding CSV file

That’s it! Enjoy it.