71

After upgrading a Rails app to Rails 5, running RSpec tests gives me the following error:

rails aborted!
ActiveRecord::NoEnvironmentInSchemaError: 

Environment data not found in the schema. To resolve this issue, run: 

    bin/rails db:environment:set RAILS_ENV=test

However, that bin does not exist and I can't seem to generate it with bundle binstubs rails or with rake rails:update:bin.

I have also tried:

rails db:environment:set RAILS_ENV=test
rake db:environment:set RAILS_ENV=test

There is a related issue on Github here.

How can I address this error?

steel
  • 11,883
  • 7
  • 72
  • 109
  • 2
    Generally: this is a terrible, over-engineered, over-complicated change. `env RAILS_ENV={{whatever}} bin/rake db:migrate spec` used to work just fine –  Nov 02 '16 at 04:16

7 Answers7

89

New Rails 5 command to generate binstubs:

rails app:update:bin

Allows me to run the solution as the error suggested:

bin/rails db:environment:set RAILS_ENV=test

Tip from @max comment: If you are using database_cleaner and this error keeps popping up then change your config to:

DatabaseCleaner.clean_with(
  :truncation,
  except: %w(ar_internal_metadata)
)
steel
  • 11,883
  • 7
  • 72
  • 109
  • 46
    If you are using `database_cleaner` and this error keeps popping up then change your config to `DatabaseCleaner.clean_with :truncation, except: %w(ar_internal_metadata)` - https://github.com/DatabaseCleaner/database_cleaner/issues/445 – max Oct 30 '16 at 19:53
  • 1
    omg, the db_cleaner solution works! :P didn't know the env was set in that table. – Frexuz Mar 06 '18 at 15:46
  • What file does this code go for minitest? Error still ocurring with code in test/test_helper.rb – rigyt Jun 06 '18 at 22:03
  • 2
    `database_cleaner >= 1.6.2` must [have](https://github.com/DatabaseCleaner/database_cleaner/issues/445) [fixed](https://github.com/DatabaseCleaner/database_cleaner/pull/487) the issue. Do note that if you're running CI tests on a server, all projects that are build there should have `database_cleaner >= 1.6.2`. – x-yuri Jul 16 '18 at 13:59
  • 1
    That `%w(ar_internal_metadata)` workaround did not work for me. (Rails 6, cucumber-rails 2.0.0, mysql). Any other ideas? – Grant Birchmeier Nov 18 '19 at 21:06
  • 1
    ^^ and database_cleaner 1.7.0 (so it's def not fixed in 1.6.2) – Grant Birchmeier Nov 18 '19 at 21:16
  • It appears this was fixed as of [1.8.2](https://github.com/DatabaseCleaner/database_cleaner/blob/master/History.rdoc#182-2020-02-01-). Running a test suite with repeat has yet to bring the message back up after: 1. Updating the gem 2. Resetting my environment data – Nathan L. Walls Jul 21 '20 at 20:28
12

For me, this error was followed by a similar one asking for a migration.

This did the trick: rails db:migrate RAILS_ENV=test

Mirror318
  • 11,875
  • 14
  • 64
  • 106
12

All of the above answers are correct, however, if you're in a more unique project such as developing a rails engine with a concept of a schema (hacky, I know) and your migration fails for some reason, you can re-run it without the check that throws this exception. Example:

rake environment db:{drop,create,migrate} DISABLE_DATABASE_ENVIRONMENT_CHECK=1

Cole Jurden
  • 131
  • 1
  • 7
8

fix for jenkins before you drop database you should execute db:environment:set with || true, so the command doesn't abort:

bin/rails db:environment:set RAILS_ENV=test || true
Exsemt
  • 1,048
  • 10
  • 22
5

For me, I had to do a mixture of things:

bin/rails db:environment:set RAILS_ENV=test
bin/rails db:migrate RAILS_ENV=test

This would make things work, and then I had to review my migrations, I was adding a null:false into a relationship and that added a bug, the migration was cancelled and didn't finish

Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
1

I had to drop my database for tests and create it again:

  1. psql
  2. DROP DATABASE your_db_name_test;
  3. bundle exec rake db:create RAILS_ENV=test

After that the warning bin/rails db:environment:set RAILS_ENV=test disappeared.

Evmorov
  • 1,134
  • 22
  • 28
1

If you happen to see this error while using parallel_tests gem then you need to run below command for each DB. Just increase TEST_ENV_NUMBER.

TEST_ENV_NUMBER=1 bin/rails db:environment:set RAILS_ENV=test
TEST_ENV_NUMBER=2 bin/rails db:environment:set RAILS_ENV=test

This helped me fix the problem when I was testing parallel_tests with knapsack_pro gem https://github.com/KnapsackPro/knapsack_pro-ruby#parallel_tests-with-knapsack_pro-on-single-ci-machine

Artur Trzop
  • 336
  • 3
  • 12