I have the ActiveRecord::Migration.maintain_test_schema! line in my rails_helper.rb, which gives the ActiveRecord::PendingMigrationError if some migrations are pending in the test environment. However when I run rails db:migrate RAILS_ENV=test after the error emerges, it tries to run all migrations from scratch which results in Relation table_name already exists error, rather than running single migrations. When I check the migration status it says Schema migrations table does not exist yet. Somehow ActiveRecord::Migration.maintain_test_schema! unloads the schema migrations table when it's supposed to load it?
If I run test migrations before I run RSpec and it hits the maintain_test_schema everything is ok. Also the above problem is solved by running rails db:migrate:reset RAILS_ENV=test but I'm really curious about what causes it in the first place.