I have rails application that connects to multiple databases. I wrote custom rake task that looks like this:
task :migrate_accounts_schema => [:environment] do |t|
users = User.find :all, :conditions => ["state = 2"], :order => "id asc"
users.each do |user|
if user.state == 2
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => user.database_host,
:port => user.database_port,
:username => user.subdomain,
:password => "#{user.database_password}",
:database => user.database_name
)
Rake::Task["db:migrate"].invoke
end
end
end
The problem is that task executes db:migrate only for users[0] user (first user in collection) and there is no errors, just stoppes silently...
Here's output from rake --trace
** Invoke app:migrate_accounts_schema (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute app:migrate_accounts_schema
** Invoke db:migrate (first_time)
** Invoke environment
** Execute db:migrate
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
** Invoke db:migrate
I have no idea why the rest of users don't get migrated.
Rake is a software task management and build automation tool created by Jim Weirich. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace. It is similar in to SCons and Make.
You can use abort("message") to gracefully fail rake task. It will print message to stdout and exit with code 1.
Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.
You have learned about Rake, a popular task runner for Ruby. Use rake -T to find out what tasks are available, create your own tasks & add them to the Rakefile , or inside the lib/tasks folder, and remember that Rake & Rack are different things.
I found answer in the Rake source:
http://rake.rubyforge.org/classes/Rake/Task.html#M000115
It says that you have to
Reenable the task, allowing its tasks to be executed if the task is invoked again.
e.g. I used this recently on my project this way:
# db/seed.rb
Rake::Task['catalog:destroy'].invoke
files = Dir.glob("private/catalog/*").sort
files.each do |file|
next unless File.extname(file) == '.xlsx'
puts file.split('/').last
Rake::Task['catalog:upload'].invoke(file)
Rake::Task['catalog:upload'].reenable
puts
end
So I run rake catalog:upload[some_file] every loop.
Hope this helps. Also see https://stackoverflow.com/a/1290119/3082929
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With