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()