How do you set the RAILS_ENV variable when using RVM? - ruby-on-rails

I have a rake task in my rails app that has a different absolute filepath compared between prod and dev. I have a different database username/pass that differs between prod and dev. I can't seem to get the RAILS_ENV variable to be set within RVM in order to tell each server what their role is. Is there a way to do this?
My workaround has been to do the following for all rake tasks:
RAILS_ENV=development rake routes
RAILS_ENV=production rake db:migrate

Been having similar problems (not with RVM though). One way we did it was to assign ENV["RAILS_ENV"] through envrionment.rb
It's not the actual answer, but it will give you some ideas:
#config/environment.rb
ENV["RAILS_ENV"] = "staging"
You may wish to look at assigning environment vars in RVM here: http://matthew.mceachen.us/blog/howto-make-system-wide-rvm-installations-work-with-cron-monit-delayed_job-and-passenger-1021.html

Related

Rspec keeps using development database

I have different databases for development and for test in database.yml.
I have ENV['RAILS_ENV'] = 'test' in start of both files rails_helper.rb and spec_helper.rb, generated with rspec initialisation command.
Also, I tried Rails.env = 'test' without success.
I run rspec as RAILS_ENV=test rake spec
I don't have ENV['RAILS_ENV'] setting or Rails.env setting anywhere in initialisation scripts.
However, Rspec keeps using development database instead of testing one.
I have checked a lot of similar questions around the internet and no answer helped.
Any solutions?
What version of rails are you using? Try rake db:test:prepare. In Rails 4+, It's deprecated but still works. It seems like in rails 4.1+, migrations are automatically checked for, and running db:schema:load will do the trick.

Rake not using the same environment value as phusion passenger setting

In my vhost, I have:
<Directory /var/www/prod/myapp/myapp/public>
...
RailsEnv production
...
</Directory>
and while any code dependent on it being production is correctly running there in the app itself (ex: display the Google Analytics code if Rails.env.production?), when I run rake about from /var/www/prod/myapp/myapp, I get:
Application root /var/www/prod/myapp/myapp
Environment development
Database adapter mysql2
which means that I have to prefix any deployment related rake stuff with RAILS_ENV=production. Granted, it's all in a deployment script at this point so it doesn't matter much, but why isn't Rake aware that it's production? Shouldn't the Passenger setting be enough? And if not, how do I fix it so I won't need to specify the environment manually?
Side-note: I am running the development instance of the app on the same box, with Environment set to development in its corresponding vhost configuration.
EDIT: Phusion Passenger version 4.0.20
rake is entirely unaware of passenger's configuration. It doesn't even know that you're using passenger. Since it isn't launched by passenger, it would have to (assuming it knew you were using apache/passenger) parse the apache config files to find this out, which would get pretty complicated, especially in the presence of multiple apps.
You could set this in one of your shell's startup files, however that doesn't sound like a good idea if you have multiple environments on the same machine.
You could stick
ENV['RAILS_ENV']='production'
At the top of one of Rails' startup files - boot.rb seems to do the trick. This would make passenger's setting ineffective though, and obviously you would only want to do this on the production deploy of your script.
Personally (especially on a machine with multiple environments in action) I'd stick to typing RAILS_ENV=production.
but why isn't Rake aware that it's production?
Because rake and all rails related commands internally do this
ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
which means that if you didnt have RAILS_ENV or RACK_ENV environment, then it will use development environment by default.
Shouldn't the Passenger setting be enough?
NO. Passenger setting is just for making your app up/live in desire or say virtual environment. It doesn't change any system configuration.
And if not, how do I fix it so I won't need to specify the environment manually?
That's very easy. Just set environment variable. If you are using bash (normally people do) you can simple do this
echo "export RAILS_ENV=production" >> ~/.bashrc ; source ~/.bashrc
This will make your server as rails production server, so if you run rails c or rake db:migrate etc or any rails or rake command it will run in production server for all available applications.
I am running the development instance of the app on the same box, with Environment set to development in its corresponding vhost configuration.
This is a problem. If you set the above environment variable, then whenever you run any code like rake db:migrate inside this app ..that will run in production environment. To avoid this either you dont set environment variable or specify environment on every command. There is one hack available . Include this line at the top of config/boot.rb
ENV["RACK_ENV"] = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
Future reading : How can I make a custom environment in rails a default environment?

Rails Setting up unique integration environment

I am trying to setup a unique environment (but effectively a new development environment so that various global parameters can be different). I've followed lots of examples to create a new environment (I used my development config as the starting point).
My new environment is singleserverintegration.
a new environment.rb
added new logic to initializers/additional.rb (elsif Rails.env.eql?("singleserverintegration"))
added entries to database.yml
BUT when-ever i attempt to setup the environment
RAILS_ENV="singleserverintegration" && rake db:drop && rake db:create && rake db:migrate
I get a
rake aborted!
uninitialized constant Capybara
why is it pulling out test configuration (which is where capybara is used as part of rspec) [i've noticed that additional.rb has capybara config reguardless of the environment, but it never complains when i run rake / db commands for my dev environment. why would it complain now?]
what am i missing - guidance appreciated for a relative newbie...
thanks
Ben
bottom line of additional.rb is
Capybara.server_port = 8066
but, this does not cause an issue when i setup development environments!?
try adding your new env to the capybara gem as well
Ex:
group :test, :development, :singleserverintegration do
gem 'capybara'
end

How to write ruby script that uses the production environment instead of development?

I have writen a ruby script that requires config/environment.rb so I can use all the models of my rails application in the script but the problem is that I want to use the production environment instead of the develoment environment which seems to be the default behavior.
Im using Rails 3.1.1 and Ruby 1.9.2
How can I run the script with the production environment?
Your script will take the environment variable RAILS_ENV as the environment to use.
I'd be very wary of overriding that in the script as it may cause much confusion if you try and run your script in another environment - e.g. staging - and it starts trying to access production databases etc.
So do either:
RAILS_ENV=production ./script/my-awesome-script
or
export RAILS_ENV=production
./script/my-awesome-script
Generally speaking; when I log into a production Rails environment I'd be changing the environment straight away if I haven't configured it to be "production" by default.
I think you want either Rails.env = 'production' or ENV['RAILS_ENV'] = 'production' in your script.
#davidb, I am not sure what u want also not as good on rails yet but may be you can use the seed.rb (i.e seed functionality) for achieving what you want if this script is run only once or sometimes as we can specify the environment while running seed
rake db:seed RAILS_ENV=production

Change Ruby Environment in Hudson

I'm trying to point the Hudson app at the test environment in my MySQL database with Rails. Do I need to change the environment variable RUBY_ENV? If so, where do I implement this?
I believe you're referring to RAILS_ENV variable? If you're writing the commands to hudson then something like this might work:
$ rake test RAILS_ENV=test
Although rake test implicitly uses the test environment. If it's needed for other commands just pass RAILS_ENV=test to them.

Resources