Populating Rails application with Rake and YAML

Following on from my post on using custom Rake tasks for application deployment, I have also used this method for populating initial data for users and roles.

At first glance this seems to conflict with the builtin seed task in the db: namespace but I have always had huge problems using seed (silent and almost impossible to debug failures) to the point that I considered rake to be a bit of a liability So, as much as anything, this is about restoring confidence in Rake as a core part of Rails application development.

Either using the Rails generator or simply editing the file by hand, we have the following task in lib/tasks/db.rake,

namespace db: do
  desc "Create basic site users"
  task task_prep_sessions: :environment do
    ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['development'])
    puts "Loading user data from YAML"
    sessions = YAML::load_file("lib/tasks/post_migrate_users.yml")
    puts "Creating session users."
    sessions.each do |s|
      Session.create(:id                  => s.id,
                     :username            => s.username,
                     :sign_in_count       => s.sign_in_count,
                     :current_sign_in_at  => s.current_sign_in_at,
                     :last_sign_in_at     => s.last_sign_in_at,
                     :current_sign_in_ip  => s.current_sign_in_ip,
                     :last_sign_in_ip     => s.last_sign_in_ip,
                     :remember_created_at => s.remember_created_at,
                     :created_at          => s.created_at,
                     :updated_at          => s.updated_at)
      puts "Created session user #{s.username}"
    end
  end

These are based on the standard user/session model used by Devise. The user records themselves are defined in the YAML file, lib/tasks/post_migrate_users.yml, with the following format,

---
- !ruby/object:Session
  attributes:
   id: 1
   username: admin
   sign_in_count: 1
   current_sign_in_at: 2014-08-01 09:32:24
   last_sign_in_at: 2014-08-01 09:32:24
   current_sign_in_ip: 127.0.0.1
   last_sign_in_ip: 127.0.0.1
   remember_created_at: 2014-08-01 09:32:24
   created_at: 2014-08-01 09:32:24
   updated_at: 2014-08-01 09:32:24
- !ruby/object:Session
  attributes:
   id: 2
   username: editor
   sign_in_count: 1
   current_sign_in_at: 2014-08-04 11:32:24
   last_sign_in_at: 2014-08-04 11:32:24
   current_sign_in_ip: 127.0.0.1
   last_sign_in_ip: 127.0.0.1
   remember_created_at: 2014-08-04 11:32:24
   created_at: 2014-08-01 09:32:24
   updated_at: 2014-08-01 09:32:24

As before, rake -T will show this in the task list in the db: namespace and it is simply run with,

$ rake db:task_prep_sessions
Loading user data from YAML
Creating session users.
Created session user admin
Created session user editor

The main benefit with using this approach over db:seed is that if the users already exist an error will be thrown which is far more useful in a deployment scenario.

We might also be able to use some of this datafor some automated testing

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s