How to check the heroku database version from ruby - ruby-on-rails

I want to check the version of my production database from a shell method running on my local MacOS development machine. From the command line, I can successfully run
heroku run rake db:version --remote production
but if I open a rails console session and type in
hv = `heroku run rake db:version --remote production`
I see the following error message:
/Users/Chris/.rvm/gems/ruby-2.5.1#golf_mentor_5.0.7/gems/bundler-1.16.1/lib/bundler/definition.rb:489:in `validate_ruby!': Your Ruby version is 1.9.3, but your Gemfile specified 2.5.1 (Bundler::RubyVersionMismatch)
from /Users/Chris/.rvm/gems/ruby-2.5.1#golf_mentor_5.0.7/gems/bundler-1.16.1/lib/bundler/definition.rb:464:in `validate_runtime!'
from /Users/Chris/.rvm/gems/ruby-2.5.1#golf_mentor_5.0.7/gems/bundler-1.16.1/lib/bundler.rb:101:in `setup'
from /Users/Chris/.rvm/gems/ruby-2.5.1#golf_mentor_5.0.7/gems/bundler-1.16.1/lib/bundler/setup.rb:20:in `<top (required)>'
from /usr/local/heroku/ruby/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/heroku/ruby/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require
I am using ruby 2.5.1. The heroku --version command yields heroku-cli/6.16.13-dbb9c23 (darwin-x64) node-v9.11.1. How do I fix this?

It turns out this is related to this github https://github.com/bundler/bundler/issues/2489. It looks like bundler is confused by a shebang in the heroku cli file. The solution is to use a clean environment for bundler
Bundler.with_clean_env {`heroku run rake db:version --remote production`}

Related

Heroku Rails build failing due to Bootsnap and Rake issues

I'm trying to follow along with the Rails Tutorial and can't seem to get builds working on Heroku.
My build log is here:
https://pastebin.com/pBnGkBXe
Here are the errors I've tried debugging so far:
! Could not detect rake tasks
! ensure you can run `$ bundle exec rake -P` against your app
! and using the production group of your Gemfile.
Running bundle exec rake -P locally does work on my repository.
! /tmp/build_5f6ec788/config/boot.rb:4:in `require': cannot load such file -- bootsnap/setup (LoadError)
! from /tmp/build_5f6ec788/config/boot.rb:4:in `<top (required)>'
! from /tmp/build_5f6ec788/bin/rake:3:in `require_relative'
! from /tmp/build_5f6ec788/bin/rake:3:in `<main>'
I wasn't able to figure out what was happening here.
I tried running this with both the Rails Default Gemfile and the Rails Tutorial Gemfile and it resulted in the same Bootsnap error.
Can anyone make suggestions on how to get this Heroku deployment working?
Thank you!
Figured it out.
I was running Ruby v2.6.3. Upgrade to 2.7.2 and fixing that in the Gemfile resulted in a successful deploy to Heroku.

ActiveRecord::PendingMigrationError - migrating from one server to another

I'm new to Rails so please bear with me.
Currently there is an existing project (Rails 4.1.6, Ruby 2.1.3) running on digital ocean VM (Ubuntu 14.04) and its connecting to Postgresdb 9.6 without any issues. I have to migrate this to new VM.
I have setup a new vm with Ubuntu 16.04, installed Rails and Ruby with exact same versions. I tried creating a test rails app and its working fine. However, when I tried to deployed the project from old server to this new one its giving me errors.
Details:
There was an error while trying to load the gem 'uglifier'.
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
Backtrace for gem load error is:
/usr/local/lib/ruby/gems/2.1.0/gems/execjs-2.7.0/lib/execjs /runtimes.rb:58:in `autodetect'
/usr/local/lib/ruby/gems/2.1.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
/usr/local/lib/ruby/gems/2.1.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>'
/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib /active_support/dependencies.rb:247:in `require'
/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
After searching on internet, most of them suggested to uncomment this line in the Gemfile.
# gem 'therubyracer', platforms: :ruby
This was commented in the old server, however, I tried uncommenting it and it came back with different error.
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
Website was connecting to the same database and old webportal was running fine but it gave me this error. So I setup a new test db and ran the following commands -
RAILS_ENV=production bundle exec rake db:create
RAILS_ENV=production bundle exec rake db:setup
RAILS_ENV=production bundle exec rake db:migrate
All those commands were executed successfully but for some reason I'm still receiving "Migrations are pending....." error.
Please advise.
The migration-error seems to think you should be on the development environment... but the migrations you ran are for production... so either your environment-setting is not correct, or you need to run the migrations for development instead

Migrating database fails after upgrading to Ruby 2.3.0

I've updated to Ruby 2.3.0 and I'm having some issues when creating a new Rails app. After creating a simple new test app and scaffolding a resource, when trying to execute rake db:migrate I'm getting the following load error:
MacBook-Pro:log medright1$ rake db:migrate
/Users/medright1/.rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/rake-10.4.2/bin/rake:31:in `require': cannot load such file -- rake (LoadError)
from /Users/medright1/.rvm/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/rake-10.4.2/bin/rake:31:in `<top (required)>'
from /Users/medright1/.rvm/gems/ruby-2.3.0/bin/rake:23:in `load'
from /Users/medright1/.rvm/gems/ruby-2.3.0/bin/rake:23:in `<main>'
from /Users/medright1/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
from /Users/medright1/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
MacBook-Pro:log medright1$
Any help sorting this would be great!
If you haven't done it yet, make sure you have all the dependencies installed.
$ bundle
or
$ bundle install
It's likely you don't have rake installed in the global RVM gemset. In any case, given you are within a Rails project, you should use bundler to execute the command.
$ bundle exec rake db:migrate
Otherwise, make sure to install rake globally
$ rvm gemset use global
$ gem install rake
However, the correct way is to execute the command via Bundler.

rake assets:precompile error message: No such file or directory -- ruby

I'm following along to the excellent Michael Hartl Rails book (Rails 3.2) but am getting an error when attempting to run the command:
rake assets:precompile
The error is as follows:
/Users/Jamie/.rvm/rubies/ruby-1.9.3-p448/bin/ruby ruby /Users/Jamie/.rvm/gems/ruby-1.9.3-p448#movies/bin/rake assets:precompile assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
/Users/Jamie/.rvm/rubies/ruby-1.9.3-p448/bin/ruby: No such file or directory -- ruby /Users/Jamie/.rvm/gems/ruby-1.9.3-p448#movies/bin/rake assets:precompile (LoadError)
rake aborted!
Command failed with status (1): [/Users/Jamie/.rvm/rubies/ruby-1.9.3-p448/b...]
/Users/Jamie/.rvm/gems/ruby-1.9.3-p448#movies/gems/actionpack- 3.2.14/lib/sprockets/assets.rake:12:in `ruby_rake_task'
/Users/Jamie/.rvm/gems/ruby-1.9.3-p448#movies/gems/actionpack-3.2.14/lib/sprockets/assets.rake:21:in `invoke_or_reboot_rake_task'
/Users/Jamie/.rvm/gems/ruby-1.9.3-p448#movies/gems/actionpack-3.2.14/lib/sprockets/assets.rake:29:in `block (2 levels) in <top (required)>'
/Users/Jamie/.rvm/gems/ruby-1.9.3-p448#movies/bin/ruby_executable_hooks:14:in `eval'
/Users/Jamie/.rvm/gems/ruby-1.9.3-p448#movies/bin/ruby_executable_hooks:14:in `<main>'
Tasks: TOP => assets:precompile
Thanks for your help!
I fixed this by reverting back from p448 to p392 and then running:
rvm ruby-1.9.3-p392#global do gem install executable-hooks
as suggested by mpapis here: Rake assets:precompile cannot find ruby
I was trying to run the rake command within ubuntu and was getting ruby_executable_hooks error. I resolved it with:
rvm all do gem install executable-hooks
This seems to be an issue with 1.9.3-p448. Today I updated ruby from p392 to p448 on my server and started getting this error. I reverted back to p392 and precompilation of assets is working again.
Perhaps try installing a version before p448 and try again.
After working with this a bit more, I found that emptying and reinstalling my gems caused precompilation to start working again:
$ rvm gemset empty
$ bundle install
$ rake assets:precompile RAILS_ENV=production

Can't make calls to heroku CLI from within a Rake task without the Heroku gem in the Gemfile

I've had an app running on Heroku for a while now and on a recent deployment saw the message that the Heroku gem was deprecated in favor of the Heroku Toolbelt. With the toolbelt, all of my Heroku commands work fine from the command line but within a Rake task (which I have setup for deployments) I get the following errors:
$ rake deploy:staging
Everything up-to-date
/Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/gems/bundler-1.2.0/lib/bundler/rubygems_integration.rb:147:in `block in replace_gem': heroku is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/heroku:18:in `<main>'
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/ruby_noexec_wrapper:14:in `eval'
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/ruby_noexec_wrapper:14:in `<main>'
/Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/gems/bundler-1.2.0/lib/bundler/rubygems_integration.rb:147:in `block in replace_gem': heroku is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/heroku:18:in `<main>'
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/ruby_noexec_wrapper:14:in `eval'
from /Users/aramisbear/.rvm/gems/ruby-1.9.3-p194#myapp/bin/ruby_noexec_wrapper:14:in `<main>'
The errors being shown are from this deployment rake task:
namespace :deploy do
desc "deploys to Production after uploading assets to S3"
task :production do
puts `git push heroku master`
puts `heroku run rake db:migrate --app myapp`
puts `heroku restart --app myapp`
end
desc "deploys to Staging after uploading assets to S3"
task :staging do
puts `git push staging staging:master`
puts `heroku run rake db:migrate --app myapp-staging`
puts `heroku restart --app myapp-staging`
end
end
The two heroku commands in each task are where the problem comes from. If I execute them from the command line, they work just fine though. I doubt it matters but I'm running OSX with RVM as well.
Any idea how to fix this? I realize it's not a huge deal since I can just run those additional command manually or alias them, but I'd just like to know why there's a problem in the first place.
The gem's version of the heroku command probably has a higher priority in your PATH than the system's.
I was able to reproduce this in a test project.
When I run which heroku, I see that the shell's choice is /home/justinf/.rvm/gems/ruby-1.9.3-p286/bin/heroku.
All it takes is a simple gem uninstall heroku, answering yes to deleting the executable.
which heroku now gives me /usr/bin/heroku, and my test.rb now completes with no error instead of crashing out with a bundler exception.

Resources