rails: `rails db:drop:all db:create:all db:migrate` isn't creating tables after adding InternalMetadata/EnvironmentMismatchError

Right now rails db:drop:all db:create:all db:migrate leaves DB without any tables, before adding internal metadata this command resulted in migrated DB. Unfortunately I can’t bisect the issue because in many commits dropping/creating/migration in a single run is broken, but AFAICS the issue emerged after adding InternalMetadata/EnvironmentMismatchError.

The first bad commit could be any of: d70c68d76abcbc24ef0e56b7a7b580d0013255dd f6628adc11e2e57db75030fca9bae035be5cd95b c1a1595740b243bed02f5e59090cc58dac77bbf3 de2cb20117af68cef4126d8998cc63e178c58187 302e92359cc88258ae15a82454c58408a4b8157e a76c4233a9ee9ffbf413c4b8353e73e8ffbeb3a5 350ae6cdc1ea83e21c23abd10e7e99c9a0bbdbd2 ab40d71ff88f99ee4219747fcc8774514ec50ef8 7b065f623f2a56d74eca88ed27970c71a76f0a17 921997675258147fdc2bbbca0da4fb0c771c218d 30391e9ddba745d6bdc0b23f526ecf432dfe6adf 900bfd94a9c3c45484d88aa69071b7a52c5b04b4 34ac8a1e98e85b28fe3ee02a83c85cdaa4b77714 We cannot bisect more!

cc @schneems

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 40 (26 by maintainers)

Most upvoted comments

I found a solution for myself. Here we go:

I was encountering ActiveRecord::NoEnvironmentInSchemaError with pg and started inspecting my databases.

The key difference I found was my ar_internal_metadata had the column key set as integer on DBs having issues and DBs not having issues key was set to varchar and had a value of environment

Also my schema which was loading was doing this:

create_table "ar_internal_metadata", primary_key: "key", force: :cascade do |t|
  t.string   "value"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

The schema should be like:

create_table "ar_internal_metadata", primary_key: "key", id: :string, force: :cascade do |t|
  t.string   "value"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

Now everything works. Hope this helps others. I’m guessing it got screwed up some how in the rails4 to rail5 migration.

app:update task (that should be run after upgrade, according to the guide) deals with it. AFAIU it is intended behavior.

@andrewyoo that schema difference was the cause of my issue as well. I deleted that table manually and re-ran rails db:migrate and it resolved itself. Sounds like this issue might need to re-open?

@Schwad I came here by googling. My problem may not be the same but seems related. And I have a super-simple reproduction:

git clone git@github.com:publify/publify_amazon_sidebar.git
cd publify_amazon_sidebar
bundle exec rake

I get the following output:

/home/matijs/.rbenv/versions/2.3.1/bin/ruby -I/home/matijs/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib:/home/matijs/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-support-3.5.0/lib /home/matijs/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
rails aborted!
ActiveRecord::EnvironmentMismatchError: You are attempting to modify a database that was last run in `development` environment.
You are running in `test` environment. If you are sure you want to continue, first set the environment using:

	bin/rails db:environment:set RAILS_ENV=test

bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:test:load => db:test:purge => db:check_protected_environments
(See full trace by running task with --trace)
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

[snip rest of successful RSpec run output]

You’ll get that error if you’re trying to run that command on a non-empty database that was upgraded from Rails 4.2. To resolve it follow directions, run that command. That error is expected, there’s no way around it, running it is part of the upgrade process.

I’m running off of a rails new app with Rails 5 pointing at master in the Gemfile.

This command also works:

$ rails db:drop db:create db:migrate --trace

I’m thinking there’s some strange interaction with the :all invocations.

It will be helpful if someone will write how you’ve got wrong column type in schema.

I strangely got this error when checking out a pre Rails 5 commit in our repo, loading schema from that commit and then going back to HEAD commit with Rails 5.

Ryans-MacBook-Pro:matic-home ryanquant$ bin/rails db:reset
I, [2016-11-28T08:55:41.209693 #15269]  INFO -- : Celluloid 0.17.3 is running in BACKPORTED mode. [ http://git.io/vJf3J ]
rails aborted!
ActiveRecord::NoEnvironmentInSchemaError: 

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

        bin/rails db:environment:set RAILS_ENV=development

Any db rake/rails command I tried to run would cause the error to be thrown (even after running the environment setup command recommended in the error).

Strangely, when the one exception is when I ran bin/rails db:migrate. The normal behavior resumed and the errors were no more.