working with database from rake tasks

Hello,

I would like to perform several database queries and updates from a
rake task. All of the example code I have found on the web use
ActiveRecord to create a connection and generally perform ad-hoc sql
or use very low level ActiveRecord functionality. I haven't found any
code that use model from the tasks. I have experimented using several
models in the rake tasks and it seems to work, except if any of the
models are using plugins.

Is it a bad practice to use models from rake tasks? If it is why? If
it isn't what would be the best way to load the plugins?

Thanks,
Dan

Dan wrote:

Hello,

I would like to perform several database queries and updates from a
rake task. All of the example code I have found on the web use
ActiveRecord to create a connection and generally perform ad-hoc sql
or use very low level ActiveRecord functionality. I haven't found any
code that use model from the tasks. I have experimented using several
models in the rake tasks and it seems to work, except if any of the
models are using plugins.

Is it a bad practice to use models from rake tasks? If it is why? If
it isn't what would be the best way to load the plugins?

Have you tried following:

http://nubyonrails.com/articles/2006/02/25/rake-rake-rake-your-boat

Dan,

  > I would like to perform several database queries and updates from a
  > rake task. All of the example code I have found on the web use
  > ActiveRecord to create a connection and generally perform ad-hoc sql
  ...

You can of course use ActiveRecords base methods (ex: save)

    desc "touch all the records of many classes"
    task :touch_many_records => :environment do
      %w(Article Post Comment Script).each{|klass|
        klass.constantize.find(:all).each{|p| p.save}
      }
    end

, but you can also move more action in the model (aka 'Fat Model')

    desc "do complex stuff"
    task :complex_stuff => :environment do
      FooBar.do_a_lot_on_complex_stuff_and_even_move
      puts "done"
    end

This second method is usually easier to test.

Alain Ravet

Alain,
In reading your second example it appears you have a task =
complex_stuff. Where do you put this task and what does it look like?
Would it look like code you would normally put inside a controller?
Why do you need to reference :environment? Is this how you get RAKE to
see the AR information and thus be able to manipulate data as we do in
our controllers?
I am grateful for your direction on this.
David

David,

  > In reading your second example it appears you have a task =
  > complex_stuff. Where do you put this task

Tasks live in rake files, like :

   <app_root>/lib/task/yourproject.rake

  > > task :complex_stuff => :environment do
  > > ...
  > Why do you need to reference :environment?

.. because 'rake' means 'Ruby Make', not 'Rails Make' => you need to
tell it to load Rails in order to have access to Rails
functionalities.

On the right of the arrow are other tasks that are executed before the
task own code. In this case, there is a system task called
:environment.
If you have rails:freeze:edge your project, look with your editor for
the string
     'task :environment'

=> vendor/rails/railties/lib/tasks/misc.rake

   task :environment do
     require(File.join(RAILS_ROOT, 'config', 'environment'))
   end

This task will load
     config/environment.rb,

that in turn will bootstrap Rails with :
    require File.join(File.dirname(__FILE__), 'boot')

hint: look at public/dispatch.fcgi (f.ex). It loads Rails the same way:

    require File.dirname(__FILE__) + "/../config/environment"

Alain Ravet

Alain,
One last question. I understand now how you would write your RAKE
tasks and put them in a \RailsProjectName\lib\tasks\rakefilename.rake.
My goal is to learn to run a CSVImport routine to read .csv values
into my newly declared table. In Dave Thomas's only example on page
226 he uses this example:

namespace :db do
  desc "Prints the migration version"
  task :schema_version => :environment do
    puts blah, blah

1. What is this "namespace" part about and is "db" the one I'll use to
import a .csv file?
2. What is the "desc" part about? Is this like a simple comment to
document what's going on with this task?
Once again, thank you for your help.
David

David,

"desc" is what you see when you type :
    rake -T

Namespaces let you group tasks, and makes for clearer calls as in
    rake db:test:prepare

- 'prepare' is the task,
- 'db' and 'test' are the namespaces

Alain