Read more

How to avoid ActiveRecord::EnvironmentMismatchError on "rails db:drop"

Arne Hartherz
August 30, 2017Software engineer at makandra GmbH

After loading a staging dump into development, you might get an ActiveRecord::EnvironmentMismatchError when trying to replace the database (like rails db:drop, rails db:schema:load).

$ rails db:drop
rails aborted!
ActiveRecord::EnvironmentMismatchError: You are attempting to modify a database that was last run in `staging` environment.
You are running in `development` environment. If you are sure you want to continue, first set the environment using:

        bin/rails db:environment:set RAILS_ENV=development

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

Starting with Rails 5, the ar_internal_metadata database table was introduced:

> SELECT * FROM ar_internal_metadata;
     key     |  value  |         created_at         |         updated_at         
-------------+---------+----------------------------+----------------------------
 environment | staging | 2017-06-01 08:30:00.123456 | 2017-06-01 08:30:00.123456

You can do what Rails suggests, and change the stored environment:

RAILS_ENV=development rails db:environment:set

For parallel tests:

rake parallel:rake[db:environment:set]

Then re-run your command.

Or, if you are absolutely certain that everything is correct, set an environment variable to run your command but disable that check.

DISABLE_DATABASE_ENVIRONMENT_CHECK=1 rails db:drop
Posted by Arne Hartherz to makandra dev (2017-08-30 19:57)