2

I'm building a Rails 5 app and I'm using Rspec for tests. I've used Travis several times in the past for Rails apps, but they were using MiniTest and Rails 4.

I can run tests locally:

joenyland@MacBook-Pro ~/Code/acrm $ rake db:drop
Dropped database 'db/development.sqlite3'
Database 'db/test.sqlite3' does not exist
joenyland@MacBook-Pro ~/Code/acrm $ RAILS_ENV=test bundle exec rake
/Users/joenyland/.rvm/rubies/ruby-2.3.0/bin/ruby -I/Users/joenyland/.rvm/gems/ruby-2.3.0@acrm/gems/rspec-core-3.5.4/lib:/Users/joenyland/.rvm/gems/ruby-2.3.0@acrm/gems/rspec-support-3.5.0/lib /Users/joenyland/.rvm/gems/ruby-2.3.0@acrm/gems/rspec-core-3.5.4/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
*.............................

Pending: (Failures listed here are expected and do not affect your suite's status)

  1) SessionsHelper add some examples to (or delete) /Users/joenyland/Code/acrm/spec/helpers/sessions_helper_spec.rb
     # Not yet implemented
     # ./spec/helpers/sessions_helper_spec.rb:14


Finished in 1.87 seconds (files took 15.63 seconds to load)
30 examples, 0 failures, 1 pending

But when Travis runs the tests, it fails:

0K$ bundle exec rake
/home/travis/.rvm/rubies/ruby-2.3.0/bin/ruby -I/home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib:/home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-support-3.5.0/lib /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
/home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:572:in `check_pending!':  (ActiveRecord::PendingMigrationError)

Migrations are pending. To resolve this issue, run:

    bin/rails db:migrate RAILS_ENV=test

    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:585:in `load_schema_if_pending!'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:591:in `block in maintain_test_schema!'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:822:in `suppress_messages'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:596:in `method_missing'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:591:in `maintain_test_schema!'
    from /home/travis/build/MasterRoot24/acrm/spec/rails_helper.rb:27:in `<top (required)>'
    from /home/travis/build/MasterRoot24/acrm/spec/models/client_spec.rb:1:in `require'
    from /home/travis/build/MasterRoot24/acrm/spec/models/client_spec.rb:1:in `<top (required)>'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `load'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:100:in `setup'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:86:in `run'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:71:in `run'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:45:in `invoke'
    from /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/exe/rspec:4:in `<main>'
/home/travis/.rvm/rubies/ruby-2.3.0/bin/ruby -I/home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib:/home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-support-3.5.0/lib /home/travis/build/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb failed

My Travis config:

language: ruby
cache: bundler

I know the obvious solution is to add a before script to the Travis config to run rails db:migrate RAILS_ENV=test. However, my question is why is this suddenly required when I've never needed to do it before?

Has something changed in the Rspec implementation for Rails 5 that means migrations aren't run before tests automatically now? (I assume this is what used to happen...)

I should add that spec/rails_helper.rb contains the lines:

# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

...so it looks like it should be running the migrations.

UPDATE 3/11/2016

With a little help from Travis, I've managed to recreate this issue outside of Travis' infrastructure.

Travis runs builds on their container based infrastructure and their image (quay.io/travisci/travis-ruby) is based on Ubuntu 12.04.5 (Precise).

I can now recreate the issue like so:

docker run -it ubuntu:precise /bin/bash

apt-get update && apt-get install git bash-completion curl nodejs -y && gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 && \curl -sSL https://get.rvm.io | bash -s stable && source /etc/profile.d/rvm.sh && rvm install 2.3.0 && git clone --depth=50 --branch=master https://github.com/MasterRoot24/acrm.git MasterRoot24/acrm && cd MasterRoot24/acrm && rvm use 2.3.0 && gem install bundler && export BUNDLE_GEMFILE=$PWD/Gemfile && bundle install --jobs=3 --retry=3 --deployment --path=${BUNDLE_PATH:-vendor/bundle} && bundle exec rake

root@e4f741e8c189:/MasterRoot24/acrm# bundle exec rake
/usr/local/rvm/rubies/ruby-2.3.0/bin/ruby -I/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib:/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-support-3.5.0/lib /MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
/MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:572:in `check_pending!':  (ActiveRecord::PendingMigrationError)

Migrations are pending. To resolve this issue, run:

    bin/rails db:migrate RAILS_ENV=test

    from /MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:585:in `load_schema_if_pending!'
    from /MasterRoot24/acrm/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/migration.rb:591:in `block in maintain_test_schema!'

If I do the same locally on macOS 10.12 or in Debian Jessie, tests pass and I am not asked to run a manual migration.

Why would I need to run a manual migration under Ubuntu 12.04, but not under any other OS?

JoeNyland
  • 280
  • 2
  • 22

1 Answers1

1

So I was able to work this out to being a bug in the way that ActiveRecord interfaces with the SQLite gem and essentially what was happening was that ActiveRecord was submitting statements to SQLite to record the completed migrations and SQLite was only storing some of the completed migrations.

This issue only surfaces when SQLite v3.7.9 is used (which doesn't support multi-insert statements) which is the latest version of SQLite available for Ubuntu 12.04 Precise. This explains why I only came across the issue when I was running tests in Travis, as that's the default distro and version they use there.

I raised a GitHub issue here and a fix was provided in fd87169.

In order to utilise this fix if you have the same problem, you need to use a version of Rails that includes it and at the time of writing there's no release that includes it so I've just been running from master with the following in my Gemfile:

gem 'rails', github: 'rails/rails'

This should be included in a future release of Rails though, but we've just got to wait...

JoeNyland
  • 280
  • 2
  • 22