Thursday, May 14, 2009

Custom Rake Tasks

I came across to write a custom rake script for my rails application. Because my application is rich client side application and doesn't use id for any of my models. Instead, it used UUID as a primary key. I got some problems while changing this, and one of them is rails rake commands such as rake db:test:prepare, rake test:functionals, .... For my application, these two commands didn't work really well since it makes my test database messed with autonumber id of all tables. Here, my rake script to load to my test database. Pay attention to mysql command, in this case, I need to run several commands inside mysql command.

task :load_test_db do
#load data to development

#clone development to test

#pull data from development
sh "mysqldump > test.sql development_ncdd_templates -u root"

sh "mysql -u root << EOF
use test_ncdd_templates;
source test.sql;

If you want to run bash command, take this as an example. I'm not a expert of shell script, but it works for me anyway.

sh "#!/bin/bash
      cd lib
      ruby to_yml.rb
      cd .."

Here are some excellent sources:


bluecell said...

From this link:

Can u please explain to me how to change push_with_attributes to << ?

Thank you.

bluecell said...

Hey, I found the solution here. Thanks for your hint.<<&cd=1&hl=vi&ct=clnk&gl=vn

Subscribe in a Reader