fix: rescue DB errors in engine initializer during db:create/db:setup#7
Open
crossi-dev wants to merge 1 commit into
Open
fix: rescue DB errors in engine initializer during db:create/db:setup#7crossi-dev wants to merge 1 commit into
crossi-dev wants to merge 1 commit into
Conversation
When config/allgood.rb touches the database (e.g. Model.find_each to register dynamic checks), the engine's after_initialize block raises ActiveRecord::NoDatabaseError or ActiveRecord::ConnectionNotEstablished on a fresh checkout before db:create has run, breaking db:setup entirely. Wrap the config evaluation in a rescue guard for both error classes and emit a Rails.logger.warn so the skip is visible in logs. Checks register normally on the next request once the database is ready. Fixes rameerez#5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
config/allgood.rbtouches the database at the top level (e.g.Model.find_eachto register dynamic checks), the engine'safter_initializeblock raisesActiveRecord::NoDatabaseErrororActiveRecord::ConnectionNotEstablishedon a fresh checkout — beforedb:createhas run. This breaksrails db:setupentirely.Reported in #5 by @sislr.
Root cause
lib/allgood/engine.rbline 5 —config.after_initializefires duringdb:create, evaluatesconfig/allgood.rbviainstance_eval, and any database call in that file raises before the DB exists.Fix
Wrap the
instance_evalcall in arescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablishedguard and emit aRails.logger.warnso the skip is visible in logs. Checks that couldn't register at boot will register normally on the next request once the database is ready — no change to runtime behavior when the DB is available.This matches the pattern suggested in the issue by @sislr, applied at the gem level so users don't need to guard their own
config/allgood.rb.Tests
Three regression tests added to
EngineConfigFileTest:test_no_database_error_is_rescued_during_config_load— config raisingNoDatabaseErrordoes not propagate; warning is loggedtest_connection_not_established_is_rescued_during_config_load— same forConnectionNotEstablishedtest_config_loads_normally_when_database_is_available— normal config still registers checks without emitting a warningBefore:
406 runs, 9 failures (all pre-existing timezone failures), 0 errorsAfter:
406 runs, 9 failures (same pre-existing), 0 errors— 3 new tests all greenCame across allgood and noticed #5 had been open a while, so I went ahead and fixed it. No strings — glad if it helps. I do this kind of work (Choreless — we fix and ship code for small teams), so if you ever want a hand, I'm around. — Charles