Skip to content

fix: rescue DB errors in engine initializer during db:create/db:setup#7

Open
crossi-dev wants to merge 1 commit into
rameerez:mainfrom
crossi-dev:fix/engine-no-database-error
Open

fix: rescue DB errors in engine initializer during db:create/db:setup#7
crossi-dev wants to merge 1 commit into
rameerez:mainfrom
crossi-dev:fix/engine-no-database-error

Conversation

@crossi-dev

@crossi-dev crossi-dev commented Jun 15, 2026

Copy link
Copy Markdown

Problem

When config/allgood.rb touches the database at the top level (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. This breaks rails db:setup entirely.

Reported in #5 by @sislr.

Root cause

lib/allgood/engine.rb line 5 — config.after_initialize fires during db:create, evaluates config/allgood.rb via instance_eval, and any database call in that file raises before the DB exists.

Fix

Wrap the instance_eval call in a rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished guard and emit a Rails.logger.warn so 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.

rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished => e
  Rails.logger.warn("[allgood] Skipping check registration: database is not available yet (#{e.class}). " \
                    "Checks will register on the next request once the database is ready.")

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 raising NoDatabaseError does not propagate; warning is logged
  • test_connection_not_established_is_rescued_during_config_load — same for ConnectionNotEstablished
  • test_config_loads_normally_when_database_is_available — normal config still registers checks without emitting a warning

Before: 406 runs, 9 failures (all pre-existing timezone failures), 0 errors
After: 406 runs, 9 failures (same pre-existing), 0 errors — 3 new tests all green


Came 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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant