Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RAILS_ENV is not working as expected. How to set the environment?

I'm configuring a new app in rails5 on my localhost and am having some trouble establishing my environment. I set my environment variable to "development" but whenever I run rake db:drop it says the following

ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database.
If you are sure you want to continue, run the same command with the environment variable:
DISABLE_DATABASE_ENVIRONMENT_CHECK=1

However my environment is set to development and not production:

sam$ printenv | grep RAILS_ENV
RAILS_ENV=development

What am I forgetting?

Many thanks for taking a look at this.

EDIT: My yaml looks like this

default: &default
  adapter: mysql2
  encoding: utf8
  username: ZZZZZZZ
  database: YYYYYYY
  host: 127.0.0.1
  socket: /tmp/mysql.sock
  password: XXXXXXXXX

development:
  <<: *default
  reconnect: true
like image 410
neanderslob Avatar asked Oct 18 '25 15:10

neanderslob


2 Answers

You could set the environment using bin/rails db:environment:set RAILS_ENV=development

like image 195
Luna Lovegood Avatar answered Oct 20 '25 03:10

Luna Lovegood


In case the DB you wanted to drop was a production copy, the ar_internal_metadata would hold the value production and rails db:drop throws the error you see. Running migrate changes the value to development

rails db:migrate
...
ActiveRecord::InternalMetadata Update (0.4ms)  UPDATE "ar_internal_metadata" SET "value" = $1, "updated_at" = $2 WHERE "ar_internal_metadata"."key" = $3  [["value", "development"]
...

Now you can drop db without error. No point on importing then dropping perhaps, but I got this error now after having played around with data in local rails console and wanted to drop and restore data.

like image 25
oma Avatar answered Oct 20 '25 04:10

oma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!