Skip to content

Commit

Permalink
fix: preserve loggers across migrations (#2835)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeldking committed Apr 10, 2024
1 parent dfcfeeb commit 2821bb4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/phoenix/db/alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = WARN
level = INFO
handlers =
qualname = alembic

Expand Down
29 changes: 27 additions & 2 deletions src/phoenix/db/migrate.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import logging
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Iterator, List

from alembic import command
from alembic.config import Config
from sqlalchemy import URL


@contextmanager
def using_log_level(new_level: int, logger_names: List[str]) -> Iterator[Any]:
original_levels = {}
try:
for name in logger_names:
logger = logging.getLogger(name)
# Save the original log level
original_levels[name] = logger.level
# Set the new log level
logger.setLevel(new_level)
yield
finally:
# Revert log levels to their original states
for name, level in original_levels.items():
logger = logging.getLogger(name)
logger.setLevel(level)


logger = logging.getLogger(__name__)


Expand All @@ -16,7 +37,8 @@ def migrate(url: URL) -> None:
Args:
url: The database URL.
"""
logger.warning("Running migrations on the database")
print("🏃‍♀️‍➡️ Running migrations on the database.")
print("---------------------------")
config_path = str(Path(__file__).parent.resolve() / "alembic.ini")
alembic_cfg = Config(config_path)

Expand All @@ -25,4 +47,7 @@ def migrate(url: URL) -> None:
alembic_cfg.set_main_option("script_location", scripts_location)
alembic_cfg.set_main_option("sqlalchemy.url", str(url))

command.upgrade(alembic_cfg, "head")
with using_log_level(logging.DEBUG, ["sqlalchemy", "alembic"]):
command.upgrade(alembic_cfg, "head")
print("---------------------------")
print("✅ Migrations complete.")
2 changes: 1 addition & 1 deletion src/phoenix/db/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
fileConfig(config.config_file_name, disable_existing_loggers=False)

# add your model's MetaData object here
# for 'autogenerate' support
Expand Down

0 comments on commit 2821bb4

Please sign in to comment.