【问题标题】:Why do I need to explicitly run Rails migrations under Ubuntu 12.04 Precise?为什么我需要在 Ubuntu 12.04 Precise 下显式运行 Rails 迁移?
【发布时间】:2016-10-28 16:57:36
【问题描述】:

我正在构建一个 Rails 5 应用程序,并且正在使用 Rspec 进行测试。我过去曾多次将 Travis 用于 Rails 应用程序,但他们使用的是 MiniTest 和 Rails 4。

我可以在本地运行测试:

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

但是当 Travis 运行测试时,它失败了:

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

我的 Travis 配置:

language: ruby
cache: bundler

我知道显而易见的解决方案是在 Travis 配置中添加一个 before 脚本以运行 rails db:migrate RAILS_ENV=test。但是,我的问题是,为什么在我以前从未需要这样做的情况下突然需要这样做?

Rails 5 的 Rspec 实现是否发生了一些变化,这意味着迁移不会在测试之前自动运行? (我假设这就是过去发生的事情......)

我应该补充一点,spec/rails_helper.rb 包含以下行:

# 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!

...所以看起来它应该正在运行迁移。

2016 年 3 月 11 日更新

在 Travis 的帮助下,我设法在 Travis 的基础架构之外重现了这个问题。

Travis 在其基于容器的基础架构上运行构建,其映像 (quay.io/travisci/travis-ruby) 基于 Ubuntu 12.04.5 (Precise)。

我现在可以像这样重新创建问题:

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!'

如果我在 macOS 10.12 或 Debian Jessie 本地执行相同的操作,则测试通过,并且不会要求我运行手动迁移。

为什么我需要在 Ubuntu 12.04 下运行手动迁移,而不是在任何其他操作系统下运行?

【问题讨论】:

    标签: ruby-on-rails rspec travis-ci rspec-rails ruby-on-rails-5


    【解决方案1】:

    所以我能够解决这个问题,这是 ActiveRecord 与 SQLite gem 接口的方式中的一个错误,基本上发生的事情是 ActiveRecord 正在向 SQLite 提交语句以记录完成的迁移,而 SQLite 只存储了一些已完成的迁移。

    此问题仅在使用 SQLite v3.7.9(不支持多插入语句)时出现,这是可用于 Ubuntu 12.04 Precise 的最新版本的 SQLite。这解释了为什么我只在 Travis 中运行测试时才遇到这个问题,因为这是他们在那里使用的默认发行版和版本。

    我提出了一个 GitHub 问题 here,并在 fd87169 中提供了修复。

    如果您遇到同样的问题,为了利用此修复程序,您需要使用包含它的 Rails 版本,并且在撰写本文时没有包含它的版本,所以我刚刚从 master 运行在我的 Gemfile 中包含以下内容:

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

    这应该包含在 Rails 的未来版本中,但我们只需要等待......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      相关资源
      最近更新 更多