Test Migrations are not running with my Rails Engine, even though `maintain_test_schema!` is specified
Asked Answered
T

3

9

I'm developing a Rails Engine (Plugin). So far I have

  • Set up RSpec as a framework
  • Added a migration to create a model/table called MyJob
  • Added some basic model tests under spec/models/my_job_spec.rb

The rails template automatically creates a spec/dummy app, so I did the following to run my test

# Create the development and test DBs
rake db:create

# Copy migrations over to my dummy app
cd spec/dummy
rake my_app:install:migrations
cd ../..

# Run specs
rspec spec/models/my_job_spec.rb

However when I run my specs, I get an error:

> rspec spec/models/
/Users/jeeves/.rvm/gems/ruby-2.2.2@gb/gems/activerecord-5.1.0/lib/active_record/migration.rb:576:in `check_pending!':  (ActiveRecord::PendingMigrationError)

Migrations are pending. To resolve this issue, run:

        bin/rails db:migrate RAILS_ENV=test

I thought this automatically would happen because in my rails_helper.rb I definitely have the following, which is supposed to maintain my test schema for me

ActiveRecord::Migration.maintain_test_schema!

Does it work differently with plugins or engines?

EDIT: I tried the recommendation of running bin/rails db:migrate RAILS_ENV=test, inside spec/dummy/ and then re-running rspec spec/. Still no luck.

Timothy answered 29/5, 2017 at 0:19 Comment(2)
ActiveRecord::Migration.maintain_test_schema! was introduced in Rails 4.1 and still does not really work as advertised. I usually have to run rake db:test:load after migrations to get it to properly update the test schema.Chaparajos
@Chaparajos - Thanks! That did the trick for me. Left a more elaborate self-answer below, but appreciate the tip off.Timothy
T
5

Ok found a few things I was doing wrong for anyone else who stumbles across this.

Firstly, I don't need to run rake my_app:installations:migrations while testing or developing. Apparently that's intended only for the downstream users of my app when they want to copy my engine's migrations into their host app. The instructions in various blogs and such mention that in retrospect, but I still think it's easy to misinterpret if it's your first time trying to figure it out.

Secondly, all commands are just to be executed from your engine's root and rails ensures that they are applied correctly. So it's just a matter of running rake db:migrate from your engine root and the dummy application will be migrated.

Thirdly, I stopped using ActiveRecord::Migration.maintain_test_schema!. As the user above noted, it doesn't really work as advertised. rake db:migrate RAILS_ENV=test correctly migrated my test db but because of the way the nested dummy app is set up that the maintain_test_schema! method things nothing has been migrated. It's kind of annoying to not have it automatically migrated in both environments, but I'll take it.

So the whole process, quite simple in the end, boils down to:

# Create the development and test DBs
rake db:create

# Migrate
rake db:migrate
rake db:migrate RAILS_ENV=test

# Run specs
rspec spec/models/my_job_spec.rb

Again, seems simple in retrospect but learned a few things on the way.

Timothy answered 29/5, 2017 at 1:34 Comment(0)
T
1

I faced similar phenomenon on my engine with MiniTest (not rspec though), which was developed at rails 3, happened when migrating it to rails 4.2.

What I do is that:

  1. I generate new engine under some work directory(e.g. /tmp).
  2. copy newly created test/test_helper.rb to my current engine.
Tyrant answered 24/8, 2019 at 15:15 Comment(0)
S
0

remove ActiveRecord::Migration.maintain_test_schema!

replaced with ActiveRecord::Migrator.migrate(Rails.root.join('db/migrate'))

Sauterne answered 14/11, 2019 at 14:21 Comment(1)
This (no longer?) works. You can achieve the same thing by putting ActiveRecord::Migrator.migrations_paths = [Rails.root.join("db/migrate").to_s] before the maintain_test_schema! line.Graceless

© 2022 - 2024 — McMap. All rights reserved.