From b742a4a25f758822d98343f2492eac1b885b9652 Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Tue, 6 Oct 2026 16:55:53 -0600 Subject: [PATCH] Keep the API's logging when migrating at startup (#782) WROLPi runs alembic migrations in-process at startup (db_bootstrap). alembic/env.py called fileConfig unconditionally, which replaced the root handlers, set the root level to WARN, and disabled every logger that already existed, so the API's logs went quiet after any startup migration until the next restart. env.py now configures logging only when alembic runs from its command line (cmd_opts is set). This also caused an order-dependent test failure: test_jobs's capture test failed whenever test_db_bootstrap ran first in the same worker. Co-authored-by: Claude Opus 5.5 --- alembic/env.py | 7 ++++--- wrolpi/test/test_db_bootstrap.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/alembic/env.py b/alembic/env.py index 9aeeb116..a9575b1e 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -6,9 +6,10 @@ from alembic import context # access to the values within the .ini file in use. config = context.config -# Interpret the config file for Python logging. -# This line sets up loggers basically. -fileConfig(config.config_file_name) +# Configure logging only for the alembic command line. WROLPi also migrates in-process at startup +# (db_bootstrap), where fileConfig would replace the API's handlers and disable its existing loggers. +if config.cmd_opts is not None: + fileConfig(config.config_file_name) # add your model's MetaData object here # for 'autogenerate' support diff --git a/wrolpi/test/test_db_bootstrap.py b/wrolpi/test/test_db_bootstrap.py index 78d511e0..44649ebd 100644 --- a/wrolpi/test/test_db_bootstrap.py +++ b/wrolpi/test/test_db_bootstrap.py @@ -17,6 +17,21 @@ def permissive_sqlite_floor(): yield +def test_migration_keeps_logging(test_directory): + """WROLPi migrates in-process at startup; alembic must not reconfigure the API's logging.""" + import logging + + existing = logging.getLogger('wrolpi.test.existing_logger') + root_handlers = list(logging.getLogger().handlers) + root_level = logging.getLogger().level + + assert db_bootstrap.ensure_db() is True + + assert existing.disabled is False + assert logging.getLogger().handlers == root_handlers + assert logging.getLogger().level == root_level + + def test_ensure_db_creates_and_migrates(test_directory): """With no database file, ensure_db creates it and migrates to head.""" db_file = get_db_file()