From 01caa468beff4ce4335810712fca46ebe2cf029e Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 08:52:24 +0000 Subject: [PATCH 1/5] Report kept env.py failures and skip empty revisions A hand-edited 0.10.x alembic/env.py, kept by 0.11 because it no longer has the stock `target_metadata = None` line, is now executed by alembic. With the placeholder URL of alembic.ini it raised NoSuchModuleError, which crashed `reflex run` and gave `reflex db` a raw traceback, and without the template's empty-revision skip every makemigrations and `migrate(autogenerate=True)` wrote an empty revision. - Classify env.py as template (docstring marker present since 0.11.0a1), legacy (stock alembic) or custom, and warn once per process about a custom one. - Report any exception an alembic run raises: the startup schema check logs it and lets the app start, `reflex db` exits 1 with it, and `migrate()` logs it and returns False when env.py is custom (it still raises under the template). The custom-env hint says to move env.py aside and run `reflex db init`; the traceback is logged at debug. - Restore the root handlers and re-enable the loggers a custom env.py disables through fileConfig, so Reflex's own messages still show. - makemigrations and migrate(autogenerate=True) run `alembic check` first and write a revision only when it reports changes. - A second legacy replacement keeps the earlier env.old, backing up to the first free name of env.old, env.old.1, ... Fixes FINDING-005, FINDING-006 and FINDING-017 of the 0.11.0a1 pre-release testing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi --- docs/database/overview.md | 23 +-- news/+kept-env-py-errors.bugfix.md | 1 + news/+no-empty-revisions.bugfix.md | 1 + reflex/model.py | 37 +++-- reflex/reflex.py | 11 +- reflex/utils/migrations.py | 130 ++++++++++++++- reflex/utils/prerequisites.py | 6 +- tests/units/utils/test_migrations.py | 240 ++++++++++++++++++++++++++- 8 files changed, 407 insertions(+), 42 deletions(-) create mode 100644 news/+kept-env-py-errors.bugfix.md create mode 100644 news/+no-empty-revisions.bugfix.md diff --git a/docs/database/overview.md b/docs/database/overview.md index 08fac91702e..3a1774dd6ab 100644 --- a/docs/database/overview.md +++ b/docs/database/overview.md @@ -165,10 +165,11 @@ command line works on the same environment: | `reflex db status` | `alembic history --indicate-current` | Anything else alembic can do, such as `alembic downgrade -1`, works the same -way. The generated `env.py` skips an autogenerated revision when the schema has -not changed, for `reflex db makemigrations` and `alembic revision --autogenerate` -alike, and both evaluate the pages before comparing the models, as -`alembic check` does, so a model that only a page function imports is compared too. +way. `reflex db makemigrations` writes a revision only when `alembic check` +reports changes, and the generated `env.py` skips an empty autogenerated revision +for `alembic revision --autogenerate` too. Both evaluate the pages before +comparing the models, as `alembic check` does, so a model that only a page +function imports is compared too. Generated scripts land in the `alembic/versions` directory. It is recommended that they be inspected before applying them. Bear in mind that your newest @@ -184,15 +185,17 @@ database schema is not up to date, a warning will be displayed on the console. Apps initialized by an earlier Reflex release have a stock alembic `env.py` that does not load the app. The next `reflex db` command, or `migrate()` call, replaces it -with the current template and keeps the previous file as `env.old`; existing -revisions are kept. +with the current template and keeps the previous file as `env.old` (or +`env.old.1`, and so on, if that name is taken); existing revisions are kept. An `env.py` you edited yourself is kept as it is, as long as it no longer contains the stock template's `target_metadata = None` line, which is how Reflex tells the -two apart. It then has none of the template's behavior, such as skipping empty -revisions or giving existing rows a new column's default. To adopt the current -template, move your `env.py` aside and run `reflex db init`, which writes a -missing `env.py` afresh. +two apart. It then has none of the template's behavior, such as taking the +database URL from `rxconfig.py` or giving existing rows a new column's default, +and Reflex warns about it once. If it fails, for example on the placeholder URL +in `alembic.ini`, the startup check logs the error and the app still starts, +while the `reflex db` commands exit with it. To adopt the current template, move +your `env.py` aside and run `reflex db init`, which writes a missing `env.py` afresh. ``` For a local experiment, `create_all` creates any missing tables without diff --git a/news/+kept-env-py-errors.bugfix.md b/news/+kept-env-py-errors.bugfix.md new file mode 100644 index 00000000000..8bfae224a75 --- /dev/null +++ b/news/+kept-env-py-errors.bugfix.md @@ -0,0 +1 @@ +A hand-edited `alembic/env.py` kept from an earlier release no longer stops `reflex run` when it fails, for example on the placeholder URL in `alembic.ini`: the startup check logs the error with the fix (move the file aside and run `reflex db init`), and the `reflex db` commands exit with that message instead of a traceback. Reflex also warns once that such an `env.py` is not the Reflex template, and replacing a stock `env.py` again keeps the earlier `env.old` as `env.old.1`. diff --git a/news/+no-empty-revisions.bugfix.md b/news/+no-empty-revisions.bugfix.md new file mode 100644 index 00000000000..cb42b6bf0f0 --- /dev/null +++ b/news/+no-empty-revisions.bugfix.md @@ -0,0 +1 @@ +`reflex db makemigrations` and `migrate(autogenerate=True)` write a revision only when `alembic check` reports model changes, so an `env.py` that is not the Reflex template no longer gets an empty revision on every call. diff --git a/reflex/model.py b/reflex/model.py index a55225fd454..364f39040ee 100644 --- a/reflex/model.py +++ b/reflex/model.py @@ -439,22 +439,10 @@ def alembic_autogenerate( ) if not environment.ALEMBIC_CONFIG.get().exists(): return False - from alembic.util.exc import AutogenerateDiffsDetected - from reflex.utils import migrations _update_legacy_env() - try: - migrations.run("--quiet", "check") - except AutogenerateDiffsDetected: - if write_migration_scripts: - migrations.run( - "revision", - "--autogenerate", - *(("--message", message) if message else ()), - ) - return True - return False + return migrations.autogenerate(message, write=write_migration_scripts) def migrate(autogenerate: bool = False) -> bool | None: """Apply alembic migrations, optionally autogenerating one first. @@ -470,17 +458,32 @@ def migrate(autogenerate: bool = False) -> bool | None: Returns: True - indicating the process was successful. None - indicating the process was skipped. + False - indicating that an env.py other than the template failed; + the error is logged instead of raised, so that an app calling + this at import still starts, as before env.py was run. + + Raises: + CommandError: If alembic reports an error. """ if not environment.ALEMBIC_CONFIG.get().exists(): return None + from alembic.util.exc import CommandError + from reflex.utils import migrations _update_legacy_env() - # Pending scripts are applied first: autogenerate needs the database at head. - migrations.run("upgrade", "head") - if autogenerate: - migrations.run("revision", "--autogenerate") + try: + # Pending scripts are applied first: autogenerate needs the database at head. migrations.run("upgrade", "head") + if autogenerate and migrations.autogenerate(): + migrations.run("upgrade", "head") + except CommandError: + raise + except Exception as error: + if migrations.env_kind() != "custom": + raise + migrations.report_failure(error) + return False return True else: diff --git a/reflex/reflex.py b/reflex/reflex.py index 93ebf4a747c..c9be6d47f0d 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -1117,7 +1117,7 @@ def _alembic_errors() -> Generator[None, None, None]: None, while the alembic commands run. Raises: - Exit: With status 1 when alembic reports a CommandError. + Exit: With status 1 when alembic, or env.py as it runs, raises. """ from alembic.util.exc import CommandError @@ -1128,6 +1128,9 @@ def _alembic_errors() -> Generator[None, None, None]: except CommandError as error: logger.error(migrations.describe_error(error), extra={"rich": True}) raise click.exceptions.Exit(1) from None + except Exception as error: + migrations.report_failure(error) + raise click.exceptions.Exit(1) from None @db_cli.command(name="init") @@ -1226,11 +1229,7 @@ def makemigrations(message: str | None): if not prerequisites.check_db_initialized(): raise click.exceptions.Exit(1) with _alembic_errors(): - migrations.run( - "revision", - "--autogenerate", - *(("--message", message) if message else ()), - ) + migrations.autogenerate(message) @cli.command() diff --git a/reflex/utils/migrations.py b/reflex/utils/migrations.py index 4055d2a9f14..c4160d43d9c 100644 --- a/reflex/utils/migrations.py +++ b/reflex/utils/migrations.py @@ -12,6 +12,7 @@ import logging import shutil from pathlib import Path +from typing import Literal from reflex_base.environment import environment from rich.markup import escape @@ -33,6 +34,27 @@ "Updated env.py to load the app; the previous one is kept as {backup}." ) +# In the docstring of every env.py written from the template, since 0.11.0a1. +TEMPLATE_MARKER = "Generated by ``reflex db init``" + +# The line of alembic's stock env.py that earlier releases never ran or edited. +LEGACY_MARKER = "target_metadata = None" + +CUSTOM_ENV_HINT = ( + "alembic/env.py is not the Reflex template: move it aside and run " + "[bold]reflex db init[/bold] to write the current template." +) + +CUSTOM_ENV_WARNING = ( + "alembic/env.py is not the Reflex template, so what the template adds may be " + "missing: the database URL from rxconfig.py, the models of the whole app, " + "server defaults for added columns and warnings for dropped tables. Move it " + "aside and run [bold]reflex db init[/bold] to adopt the template; existing " + "revisions are kept." +) + +EnvKind = Literal["template", "legacy", "custom"] + # True while env.py imports the app, when an import-time `migrate()` call would # start a second alembic run inside the one already in progress. _loading_app = False @@ -72,6 +94,15 @@ def run(*args: str) -> None: return import alembic.config + # An env.py other than the template may apply the logging settings in + # alembic.ini, which replace the root handlers and disable every other logger. + root = logging.getLogger() + root_handlers, root_level = root.handlers[:], root.level + enabled = [ + child + for child in logging.Logger.manager.loggerDict.values() + if isinstance(child, logging.Logger) and not child.disabled + ] _in_run = True try: alembic.config.main( @@ -85,6 +116,38 @@ def run(*args: str) -> None: ) finally: _in_run = False + root.handlers[:] = root_handlers + root.setLevel(root_level) + for child in enabled: + child.disabled = False + + +def autogenerate(message: str | None = None, write: bool = True) -> bool: + """Compare the models with the database and write a revision if they differ. + + ``alembic check`` runs first, so that no empty revision is written even when + env.py, unlike the template, does not skip one. + + Args: + message: The message of the revision. + write: Write the revision; otherwise only compare. + + Returns: + True when the models and the database differ. + """ + from alembic.util.exc import AutogenerateDiffsDetected + + try: + run("--quiet", "check") + except AutogenerateDiffsDetected: + if write: + run( + "revision", + "--autogenerate", + *(("--message", message) if message else ()), + ) + return True + return False def evaluate_pages() -> None: @@ -171,15 +234,18 @@ def script_directory() -> Path: return Path(alembic.script.ScriptDirectory.from_config(config).dir) -def is_legacy_env() -> bool: - """Whether env.py predates the reflex template and cannot load the app. +def env_kind() -> EnvKind | None: + """Tell where env.py comes from. Earlier Reflex releases ran `alembic init` with alembic's stock template and never executed the resulting env.py, which still reads ``target_metadata = None`` and connects to the placeholder URL in alembic.ini. + An env.py with neither that line nor the template's marker was written or + edited by hand, and alembic runs it as it is. Returns: - True if env.py is the stock alembic template. + ``"template"`` for the reflex template, ``"legacy"`` for the stock one, + ``"custom"`` for any other, or None without an env.py. """ from alembic.util.exc import CommandError @@ -187,20 +253,53 @@ def is_legacy_env() -> bool: env_py = script_directory() / "env.py" except CommandError: # Let the command that needs the directory report what is wrong with it. - return False - return env_py.exists() and "target_metadata = None" in env_py.read_text() + return None + if not env_py.exists(): + return None + source = env_py.read_text() + if TEMPLATE_MARKER in source: + return "template" + return "legacy" if LEGACY_MARKER in source else "custom" + + +def check_env() -> EnvKind | None: + """Tell where env.py comes from, warning once per process when it is custom. + + Returns: + What ``env_kind`` returns. + """ + kind = env_kind() + if kind == "custom": + logger.warning(CUSTOM_ENV_WARNING, extra={"dedupe": True, "rich": True}) + return kind + + +def is_legacy_env() -> bool: + """Whether env.py predates the reflex template and cannot load the app. + + Returns: + True if env.py is the stock alembic template. + """ + return env_kind() == "legacy" def update_legacy_env() -> Path | None: """Replace a stock env.py with the reflex template, keeping it as env.old. + An earlier backup is kept too: the new one takes the first free name of + env.old, env.old.1, env.old.2 and so on. + Returns: - Where the previous env.py went, or None when env.py already loads the app. + Where the previous env.py went, or None when env.py is not the stock one. """ - if not is_legacy_env(): + if check_env() != "legacy": return None env_py = script_directory() / "env.py" backup = env_py.with_name("env.old") + index = 0 + while backup.exists(): + index += 1 + backup = env_py.with_name(f"env.old.{index}") env_py.replace(backup) install_env() return backup @@ -220,3 +319,20 @@ def describe_error(error: Exception) -> str: if "Target database is not up to date." in message: message += " Run [bold]reflex db migrate[/bold] to update database." return message + + +def report_failure(error: Exception) -> None: + """Log an exception other than a ``CommandError`` that an alembic run raised. + + It names the exception, with the fix when env.py is not the template, once + per process: an import-time ``migrate()`` and the startup check fail alike. + The traceback is logged at debug level. + + Args: + error: The exception raised while alembic ran env.py. + """ + message = escape(f"Alembic failed with {type(error).__name__}: {error}") + if env_kind() == "custom": + message = f"{message}\n{CUSTOM_ENV_HINT}" + logger.error(message, extra={"dedupe": True, "rich": True}) + logger.debug("The alembic run failed.", exc_info=error) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index cd87631ca40..c7e32ac36ab 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -872,7 +872,7 @@ def check_schema_up_to_date(): from reflex.utils import migrations - if migrations.is_legacy_env(): + if migrations.check_env() == "legacy": logger.error(migrations.LEGACY_ENV_HINT, extra={"dedupe": True, "rich": True}) return try: @@ -885,6 +885,10 @@ def check_schema_up_to_date(): ) except CommandError as command_error: logger.error(migrations.describe_error(command_error), extra={"rich": True}) + except Exception as error: + # Reported, not raised: the app starts without the check, as it did when + # earlier releases never ran env.py. + migrations.report_failure(error) @once diff --git a/tests/units/utils/test_migrations.py b/tests/units/utils/test_migrations.py index 1aaf686aac5..190e1c6fc70 100644 --- a/tests/units/utils/test_migrations.py +++ b/tests/units/utils/test_migrations.py @@ -12,7 +12,7 @@ import click.testing import pytest from reflex_base import constants -from reflex_base.config import reload_config +from reflex_base.config import get_config, reload_config from reflex_base.environment import environment from reflex_base.registry import RegistrationContext from reflex_base.utils import serializers @@ -1053,3 +1053,241 @@ def test_deprecated_migration_helpers_run_through_env_py( assert lines[-1].endswith(f"{current} (head), add t2") for name in ("alembic_autogenerate", "get_migration_history", "format_revision"): assert f"reflex.model.{name}" in caplog.text + + +# What a 0.10.x user made of the stock env.py: models wired in, URL left as is. +HAND_EDITED_METADATA = ( + "from sqlmodel import SQLModel\ntarget_metadata = SQLModel.metadata" +) +REGISTRY_METADATA = ( + "from reflex.model import ModelRegistry\n" + "target_metadata = ModelRegistry.get_metadata()" +) +PLACEHOLDER_URL = "driver://user:pass@localhost/dbname" + + +def _stock_env_py() -> str: + """Alembic's stock env.py, which earlier Reflex releases wrote and never ran. + + Returns: + The source of the stock env.py. + """ + import alembic.config + + return Path( + alembic.config.Config().get_template_directory(), "generic", "env.py" + ).read_text() + + +def _write_custom_env(project: Path, metadata: str, url: str | None = None) -> str: + """Replace env.py with an edited stock one, as 0.10.x users wrote by hand. + + Args: + project: The project directory. + metadata: The code that sets ``target_metadata``. + url: The ``sqlalchemy.url`` to write to alembic.ini instead of the + placeholder, if any. + + Returns: + The source of the written env.py. + """ + source = _stock_env_py().replace("target_metadata = None", metadata) + (project / "alembic" / "env.py").write_text(source) + # Without the template's render_item, revisions need sqlmodel imported. + mako = project / "alembic" / "script.py.mako" + mako.write_text( + mako.read_text().replace( + "import sqlalchemy as sa", "import sqlalchemy as sa\nimport sqlmodel" + ) + ) + if url is not None: + ini = project / "alembic.ini" + ini.write_text(ini.read_text().replace(PLACEHOLDER_URL, url)) + return source + + +def _db_url() -> str: + """The database URL of the project's rxconfig.py. + + Returns: + The configured ``db_url``. + """ + url = get_config().db_url + assert url is not None + return url + + +def _assert_env_failure_reported(caplog: pytest.LogCaptureFixture, error: str) -> None: + """Check that the env.py failure was logged once, with its cause and the fix. + + Args: + caplog: The log capture fixture. + error: The exception class name the message must name. + """ + (message,) = [message for message in _errors(caplog) if error in message] + assert "not the Reflex template" in message + assert "reflex db init" in message + + +def test_custom_env_failure_is_reported_with_the_fix( + project: Path, caplog: pytest.LogCaptureFixture +): + """A hand-edited env.py that fails is reported with the hint, not a traceback. + + The startup check logs it and lets the app start, as 0.10.x, which never ran + env.py, did; the commands exit with status 1. env.py itself is kept. + """ + _db("init") + source = _write_custom_env(project, HAND_EDITED_METADATA) + caplog.clear() + + # What `reflex run` does before serving: compile with the schema check. + _forget_app() + prerequisites.get_compiled_app(dry_run=True, check_if_schema_up_to_date=True) + _assert_env_failure_reported(caplog, "NoSuchModuleError") + caplog.clear() + + for args in (["status"], ["migrate"], ["makemigrations"]): + result = _db(*args, exit_code=1) + assert isinstance(result.exception, SystemExit), result.exception + _assert_env_failure_reported(caplog, "NoSuchModuleError") + caplog.clear() + + # `rx.model.migrate()` at module level does not stop the app from importing. + assert reflex.model.migrate(autogenerate=True) is False + _assert_env_failure_reported(caplog, "NoSuchModuleError") + assert (project / "alembic" / "env.py").read_text() == source + assert len(_versions(project)) == 1 + + +def test_template_env_failure_is_reported_without_the_hint( + project: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture +): + """A failure under the template env.py is an error of its own, not the hint's. + + The commands exit with status 1 and the error; `migrate()` raises it. + """ + _db("init") + + def unreachable(): + msg = "database unreachable" + raise RuntimeError(msg) + + monkeypatch.setattr(reflex.model, "get_engine", unreachable) + caplog.clear() + + _db("migrate", exit_code=1) + (message,) = _errors(caplog) + assert "RuntimeError: database unreachable" in message + assert "not the Reflex template" not in message + caplog.clear() + prerequisites.check_schema_up_to_date() + (message,) = _errors(caplog) + assert "database unreachable" in message + with pytest.raises(RuntimeError, match="database unreachable"): + reflex.model.migrate() + + +@pytest.mark.parametrize("orm", ["sqlmodel", "sqlalchemy"]) +def test_custom_env_warns_once_that_it_is_not_the_template( + project: Path, caplog: pytest.LogCaptureFixture, orm: str +): + """A kept env.py that is not the template is pointed out, once per process. + + It still runs, and the process keeps the logging it set up, although the + stock env.py applies the logging settings in alembic.ini. + + Args: + project: The project directory. + caplog: The log capture fixture. + orm: Which ORM the app's models use. + """ + _write_app(project, orm, COLUMNS[orm][0]) + _db("init") + caplog.clear() + prerequisites.check_schema_up_to_date() + assert _warnings(caplog, "not the Reflex template") == [] + + _write_custom_env(project, REGISTRY_METADATA, url=_db_url()) + for check in ( + lambda: _db("status"), + prerequisites.check_schema_up_to_date, + reflex.model.migrate, + ): + check() + (warning,) = _warnings(caplog, "not the Reflex template") + assert "reflex db init" in warning + (record,) = [r for r in caplog.records if r.getMessage() == warning] + assert getattr(record, "dedupe", False) + caplog.clear() + assert _errors(caplog) == [] + logging.getLogger("reflex.utils.migrations").warning("still logging") + assert "still logging" in caplog.text + + +@pytest.mark.parametrize("orm", ["sqlmodel", "sqlalchemy"]) +def test_custom_env_writes_no_empty_revision(project: Path, orm: str): + """Without the template's empty-revision skip, unchanged models write nothing. + + Args: + project: The project directory. + orm: Which ORM the app's models use. + """ + v1, v2, _ = COLUMNS[orm] + _write_app(project, orm, v1) + _db("init") + _write_custom_env(project, REGISTRY_METADATA, url=_db_url()) + + _db("makemigrations", "--message", "nothing") + _db("makemigrations") + assert reflex.model.migrate(autogenerate=True) is True + with get_engine().connect() as connection: + assert not reflex.model.alembic_autogenerate(connection) + assert len(_versions(project)) == 1 + + _write_app(project, orm, v2) + _db("makemigrations", "--message", "add t2") + assert len(_versions(project)) == 2 + _db("migrate") + _db("makemigrations") + assert len(_versions(project)) == 2 + + +def test_custom_env_import_time_autogenerate_writes_no_empty_revision(project: Path): + """`migrate(autogenerate=True)` at module level writes nothing for unchanged models.""" + _db("init") + _write_custom_env(project, HAND_EDITED_METADATA, url=_db_url()) + (project / APP_NAME / f"{APP_NAME}.py").write_text( + "import reflex as rx\n\n\n" + f"class Thing(rx.Model, table=True):\n{COLUMNS['sqlmodel'][0]}\n\n\n" + "rx.Model.migrate(autogenerate=True)\n\n\napp = rx.App()\n" + ) + for _ in range(3): + _forget_app() + # What `reflex run` does first: import the app. + prerequisites.get_app() + + assert len(_versions(project)) == 1 + + +def test_second_legacy_env_keeps_the_first_backup( + project: Path, caplog: pytest.LogCaptureFixture +): + """Replacing a stock env.py again keeps the earlier env.old, using a free name.""" + caplog.set_level(logging.INFO) + _db("init") + env_py = project / "alembic" / "env.py" + alembic_dir = project / "alembic" + first = _stock_env_py() + "\n# first\n" + second = _stock_env_py() + "\n# second\n" + third = _stock_env_py() + "\n# third\n" + + for source in (first, second, third): + env_py.write_text(source) + assert prerequisites.check_db_initialized() + + assert (alembic_dir / "env.old").read_text() == first + assert (alembic_dir / "env.old.1").read_text() == second + assert (alembic_dir / "env.old.2").read_text() == third + assert "env.old.2" in caplog.text + assert env_py.read_text() == migrations.TEMPLATE_ENV_PY.read_text() From 38150cb69b4d066468a6749878d45d88fb2cc134 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 09:25:16 +0000 Subject: [PATCH 2/5] Keep the custom env.py test within what every SQLite runs test_custom_env_writes_no_empty_revision made t1 nullable and added a NOT NULL t2 under a stock alembic env.py, which has no render_as_batch. The revision then holds a plain `ALTER TABLE ... ALTER COLUMN ... DROP NOT NULL`, which only recent SQLite builds accept: it passed with the SQLite 3.53 of uv's Pythons and failed on the system Pythons of the Windows runners and ubuntu 3.14 (SQLite 3.45 reproduces it). The step now adds a nullable column, and the test asserts the revision has no alter_column. `reflex db makemigrations` also says when it writes no revision. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi --- reflex/reflex.py | 3 ++- tests/units/utils/test_migrations.py | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/reflex/reflex.py b/reflex/reflex.py index c9be6d47f0d..e3a2b9aaf35 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -1229,7 +1229,8 @@ def makemigrations(message: str | None): if not prerequisites.check_db_initialized(): raise click.exceptions.Exit(1) with _alembic_errors(): - migrations.autogenerate(message) + if not migrations.autogenerate(message): + logger.info("No model changes detected; no revision was written.") @cli.command() diff --git a/tests/units/utils/test_migrations.py b/tests/units/utils/test_migrations.py index 190e1c6fc70..98b16e04144 100644 --- a/tests/units/utils/test_migrations.py +++ b/tests/units/utils/test_migrations.py @@ -1064,6 +1064,11 @@ def test_deprecated_migration_helpers_run_through_env_py( "target_metadata = ModelRegistry.get_metadata()" ) PLACEHOLDER_URL = "driver://user:pass@localhost/dbname" +# A column every SQLite build can add without the template's batch mode. +NULLABLE_T2 = { + "sqlmodel": "\n t2: str | None = None", + "sqlalchemy": "\n t2: Mapped[str | None] = mapped_column()", +} def _stock_env_py() -> str: @@ -1226,19 +1231,27 @@ def test_custom_env_warns_once_that_it_is_not_the_template( @pytest.mark.parametrize("orm", ["sqlmodel", "sqlalchemy"]) -def test_custom_env_writes_no_empty_revision(project: Path, orm: str): +def test_custom_env_writes_no_empty_revision( + project: Path, caplog: pytest.LogCaptureFixture, orm: str +): """Without the template's empty-revision skip, unchanged models write nothing. Args: project: The project directory. + caplog: The log capture fixture. orm: Which ORM the app's models use. """ - v1, v2, _ = COLUMNS[orm] + caplog.set_level(logging.INFO) + v1 = COLUMNS[orm][0] + # Only a nullable column is added: without the template's batch mode, older + # SQLite builds can neither alter a column nor add a NOT NULL one. + v2 = v1 + NULLABLE_T2[orm] _write_app(project, orm, v1) _db("init") _write_custom_env(project, REGISTRY_METADATA, url=_db_url()) _db("makemigrations", "--message", "nothing") + assert "no revision was written" in caplog.text _db("makemigrations") assert reflex.model.migrate(autogenerate=True) is True with get_engine().connect() as connection: @@ -1248,6 +1261,9 @@ def test_custom_env_writes_no_empty_revision(project: Path, orm: str): _write_app(project, orm, v2) _db("makemigrations", "--message", "add t2") assert len(_versions(project)) == 2 + script = _versions(project)[-1].read_text().split("def downgrade")[0] + assert "add_column('thing'" in script + assert "alter_column" not in script _db("migrate") _db("makemigrations") assert len(_versions(project)) == 2 From 6b76448005d9e544d1f8a672baa93c26d021f6d7 Mon Sep 17 00:00:00 2001 From: Farhan Date: Wed, 11 Nov 2026 23:29:51 +0500 Subject: [PATCH 3/5] Keep revision script tracebacks and hint only at a failing env.py A failing revision script was reported in one line, without its file or line, even under the template env.py; only REFLEX_LOGLEVEL=debug showed the traceback. With a hand-edited env.py that works, the same failure also got the "move env.py aside" hint, which points at the wrong file. report_failure now hints and keeps the traceback at debug level only when the innermost frame in the alembic script directory is a custom env.py. Any other failure is logged with its traceback. --- reflex/utils/migrations.py | 38 ++++++++++++++++++----- tests/units/utils/test_migrations.py | 45 ++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/reflex/utils/migrations.py b/reflex/utils/migrations.py index c4160d43d9c..5d8b93a3f0e 100644 --- a/reflex/utils/migrations.py +++ b/reflex/utils/migrations.py @@ -11,6 +11,7 @@ import io import logging import shutil +import traceback from pathlib import Path from typing import Literal @@ -321,18 +322,39 @@ def describe_error(error: Exception) -> str: return message +def _raised_in_env_py(error: Exception) -> bool: + """Whether env.py itself raised, not a revision script that it ran. + + Args: + error: The exception raised while alembic ran. + + Returns: + True if the innermost frame in the script directory is env.py's. + """ + scripts = script_directory() + for frame in reversed(traceback.extract_tb(error.__traceback__)): + path = Path(frame.filename) + if path.is_relative_to(scripts): + return path.name == "env.py" + return False + + def report_failure(error: Exception) -> None: """Log an exception other than a ``CommandError`` that an alembic run raised. - It names the exception, with the fix when env.py is not the template, once - per process: an import-time ``migrate()`` and the startup check fail alike. - The traceback is logged at debug level. + A custom env.py that failed is named with the fix and the traceback goes to + the debug log; any other failure, such as a revision script's, is logged + with its traceback. Each is logged once per process: an import-time + ``migrate()`` and the startup check fail alike. Args: - error: The exception raised while alembic ran env.py. + error: The exception raised while alembic ran. """ message = escape(f"Alembic failed with {type(error).__name__}: {error}") - if env_kind() == "custom": - message = f"{message}\n{CUSTOM_ENV_HINT}" - logger.error(message, extra={"dedupe": True, "rich": True}) - logger.debug("The alembic run failed.", exc_info=error) + if env_kind() == "custom" and _raised_in_env_py(error): + logger.error( + f"{message}\n{CUSTOM_ENV_HINT}", extra={"dedupe": True, "rich": True} + ) + logger.debug("The alembic run failed.", exc_info=error) + else: + logger.error(message, exc_info=error, extra={"dedupe": True, "rich": True}) diff --git a/tests/units/utils/test_migrations.py b/tests/units/utils/test_migrations.py index 98b16e04144..cfd366af7f4 100644 --- a/tests/units/utils/test_migrations.py +++ b/tests/units/utils/test_migrations.py @@ -1307,3 +1307,48 @@ def test_second_legacy_env_keeps_the_first_backup( assert (alembic_dir / "env.old.2").read_text() == third assert "env.old.2" in caplog.text assert env_py.read_text() == migrations.TEMPLATE_ENV_PY.read_text() + + +def _write_failing_revision(project: Path) -> None: + """Add a revision script on top of the initial one whose upgrade raises. + + Args: + project: The project directory. + """ + (initial,) = _versions(project) + down = initial.name.partition("_")[0] + (project / "alembic" / "versions" / "f00d_broken.py").write_text( + f'revision = "f00d"\ndown_revision = "{down}"\n' + "branch_labels = None\ndepends_on = None\n\n\n" + "def upgrade():\n sa.Column('note')\n\n\n" + "def downgrade():\n pass\n" + ) + + +@pytest.mark.parametrize("custom", [False, True]) +def test_revision_script_failure_keeps_its_traceback( + project: Path, caplog: pytest.LogCaptureFixture, custom: bool +): + """A failing revision script is logged with its traceback and without the hint. + + The fault is in the script, under either env.py, so moving env.py aside + would not help. + + Args: + project: The project directory. + caplog: The log capture fixture. + custom: Run the revision under a working hand-edited env.py. + """ + _db("init") + if custom: + _write_custom_env(project, REGISTRY_METADATA, url=_db_url()) + _write_failing_revision(project) + caplog.clear() + + _db("migrate", exit_code=1) + + (record,) = [r for r in caplog.records if r.levelno >= logging.ERROR] + assert "NameError" in record.getMessage() + assert "not the Reflex template" not in record.getMessage() + assert record.exc_info is not None + assert "f00d_broken.py" in caplog.text From aa065b281f5824795db46cb5f27085de983d279f Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 18:41:58 +0000 Subject: [PATCH 4/5] Hint at a custom env.py that fails to compile; match env.py by its path A SyntaxError in a hand-edited env.py is raised while alembic compiles it, so no traceback frame is in the script directory and the "move env.py aside" hint was lost. Its filename is now taken as the innermost location. The frame is compared with the script directory's env.py itself, so a revision script named env.py under versions/ keeps its traceback and gets no hint. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi --- reflex/utils/migrations.py | 11 +++++---- tests/units/utils/test_migrations.py | 36 ++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/reflex/utils/migrations.py b/reflex/utils/migrations.py index 5d8b93a3f0e..6c0135d4038 100644 --- a/reflex/utils/migrations.py +++ b/reflex/utils/migrations.py @@ -329,13 +329,16 @@ def _raised_in_env_py(error: Exception) -> bool: error: The exception raised while alembic ran. Returns: - True if the innermost frame in the script directory is env.py's. + True if the innermost frame in the script directory is env.py's, or + env.py failed to compile, which leaves only its file name. """ scripts = script_directory() - for frame in reversed(traceback.extract_tb(error.__traceback__)): - path = Path(frame.filename) + paths = [frame.filename for frame in traceback.extract_tb(error.__traceback__)] + if isinstance(error, SyntaxError) and error.filename: + paths.append(error.filename) + for path in map(Path, reversed(paths)): if path.is_relative_to(scripts): - return path.name == "env.py" + return path == scripts / "env.py" return False diff --git a/tests/units/utils/test_migrations.py b/tests/units/utils/test_migrations.py index cfd366af7f4..b0a1af5e6b7 100644 --- a/tests/units/utils/test_migrations.py +++ b/tests/units/utils/test_migrations.py @@ -1309,15 +1309,33 @@ def test_second_legacy_env_keeps_the_first_backup( assert env_py.read_text() == migrations.TEMPLATE_ENV_PY.read_text() -def _write_failing_revision(project: Path) -> None: +def test_custom_env_syntax_error_is_reported_with_the_fix( + project: Path, caplog: pytest.LogCaptureFixture +): + """A hand-edited env.py that does not compile is reported with the hint. + + A SyntaxError leaves no env.py frame in the traceback, only its filename. + """ + _db("init") + source = _write_custom_env(project, REGISTRY_METADATA, url=_db_url()) + (project / "alembic" / "env.py").write_text(source + "\ndef broken(:\n") + caplog.clear() + + _db("migrate", exit_code=1) + + _assert_env_failure_reported(caplog, "SyntaxError") + + +def _write_failing_revision(project: Path, name: str = "f00d_broken.py") -> None: """Add a revision script on top of the initial one whose upgrade raises. Args: project: The project directory. + name: The revision script's file name. """ (initial,) = _versions(project) down = initial.name.partition("_")[0] - (project / "alembic" / "versions" / "f00d_broken.py").write_text( + (project / "alembic" / "versions" / name).write_text( f'revision = "f00d"\ndown_revision = "{down}"\n' "branch_labels = None\ndepends_on = None\n\n\n" "def upgrade():\n sa.Column('note')\n\n\n" @@ -1325,24 +1343,28 @@ def _write_failing_revision(project: Path) -> None: ) -@pytest.mark.parametrize("custom", [False, True]) +@pytest.mark.parametrize( + ("custom", "name"), + [(False, "f00d_broken.py"), (True, "f00d_broken.py"), (True, "env.py")], +) def test_revision_script_failure_keeps_its_traceback( - project: Path, caplog: pytest.LogCaptureFixture, custom: bool + project: Path, caplog: pytest.LogCaptureFixture, custom: bool, name: str ): """A failing revision script is logged with its traceback and without the hint. The fault is in the script, under either env.py, so moving env.py aside - would not help. + would not help, even when the script is itself named env.py. Args: project: The project directory. caplog: The log capture fixture. custom: Run the revision under a working hand-edited env.py. + name: The revision script's file name. """ _db("init") if custom: _write_custom_env(project, REGISTRY_METADATA, url=_db_url()) - _write_failing_revision(project) + _write_failing_revision(project, name) caplog.clear() _db("migrate", exit_code=1) @@ -1351,4 +1373,4 @@ def test_revision_script_failure_keeps_its_traceback( assert "NameError" in record.getMessage() assert "not the Reflex template" not in record.getMessage() assert record.exc_info is not None - assert "f00d_broken.py" in caplog.text + assert f"versions/{name}" in caplog.text From 9e53d00e48c33c15249723f1ae8f9b48f94cfa7a Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 18:44:24 +0000 Subject: [PATCH 5/5] Build the expected revision path with Path in the traceback test Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi --- tests/units/utils/test_migrations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/units/utils/test_migrations.py b/tests/units/utils/test_migrations.py index b0a1af5e6b7..8e6320f9936 100644 --- a/tests/units/utils/test_migrations.py +++ b/tests/units/utils/test_migrations.py @@ -1373,4 +1373,4 @@ def test_revision_script_failure_keeps_its_traceback( assert "NameError" in record.getMessage() assert "not the Reflex template" not in record.getMessage() assert record.exc_info is not None - assert f"versions/{name}" in caplog.text + assert str(Path("versions", name)) in caplog.text