capistrano not running migrations in staging - ruby-on-rails

I am using capistrano 3 and I run cap staging deploy. After various commands, it then runs rake db:migrate, which looks like this:
INFO [f67aeefb] Running /usr/local/rvm/bin/rvm ruby-2.1.2#core do
bundle exec rake db:migrate as deploy#10.10.4.131 DEBUG [f67aeefb]
Command: cd /var/www/mysite_staging/releases/20150617233945 && (
RAILS_ENV=staging /usr/local/rvm/bin/rvm ruby-2.1.2#core do bundle
exec rake db:migrate ) DEBUG [f67aeefb] "Env => staging"
The problem is the migration appears to have run in production, not in staging.
How can I ensure capistrano runs the migrations in staging when I specify staging during the deploy?

I'm not sure which of it did it, but by adding the following to staging.rb:
set :rails_env, 'staging'
set :database_name, 'core_staging'
And then in my apache virtual host adding:
RailsEnv staging
It seems to now recognize the staging database.

Related

Command to run heroku run rake db:migrate on production db when you also have a staging db in heroku

I have a ruby on rails app, leveraging Postgres running in heroku. I have a staging environment and a production environment in heroku. I want to run the heroku run rake db:migrate on the production environment however I can find the command to do so, any help would be greatly appreciated
I think you can just set the remote to get it to pick the right environment:
heroku run rake db:migrate --remote production
When I ran heroku run rake db:migrate --remote it gave me three choices (heroku, staging & test) I ran the command: heroku run rake db:migrate --remote heroku and it worked!! (didn't understand that heroku was calling my production environment "heroku" and not production) thanks again for all your help

Create alias for production stage in Capistrano?

I've been using Capistrano to deploy my latest project. I have a fairly simple setup, with only a production stage (it created a staging one, but I don't use it).
I'm tired of typing cap production deploy to deploy my app. I really like how Rails handles this, with rails c p being the same as rails console production. I'd like to be able to type this:
cap p deploy
instead of the longer production version.
I've tried making a simple alias:
ln production.rb p.rb
Running cap p deploy gets through almost all of the deploy steps, then dies when running Rake migrations:
INFO [03d68697] Running ~/.rvm/bin/rvm 2.2.2 do bundle exec rake db:migrate as ubuntu#mydomain.com
DEBUG [03d68697] Command: cd /var/railsapps/metasmoke/releases/20150904155532 && ( RAILS_ENV=p ~/.rvm/bin/rvm 2.2.2 do bundle exec rake db:migrate )
DEBUG [03d68697] Please require the stackprof gem falling back to fast_stack
DEBUG [03d68697]
DEBUG [03d68697] config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:
DEBUG [03d68697]
DEBUG [03d68697] * development - set it to false
DEBUG [03d68697] * test - set it to false (unless you use a tool that preloads your test environment)
DEBUG [03d68697] * production - set it to true
DEBUG [03d68697]
DEBUG [03d68697] rake aborted!
DEBUG [03d68697] ActiveRecord::AdapterNotSpecified: 'p' database is not configured. Available: ["default", "development", "test", "production"]
DEBUG [03d68697] /var/railsapps/metasmoke/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection'
How can I do this?
You probably need to add
set :rails_env, 'production'
to production.rb. Capistrano is inferring the Rails environment name from the stage name.

Delayed Job in Rails 4 with Capistrano

I cant figure how to start Delayed Jobs on a dedicated Ubuntu server.
It works fine on my localhost but when I run on my server
sudo RAILS_ENV=production bin/delayed_job restart
I get
sudo: bin/delayed_job: command not found
On top of that, if I run the "rake jobs:work RAILS_ENV=production" command Im getting the following error:
PG::FeatureNotSupported: ERROR: SELECT FOR UPDATE/SHARE is not allowed in subqueries
Apparently theres an issue with my psql version.
Is there any way I can get the script to work? Any effective Capistrano recipes available? All ive found on the web are old recipes for Rails 3 and older versions of capistrano.
Thanks in advance.
EDIT:
I have already bundled install the daemons gem and generated "delayed_job:active_record" on my local machine, then proceded to cap deploy which bundle installed and migrated in the production server.
The bin/delayed_job file exists in the server yet it fails with command not found.
And add this to config/environment.rb:
ENV['RAILS_ENV'] ||= 'production'
Then at your production server:
RAILS_ENV=production rake db:migrate
RAILS_ENV=test production generate delayed_job:active_record && RAILS_ENV=production rake db:migrate
Now after you do that:
RAILS_ENV=production script/delayed_job start
As for Capistrano error you are facing, please try to add the command like:
run "cd #{current_path}; #{sudo} RACK_ENV=production bundle exec #{current_path}/bin/delayed_job start"
You must run this on target server:
bundle exec rails generate delayed_job

Capistrano deploy:migrate Could not find rake-0.9.2.2 in any of the sources

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

How to modify capistrano deploy to automatically run migrations in Rails 3.0

Right now, I have to run cap deploy and cap deploy:migrations if there are migrations to be run.
how I modify the cap deploy task to run migrations.
Just add:
after "deploy:update_code", "deploy:migrate"
to your config/deploy.rb.
Running "cap deploy:migrations" is equivalent to running "cap deploy" && subsequently "cap deploy:migrate" in one command.
To view the description for deploy:migrations, just use the -e option:
cap -e deploy:migrations
will give you this output:
cap deploy:migrations
Deploy and run pending migrations. This will work similarly to the
'deploy' task, but will also run any pending migrations (via the
'deploy:migrate' task) prior to updating the symlink. Note that the
update in this case it is not atomic, and transactions are not used,
because migrations are not guaranteed to be reversible.

Resources