Problem
Some rake tasks provided by gems did not show up with rake -T or rake -T -A (rake > 10).
Discovery
Gems listed only in the :development group are not loaded when rake -T is run. I found that this is likely because ENV['RAILS_ENV'] is being set to test when running rake -T.
However, rake tasks are invoked in the development environment. Why is rake -T setting the RAILS_ENV to test?
Related
I have the following .travis.yml file:
language: ruby
rvm:
- "2.0.0"
# uncomment this line if your project needs to run something other than `rake`:
before_script:
- psql -c "create database dummy_test;" -U postgres
- psql -c "CREATE USER dummy WITH PASSWORD 'dummy';" -U postgres
script:
- RAILS_ENV=test bundle exec rake db:migrate --trace
- bundle exec rake db:test:prepare
- bundle exec rspec spec
When I try running it in Travis CI, I get this error message:
$ RAILS_ENV=test bundle exec rake db:migrate --trace
rake aborted!
Don't know how to build task 'db:migrate'
I've been trying different approaches for hours. What am I doing wrong?
Finally got it working.
The issue was that the application was a dummy application buried within spec/dummy, so running rake db:migrate wouldn't work from the root directory. In order to fix this, I followed the advice given here. Edit the Rakefile to point to spec/dummy and then run rspec tests:
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'
require "rspec/core/rake_task"
task :default => :spec
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
# spec.rspec_opts = ['-cfs --backtrace']
end
What's the difference between adding a RAILS_ENV before or after a rake task? Here are samples from my staging environments:
Adding RAILS_ENV after rake task.
This raised an error, and the reason for this is accepting development environment as by default and not taking devutility as the environment.
$bundle exec rake -T RAILS_ENV=devutility
$rake aborted!
$cannot load such file -- rack/bug
Adding RAILS_ENV before rake task
This works and lists all the rake task available.
$RAILS_ENV=devutility bundle exec rake -T
rake about # List versions of all Rails frameworks and the environment
rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake bourbon:install[sass_path] # Move files to the Rails assets directory
rake ci # Continuous Integration build (simplecov-rcov and deploy)
rake cucumber # Alias for cucumber:ok
rake cucumber:all # Run all features
rake cucumber:ok # Run features that should pass
rake cucumber:rerun # Record failing features and run only them if any exist
rake cucumber:wip # Run features that are being worked on
rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:data:dump ....................
..............
RAILS_ENV is an environment variable that needs to be available before running your rake task.
When you do:
RAILS_ENV=devutility bundle exec rake -T
It has the same affect as:
export RAILS_ENV=devutility
bundle exec rake -T
RAILS_ENV is not an argument to rake as it may appear, it's part of the environment available to Ruby though it's ENV constant.
My Capistrano deploy:migrate task is set to run a simple rake db:migrate command, as follows:
env PATH=/home/user/.gems/bin sh -c 'cd /home/user/app/releases/20121003140503 && rake RAILS_ENV=production db:migrate'
When I run this task during an ssh session manually it completes successfully. However when I run from my local development box, I receive the following error:
** [out :: app] Could not find rake-0.9.2.2 in any of the sources
I am able to locate my rake gem by typing which rake via ssh (/home/user/.gems/bin/rake) and rake --version gives me "rake, version 0.9.2.2," so I don't understand why this command fails via Capistrano?
Capistrano does not place bundle exec before rake command in default. If you are sure you have the rake gem in your bundle, try adding this to your deploy.rb.
set :rake, 'bundle exec rake'
This will tell Capistrano to instead of just rake run bundle exec rake. If it is in your bundle, you won't have any problems any more and you will also avoid collisions if you have more versions of rake installed on your system.
You might also just need to bundle your gems with this:
require "bundler/capistrano"
Via: Why is Capistrano not installing gems with bundler?
Once you go into your app folder, you simply type:
$bundle exec rake instead of just $rake
I can run all models with rake:models
Any option to do that that for cucumber tests instead of just rake and running all tests at all levels?
I tried:
$ rake spec:cucumber
rake aborted!
Don't know how to build task 'spec:cucumber'
You should be able to just run rake cucumber
A quick output of rake -T shows:
rake cucumber # Alias for cucumber:ok
rake cucumber:all # Run all features
rake cucumber:ok # Run features that should pass
rake cucumber:rerun # Record failing features and run only them if any exist
rake cucumber:wip # Run features that are being worked on
I've been doing Ruby on Rails development with ElasticSearch between two machines and its starting to get a little annoying. My usual workflow is:
git pull
bundle install
rake db:migrate (or rake db:setup depending)
rails server
elasticsearch -f -D myconfig.xml
rake environment tire:import CLASS=MyObject FORCE=true
Is there anyway I can add all of these commands to some type of start up script in Rails to bring them all into one place? It would make bringing up a dev environment a lot easier on me everytime I switch machines.
The best way I've found is to use the Foreman gem to kickstart your project and associated processes.
It looks like you should do this in your deployment using Capistrano. Here is an example config/deploy.rb file:
[basic parts omitted]
after "deploy", "bundler:bundle_install"
after "bundler:bundle_install", "db:db_migrate"
after "deploy:db_migrate", "deploy:elastic_search_indexing"
namespace :bundler do
desc 'call bundle install'
task :bundle_install do
run "cd #{deploy_to}/current && bundle install"
end
end
namespace :db do
desc 'fire the db migrations'
task :db_migrate do
run "cd #{deploy_to}/current && bundle exec rake db:migrate RAILS_ENV=\"production\""
end
end
namespace :elasticsearch do
desc 'run elasticsearch indexing via tire'
task :index_classes do
run "cd #{deploy_to}/current && bundle exec rake environment tire:import CLASS=YourObject FORCE=true "
end
end
[rest omitted]
Make sure you have a config file on the target machine (Linux) in /etc/elasticsearch/elasticsearch.yml with contents like:
cluster:
name: elasticsearch_server
network:
host: 66.98.23.12
And the last point to mention is that you should create an initializer config/initializers/tire.rb:
if Rails.env == 'production'
Tire.configure do
url "http://66.98.23.12:9200"
end
end
As you can see, this is the exact same IP address, but only used for the production environment. I assume that you access elasticsearch locally (in development mode) via localhost. elasticsearch is connection per default to
http://0.0.0.0:9200
A good starting point and also in depth help is provided by awesome Ryan Bates and his Railscasts http://railscasts.com/episodes?utf8=%E2%9C%93&search=capistrano
Whats keeping you from putting it in a bash script? And put the script inside your RAILS_APP_HOME/scripts folder?
#!/bin/sh
git pull
bundle install
rake db:migrate
rails server
elasticsearch -f -D myconfig.xml
rake environment tire:import CLASS=MyObject FORCE=true