ruby 运行规范时,将从架构迁移表中删除条目

gblwokeq  于 2023-03-17  发布在  Ruby
关注(0)|答案(2)|浏览(118)

升级到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,但到目前为止没有运气
轨道_助手.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

数据库清理程序(2.0.1)
轨道(6.1)
Ruby(2.5.0)
活动记录-oracle_增强-适配器(6.1.4)
Ruby-氯化钠(2.2.6.1)
注意:我使用oracle作为数据库

ar7v8xwq

ar7v8xwq1#

也有同样的问题。
schema_migrations表已清理,因为其中一个迁移文件包含db/schema.rb不支持的某些高级功能
解决办法是开始使用sql格式
1.在application.rb中添加config.active_record.schema_format = :sql
1.运行dev服务器,rails将为您创建db/structure.sql
1.删除schema.rb并立即运行测试。

hivapdat

hivapdat2#

为了解决此问题,我删除了此行

ActiveRecord::Migration.maintain_test_schema!

相关问题