在运行规范条目时从模式迁移表中删除

Posted

技术标签:

【中文标题】在运行规范条目时从模式迁移表中删除【英文标题】:While running specs entries are getting delete from schema migrations table 【发布时间】:2021-12-06 03:05:40 【问题描述】:

升级到 rails 6.1 后,我的规范失败,因为条目正在从 schema_migrations 表中删除

ActiveRecord::SchemaMigration.count
   (2.1ms)  SELECT COUNT(*) FROM "SCHEMA_MIGRATIONS"
 => 1


ActiveRecord::NoEnvironmentInSchemaError:

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

        bin/rails db:environment:set RAILS_ENV=test

Failure/Error: ActiveRecord::Migration.maintain_test_schema!

ActiveRecord::PendingMigrationError:


  Migrations are pending. To resolve this issue, run:

          bin/rails db:migrate RAILS_ENV=test

当我运行以下命令时

    bin/rails db:environment:set RAILS_ENV=test

它在 schema_migrations 表中添加条目。

但是当我跑步时

rspec spec/ 

它从 schema_migrations 表中删除了我的所有条目,除了 1 个条目。我怀疑问题出在数据库清洁器中。另外,我检查了几个post,但到目前为止还没有运气

rails_helper.rb

require 'simplecov'
SimpleCov.start 'rails'
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'json_matchers/rspec'
# Add additional requires below this line. Rails is not loaded until this point!

require 'database_cleaner'

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

#
Dir[Rails.root.join('spec/support/**/*.rb')].each  |f| require f 

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#::Rails.root/spec/fixtures"

  config.use_transactional_fixtures = true

  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")
  config.include FactoryBot::Syntax::Methods

  config.before(:suite) do
    DatabaseCleaner.clean_with :truncation, expect: %w(ar_internal_metadata schema_migrations)
    DatabaseCleaner.strategy = :transaction
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end

  config.include RequestSpecHelper
  config.include ControllerSpecHelper
end

database_cleaner (2.0.1) 导轨 (6.1) 红宝石 (2.5.0) activerecord-oracle_enhanced-adapter (6.1.4) ruby-oci8 (2.2.6.1)

注意:我使用 oracle 作为数据库

【问题讨论】:

您是否运行了建议的迁移命令(它确实显示“迁移正在等待”)?如果不是这样,您是否尝试过为您的测试环境显式重置数据库 (rails db:reset RAILS_ENV=test)? 【参考方案1】:

为了解决这个问题,我删除了这一行

ActiveRecord::Migration.maintain_test_schema!

【讨论】:

以上是关于在运行规范条目时从模式迁移表中删除的主要内容,如果未能解决你的问题,请参考以下文章

更新 Firebase 时从 ListView 中删除旧条目

schema_migrations 表上未显示应用的迁移条目

如果迁移文件不存在但迁移表中的条目可用,我如何解决迁移问题

从表中删除所有数据,但最后 N 个条目

删除不同表中的条目 MySQL 查询

Spring:如何从联结表中删除条目?