mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-14 09:46:03 +00:00
fix(persistence): repair missing mcp readme column
This commit is contained in:
@@ -11,6 +11,7 @@ from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from alembic.script import ScriptDirectory
|
||||
from sqlalchemy import inspect, text
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
from langbot.pkg.entity.persistence.base import Base
|
||||
@@ -147,6 +148,41 @@ class TestSQLiteMigrationUpgrade:
|
||||
rev2 = await get_alembic_current(sqlite_engine)
|
||||
assert rev2 == rev1, f"Expected {rev1}, got {rev2}"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_upgrade_repairs_head_stamped_mcp_readme_column(self, sqlite_engine):
|
||||
"""
|
||||
A database may already be stamped at the previous head while missing a
|
||||
column added by an earlier guarded migration. Upgrade should still
|
||||
repair mcp_servers.readme so startup ORM queries do not fail.
|
||||
"""
|
||||
async with sqlite_engine.begin() as conn:
|
||||
await conn.execute(
|
||||
text(
|
||||
"""
|
||||
CREATE TABLE mcp_servers (
|
||||
uuid VARCHAR(255) NOT NULL PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
enable BOOLEAN NOT NULL,
|
||||
mode VARCHAR(255) NOT NULL,
|
||||
extra_args JSON NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
|
||||
)
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
await run_alembic_stamp(sqlite_engine, '7b2c1d9e4f30')
|
||||
await run_alembic_upgrade(sqlite_engine, 'head')
|
||||
|
||||
async with sqlite_engine.connect() as conn:
|
||||
columns = await conn.run_sync(
|
||||
lambda sync_conn: {column['name'] for column in inspect(sync_conn).get_columns('mcp_servers')}
|
||||
)
|
||||
|
||||
assert 'readme' in columns
|
||||
assert await get_alembic_current(sqlite_engine) == alembic_head_revision()
|
||||
|
||||
|
||||
class TestSQLiteMigrationFreshDatabase:
|
||||
"""Tests for fresh database workflow."""
|
||||
|
||||
@@ -15,11 +15,14 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
import pytest
|
||||
from alembic.config import Config
|
||||
from alembic.script import ScriptDirectory
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
from sqlalchemy import text
|
||||
|
||||
from langbot.pkg.entity.persistence.base import Base
|
||||
from langbot.pkg.persistence.alembic_runner import (
|
||||
_ALEMBIC_DIR,
|
||||
run_alembic_upgrade,
|
||||
run_alembic_stamp,
|
||||
get_alembic_current,
|
||||
@@ -29,6 +32,13 @@ from langbot.pkg.persistence.alembic_runner import (
|
||||
pytestmark = [pytest.mark.integration, pytest.mark.slow]
|
||||
|
||||
|
||||
def alembic_head_revision() -> str:
|
||||
"""Return the repository's current Alembic head revision."""
|
||||
cfg = Config()
|
||||
cfg.set_main_option('script_location', _ALEMBIC_DIR)
|
||||
return ScriptDirectory.from_config(cfg).get_current_head()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def postgres_url():
|
||||
"""Get PostgreSQL URL from environment."""
|
||||
@@ -150,8 +160,7 @@ class TestPostgreSQLMigrationUpgrade:
|
||||
# Verify revision
|
||||
rev = await get_alembic_current(postgres_engine)
|
||||
assert rev is not None, "Expected a revision after upgrade"
|
||||
# Head should be the latest migration (0004 for current state)
|
||||
assert rev.startswith('0004'), f"Expected head to be 0004_*, got {rev}"
|
||||
assert rev == alembic_head_revision()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_postgres_upgrade_idempotent(
|
||||
@@ -214,4 +223,4 @@ class TestPostgreSQLMigrationGetCurrent:
|
||||
await run_alembic_stamp(postgres_engine, '0001_baseline')
|
||||
|
||||
rev = await get_alembic_current(postgres_engine)
|
||||
assert rev == '0001_baseline'
|
||||
assert rev == '0001_baseline'
|
||||
|
||||
Reference in New Issue
Block a user