While running specs entries are getting delete from schema migrations table

510 Views Asked by At

When upgraded to rails 6.1 my specs are failing due to entries are getting delete from schema_migrations table

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

When I run the following command

    bin/rails db:environment:set RAILS_ENV=test

It adds entry in the schema_migrations table.

But when I run

rspec spec/ 

It deleted all my entries from the schema_migrations table except 1 entry. I suspect the issue is in database cleaner. Also, I check few post but so far no luck

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)
rails (6.1)
ruby (2.5.0)
activerecord-oracle_enhanced-adapter (6.1.4)
ruby-oci8 (2.2.6.1)

Note: I am using oracle as a database

2

There are 2 best solutions below

0
Goaul On BEST ANSWER

Had the same problem.

The schema_migrations table was cleaned as one of migration files had some advanced function in it, what is not supported by db/schema.rb

Solution is to start use sql format

  1. in application.rb add config.active_record.schema_format = :sql
  2. run dev server and rails will create for you db/structure.sql
  3. delete schema.rb and run your tests now.
1
Aniket Tiwari On

To fixed this issue I have removed this line

ActiveRecord::Migration.maintain_test_schema!