Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Use `Rails.env` instead of `ENV['RAILS_ENV']` #7951

Merged
merged 1 commit into from

4 participants

@Grandrath

ENV['RAILS_ENV'] is not defined unless explicitly specified on the
command line when running rake db:structure:load. This patch lets
the rake task retrieve the environment from Rails.env which defaults
to "development".

@carlosantoniodasilva

I think the idea is to default to test in case RAILS_ENV is not set. This way we'd be defaulting to development, which changes the functionality, making the || 'test' clause "useless". I don't think we're supposed to change that, though.

@rafaelfranca

Agree with @carlosantoniodasilva. Thank you for the pull request.

@Grandrath

Are you sure this behavior is desired? rake db:schema:load loads the db schema into the development database without the need to specify RAILS_ENV=development on the command line. rake db:structure:load is supposed to behave alike.

Furthermore rake db:structure:load did access the development database in version 3.2.6. That changed somewhere on the road between 3.2.6 and 3.2.8. Can you confirm that this change in behavior is by design?

@rafaelfranca rafaelfranca reopened this
@rafaelfranca

@Grandrath you are right. So the best thing to do is remove the arguments of current_config and it will use the Rails.env

@Grandrath

OK, I updated the pull request accordingly.

@rafaelfranca

@Grandrath great! We don't have tests for this code, so could I ask to you test it in a blank application?

Also these are the changes between Rails 3.2.8 and 3.2.6 v3.2.6...v3.2.8#diff-61

@carlosantoniodasilva

@Grandrath this is new info to the pull request :). So yeah, if something has changed and the code is now not behaving as it was before, I agree with you, thanks for spotting this!

@robotarmy

I am wondering what happening in this issue?

I was going to file a bug report for the fact that 'rake' or 'rake default' with rspec-rails is no longer setting the rails environment into 'test'.

I'm a 0.5.0 rails user - and as near as i can rememeber - i could always rely on 'rake' running the test harness in the RAILS_ENV test environment.

Is this intentionally changing?

Thank you so much for your hard work on this great open source product!

versions

rspec-rails (2.11.0)

rails (3.2.7)
actionmailer (= 3.2.7)
actionpack (= 3.2.7)
activerecord (= 3.2.7)
activeresource (= 3.2.7)
activesupport (= 3.2.7)
bundler (~> 1.0)
railties (= 3.2.7)

@rafaelfranca

@robotarmy please use the Rails 3.2.8 version. The 3.2.7 has this bug.

@robotarmy
@Grandrath

@rafaelfranca OK, I'll test the change in a blank app. But I guess I don't find the time to look into this today.

@carlosantoniodasilva

@Grandrath also make sure to squash your commits, thanks!

@robotarmy

@rafaelfranca 3.2.8 worked :dancer:

@rafaelfranca

@robotarmy thank you to share it back

@rafaelfranca

@Grandrath just ping us if everything works fine and we will merge.

@Grandrath Grandrath referenced this pull request from a commit in Grandrath/rails
@Grandrath Grandrath Use `Rails.env` instead of `ENV['RAILS_ENV']`; #7951
`ENV['RAILS_ENV']` is not defined unless explicitly specified on the
command line when running `rake db:structure:load`. This patch lets
the rake task retrieve the environment from `Rails.env` which defaults
to "development".
f4ddc49
@Grandrath Grandrath Don't use test environment by default when running rake db:structure:…
…load; #7951

`ENV['RAILS_ENV']` is not defined unless explicitly specified on the
command line when running `rake db:structure:load`.
5042eda
@Grandrath

I added rails/rails#7986 which solves this issue for 3.2.8 (tested).

As for this pull request: I have trouble testing this in a blank app from master. When I try to run rake db:structure:load I get the error message

rake aborted!
undefined method `whitelist_attributes=' for ActiveRecord::Base:Class

Can someone with more experience in Rails development help me, please?

@carlosantoniodasilva

@Grandrath you shouldn't have this option anymore when generating a Rails 4 application, do you have a stack trace?

@Grandrath

@carlosantoniodasilva Thanks. It works now after running rake rails:update.

I can now confirm that this patch does what it should. Thank you all for your fast and kind responses!

@carlosantoniodasilva carlosantoniodasilva merged commit c869ce0 into rails:master
@carlosantoniodasilva

@Grandrath great, thanks for verifying everything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 18, 2012
  1. @Grandrath

    Don't use test environment by default when running rake db:structure:…

    Grandrath authored
    …load; #7951
    
    `ENV['RAILS_ENV']` is not defined unless explicitly specified on the
    command line when running `rake db:structure:load`.
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  activerecord/lib/active_record/railties/databases.rake
View
2  activerecord/lib/active_record/railties/databases.rake
@@ -307,7 +307,7 @@ db_namespace = namespace :db do
# desc "Recreate the databases from the structure.sql file"
task :load => [:environment, :load_config] do
- current_config = ActiveRecord::Tasks::DatabaseTasks.current_config(:env => (ENV['RAILS_ENV'] || 'test'))
+ current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, "db", "structure.sql")
case current_config['adapter']
when /mysql/, /postgresql/, /sqlite/
Something went wrong with that request. Please try again.