Point reflex db commands at the db extra instead of dumping a traceback

Merged
NI
1mo ago
+49-0

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Description

closes #7259

On a bare reflex install, reflex db init reaches model.alembic_init(), which is _print_db_not_available when sqlalchemy/alembic are missing. The ImportError it raises has the right message but nothing catches it, so click prints the whole traceback and exits 1.

db_cli now checks for sqlalchemy and alembic before any subcommand runs. If either is missing it logs the same "pip install reflex[db]" message the model path uses (#7083) and exits 1. Because the check lives on the group, init, migrate, makemigrations and status all get it. reflex db --help still works without the extra, and find_spec is used so the CLI startup import guard in test_reflex.py is unaffected (reflex.model is still not imported at startup).

Before (bare install, db_url set, from the issue repro):

Traceback (most recent call last):
  ...
  File ".../reflex/reflex.py", line 986, in db_init
    model.alembic_init()
  File ".../reflex/model.py", line 47, in _print_db_not_available
    raise ImportError(msg)
ImportError: Database is not available. Please install the required packages: `pip install reflex[db]`.

After:

$ reflex db init
Database is not available. Please install the required packages: `pip install reflex[db]`.
$ echo $?
1

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Added test_db_commands_without_db_extra_point_to_install in tests/units/test_reflex.py, parametrized over the four db subcommands. It fails on main (the ImportError escapes) and passes with this change. tests/units/test_reflex.py and tests/units/test_model.py pass locally, and ruff check, ruff format and pyright are clean on the changed files. I also checked the patched CLI by hand in a fresh venv with only reflex==0.9.12a1 installed. News fragment added as news/7259.bugfix.md.