From fc8ae2fe21d4fbeb9d673c4e3881c114ecee01ff Mon Sep 17 00:00:00 2001 From: Igor Ilic <30923996+dexters1@users.noreply.github.com> Date: Thu, 24 Jul 2025 16:32:34 +0200 Subject: [PATCH] feat: Add database migration at start of MCP server (#1145) ## Description ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin. --- cognee-mcp/src/server.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cognee-mcp/src/server.py b/cognee-mcp/src/server.py index 5a0a36b5a..9e1ecc23a 100755 --- a/cognee-mcp/src/server.py +++ b/cognee-mcp/src/server.py @@ -4,6 +4,8 @@ import sys import argparse import cognee import asyncio +import subprocess +from pathlib import Path from cognee.shared.logging_utils import get_logger, setup_logging, get_log_file_location import importlib.util @@ -552,6 +554,29 @@ async def main(): args = parser.parse_args() + # Run Alembic migrations from the main cognee directory where alembic.ini is located + print("Running database migrations...") + migration_result = subprocess.run( + ["alembic", "upgrade", "head"], + capture_output=True, + text=True, + cwd=Path(__file__).resolve().parent.parent.parent, + ) + + if migration_result.returncode != 0: + migration_output = migration_result.stderr + migration_result.stdout + # Check for the expected UserAlreadyExists error (which is not critical) + if ( + "UserAlreadyExists" in migration_output + or "User default_user@example.com already exists" in migration_output + ): + print("Warning: Default user already exists, continuing startup...") + else: + print(f"Migration failed with unexpected error: {migration_output}") + sys.exit(1) + + print("Database migrations done.") + logger.info(f"Starting MCP server with transport: {args.transport}") if args.transport == "stdio": await mcp.run_stdio_async()