Simply On Rails - Part 4: Quick and Easy Default Data Migrations

Published on

By Fabio Cevasco

In the last post of this series I tried to find a DRY solution to deal with tables storing “ancillary” data, i.e. names of user roles, predefined categories, page state names and other similar things.
I personally chose to put this kind of data to make my application more dynamic, although I could have decided to use ENUMs or simply ordinary varchar fields — that would have been easier, but less flexible. For now, I’m sticking with my original choice.

The data in these tables is kind of a prerequisite for the application to run: I must be able to have a status to assign to a user when creating it, and the same applies to roles. Sure, I could spend 20 minutes populating these tables manually, but it would be nice if there was a less tedious way, wouldn’t it?

There is indeed. The inspiration came from a technique described in the book (which I highly recommend) Agile Web Development With Rails, in which the author outlines how it would be possible to use Rails’ fixtures and migrations to load data in the database automatically from YAML files.
All you have to do is create a migration to load the specified YAML files and you’re all set.

I wanted to take a little step further, allowing the migration to load data from all YAML files in a specific directory, automatically.Let’s start creating the YAML files then and place them all in one directory of the application like /db/migrate/defaults. Here’s the one I used for user roles, for example:

 1visitor:
 2  id: 1
 3  name: Visitor
 4  level: 0
 5
 6user:
 7  id: 2
 8  name: User
 9  level: 10
10
11contributor:
12  id: 3
13  name: Contributor
14  level: 20
15
16provider:
17  id: 4
18  name: Provider
19  level: 50
20
21operator:
22  id: 5
23  name: Operator
24  level: 100
25
26administrator:
27  id: 6
28  name: Administrator
29  level: 500
30
31webmaster:
32  id: 7
33  name: Webmaster
34  level: 1000

The important thing to remember is to provide a unique string to identify each record, before specifying each fiels. The other files look similar, so I won’t bother listing them here.

And here’s the simple code for the migration:

 1require 'active_record/fixtures'
 2
 3class LoadDefaults < ActiveRecord::Migration
 4
 5  def self.up
 6    down
 7    models = self.default_models
 8    models.each do |m|   
 9      Fixtures.create_fixtures(self.default_directory, m)
10    end
11  end
12
13  def self.down
14    models = self.default_models
15    models.each do |m|
16      eval("#{m.singularize.capitalize}.delete_all")
17    end
18  end
19
20  def self.default_directory
21    File.join(File.dirname(__FILE__), "defaults" )
22  end
23
24  def self.default_models
25    files, names = Dir.glob("#{self.default_directory}/*.yml"), []
26    unless files.blank?
27      files.each { |f| names << File.basename(f, '.yml') }
28      names
29    else
30      []
31    end
32  end
33
34end

Basically the migration will look in a directory named “defaults” for some YAML files named after a particular database table, and it will attempt to load all the records defined in each one of them.
The down method of the migration deletes all the data in the specified tables, so use with care…

Legacy Comments

These comments were imported automatically from an old version of this web site. Scroll down for the newest stuff.

Very nice. I still prefer to include my migrations as rails code User.create() in my migrations to avoid having developers create dodgy yaml files though, especially with pks.

Indeed, indeed you are right. You have to be careful when you prepare the YAML files, and in certain situation (and for certain kind of data) a more standard approach is recommendable.

Cool code and post. I second the idea of using Migrations with such application.

Thanks

Migrations would make the application much more flexible in terms of moving it among different DB servers and more.