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
Rake::Task["db:fixtures:load"].invoke

#clone development to test
Rake::Task["db:test:clone_structure"].invoke

#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;
exit
EOF"
end


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:
http://www.railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial
http://railscasts.com/episodes/66-custom-rake-tasks

2 comments:

bluecell said...

From this link:

http://marcuswestinblog.blogspot.com/2008/02/ruby-actsastaggable-plugin-bug-fix.html

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.

http://74.125.155.132/search?q=cache:chRd0hLIRfoJ:https://trac-git.assembla.com/breakout/browser/vendor/plugins/acts_as_taggable/lib/taggable.rb%3Frev%3D5a99074fb77c3ff0118caa7029d4e09a66efa75e%26format%3Dtxt+tag_collection.push_with_attributes+tag_collection<<&cd=1&hl=vi&ct=clnk&gl=vn

Subscribe in a Reader