Posted over 2 years ago. Visible to the public.

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

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).

Copy
$ 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

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

Copy
> 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:

Copy
RAILS_ENV=development rails db:environment:set

For parallel tests:

Copy
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.

Copy
DISABLE_DATABASE_ENVIRONMENT_CHECK=1 rails db:drop

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Arne Hartherz
Last edit:
4 days ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more