From 14e95e671353c41e447fdaa096d6abe4e691f423 Mon Sep 17 00:00:00 2001 From: Daniel Uber Date: Wed, 15 Sep 2021 09:29:44 -0500 Subject: [PATCH] Ensure ActiveRecord::Base has descendants before checking triggers (#14732) HairTrigger uses the ActiveRecord::Base.descendants to find triggers in the db (from `models`). WithModel (used in the Settings::Base spec) clears this during its descendants cleanup, causing this test to ocassionally fail. Before checking hairtrigger, if AR::Base descendents is empty, add ApplicationRecord (there's an assumption here that all of our application models inherit from application record - this may or may not be true if we use engine generated models that do not follow this convention and need to validate triggers there. --- spec/models/hair_trigger_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/models/hair_trigger_spec.rb b/spec/models/hair_trigger_spec.rb index 215ee156c..c1a4924a0 100644 --- a/spec/models/hair_trigger_spec.rb +++ b/spec/models/hair_trigger_spec.rb @@ -5,6 +5,12 @@ require "rails_helper" RSpec.describe HairTrigger, type: :model do describe ".migrations_current?" do it "is always true" do + # work-around empty AR::Base descendants array caused by with_model cleanup + # HairTrigger uses AR::Base to get database triggers (and compare against the schema) + if ActiveRecord::Base.descendants.blank? + ActiveSupport::DescendantsTracker.store_inherited(ActiveRecord::Base, ApplicationRecord) + end + expect(described_class.migrations_current?).to be(true) end end