feat: Add database migration at start of MCP server (#1145)

<!-- .github/pull_request_template.md -->

## Description
<!-- Provide a clear description of the changes in this PR -->

## 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.
This commit is contained in:
Igor Ilic 2025-07-24 16:32:34 +02:00 committed by GitHub
parent dbdf04c089
commit fc8ae2fe21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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