Let say I have this Rake task:
namespace :db do
namespace :dump do.
desc 'Backup database dump to s3'
task :backup => :environment do
cmd = ['backup', 'perform', '-t project_backup', "-c #{Rails.root.join 'lib', 'backup', 'config.rb'}"]
system(*cmd) # ...I've tried `` & exec() sa well, same thing
end
end
end
Backup gem is stand alone ruby gem application which dependencies needs to be isolated from application bundler. In other words it cannot be part of Gemfile
. This gem is simply installed over gem install backup
When I run backup
command over bash console, it successfully run:
$ backup perform -t validations_backup -c /home/equivalent/my_project/lib/backup/config.rb
When I execute rake db:dump:backup
I will get
backup is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
...which is the same thing when I run backup command with bundle exec
from bash
$ bundle exec backup perform -t validations_backup -c /home/equivalent/my_project/lib/backup/config.rb
...meaning that the backup
command is executed over bundler when run as part of rake task.
my question: How can I run rake db:dump:backup
outsite the bundle scope, meaning that backup
command won`t be executed over bundler?
Thank you
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.
bundle exec is a Bundler command to execute a script in the context of the current bundle (the one from your directory's Gemfile). rake db:migrate is the script where db is the namespace and migrate is the task name defined.
Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.
Rake is a Rubygem that automates many common functions required to build, test, package, and install programs; it is part of every modern Ruby installation, so you don't need to install it yourself. Here are some common Rake tasks that you may encounter: Set up required environment by creating directories and files.
I found a workaround for this problem here:
namespace :db do
namespace :dump do
desc 'Backup database dump to s3'
task :backup do
Bundler.with_clean_env do
sh "backup perform -t project_backup -c #{Rails.root.join 'lib', 'backup', 'config.rb'}"
end
end
end
end
The key here is to enclose the code that must not run under bundler's environment in a block like this:
Bundler.with_clean_env do
# Code that needs to run without the bundler environment loaded
end
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