testdouble/good-migrations

Error not raised during migration that references AR model

Closed this issue · 5 comments

Hi. I just installed this gem, but it doesn't seem to be raising an error when I believe it should be. I am running a migration in my development environment, where I have config.eager_load = false. I added a new migration that references an AR model as such:

class PopulateEncryptedOtpFields < ActiveRecord::Migration
  def up
    User.reset_column_information

    User.find_each do |user|
      user.otp_secret_key = user.read_attribute('otp_secret_key')
      user.save!
    end
  end

  def down
    User.reset_column_information

    User.find_each do |user|
      user.otp_secret_key = ROTP::Base32.random_base32
      user.save!
    end
  end
end

When I run bin/rake db:migrate, the migration completes successfully. Shouldn't it fail?

To rule out any issues with Spring, I ran spring stop, then bundle exec rake db:migrate and it still succeeds.

I'm on Rails 4.2.6

To troubleshoot this, I cloned this repo, then referenced the local repo in my Gemfile, and added a puts to check the path:

namespace :good_migrations do
  task :disable_autoload do
    next if ENV['GOOD_MIGRATIONS'] == "skip"
    ActiveSupport::Dependencies.class_eval do
      extend Module.new {
        def load_file(path, const_paths = loadable_constants_for_path(path))
          puts "path: #{path}"
          if path.starts_with? File.join(Rails.application.root, 'app')
            raise GoodMigrations::LoadError, <<-ERROR
...

When I run bundle exec rake db:migrate, that line never gets called, although the rake task does get called.

bf4 commented

don't use spring. export DISABLE_SPRING=1 and Ruby behaves as expected

Yes, I agree. The number of problems caused by Spring are myriad, and there's not a lot that can be done AFAIK with respect to invalidating objects it's holding in memory. Strongly recommend working without it, especially if you have bootsnap working.