This are my notes in the fields of computer science and technology. Everything is written with ABSOLUTE NO WARRANTY of fitness for any purpose. Of course, feel free to comment anything.

Thursday, August 14, 2008

Execute a Rake task in another

It is easy to make one task dependent on another, for example:

task :one => [:two, :three]

executes task two, three, then one (I have to test that the order is really this). But how to execute task ":two" in the middle of the code of another task?

Here is the solution:

desc "This task executes task two in its code!"
task :one do

# ... do domething

ENV['PAR1'] = 'xxx'
ENV['PAR2'] = 'yyy'
Rake::Task[ "two" ].execute

# ... do something

end

the ENV assignments and execute call have a similar effect to executing in your shell:

rake two PAR1 = xxx, PAR2 = yyy

Yeah, Rake is a really easy and cool tool for every scripting need...

Fake migrations for rails < 2.0.2

The following rake task can be used to update the migration pointer without actually migrate in rails databases for rails under 2.0.2. This is sometimes useful if you applied something manually or want to skip some migration anyway.

Usage examples:

rake db:pretend:migrate
rake db:pretend:migrate VERSION=20080730181045
rake db:pretend:rollback
namespace(:db) do
namespace(:pretend) do
desc "Pretend the database migrated 1 step or to VERSION=<nn>"
task :migrate => :environment do
c = ActiveRecord::Base.connection
if ENV['VERSION']
version = ENV['VERSION'].to_i
else
version = c.select_one("select version from schema_info")['version'].to_i + 1
end
c.execute("update schema_info set version = #{version}")
puts "Current version: #{version}"
end
desc "Pretend that the last migration did not happen"
task :rollback => :environment do
c = ActiveRecord::Base.connection
version = c.select_one("select version from schema_info")['version'].to_i - 1
c.execute("update schema_info set version = #{version}")
puts "Current version: #{version}"
end
end
end

oracle adapter and rails migrations

The Oracle adapter for active record has a bug that does not allow to create a dump of the schema using rake. The following code is from http://dev.rubyonrails.org/ticket/10415 and corrects this problem.
require 'active_record/connection_adapters/oracle_adapter'
module ActiveRecord
module ConnectionAdapters
class OracleAdapter
# Returns an array of arrays containing the field values.
# Order is the same as that returned by #columns.
def select_rows(sql, name = nil)
result = select(sql, name)
result.map{ |v| v.values}
end
end
end
end

About Me

My photo
Hamburg, Hamburg, Germany
Former molecular biologist and web developer (Rails) and currently research scientist in bioinformatics.