Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(persistence): loosen PostgreSQL columns at startup so Docker upgrades need no Alembic step (#3068)
Raghav Chamadiya69c29edc9529
diff --git a/packages/core/src/repowise/core/persistence/database.py b/packages/core/src/repowise/core/persistence/database.pyindex 0687d60ea..9351077a6 100644--- a/packages/core/src/repowise/core/persistence/database.py+++ b/packages/core/src/repowise/core/persistence/database.py@@ -18,7 +18,7 @@ from contextlib import asynccontextmanagerfrom pathlib import Pathimport structlog-from sqlalchemy import event, inspect, literal+from sqlalchemy import String, event, inspect, literalfrom sqlalchemy.ext.asyncio import (AsyncEngine,AsyncSession,@@ -478,6 +478,40 @@ def _relax_not_null(connection: object, table: object) -> None:log.info("schema_table_rebuilt_for_nullable", table=name)+def _is_widened(model_type: object, live_type: object) -> bool:+ """True when the model's string column is longer than the live one."""+ if not isinstance(model_type, String) or not isinstance(live_type, String):+ return False+ live_length = live_type.length+ if live_length is None:+ return False+ return model_type.length is None or model_type.length > live_length+++def _loosen_postgres_columns(table: object, live: dict[str, dict], dialect: object) -> list:+ """``(label, ALTER)`` pairs bringing live PostgreSQL columns up to the model.++ Only ever loosens: drops a NOT NULL the model no longer declares and widens+ a VARCHAR the model made longer or unbounded. Both are catalog-only changes+ on PostgreSQL and match what migrations 0062, 0064, 0086 and 0094 do, so a+ database that never ran Alembic (the Docker image's path) still accepts+ what the current code writes. Tightening stays an explicit migration.+ """+ statements = []+ for column in table.columns: # type: ignore[attr-defined]+ found = live.get(column.name)+ if found is None:+ continue+ what = f"{table.name}.{column.name}:loosen" # type: ignore[attr-defined]+ target = f'ALTER TABLE "{table.name}" ALTER COLUMN "{column.name}"' # type: ignore[attr-defined]+ if _is_widened(column.type, found["type"]):+ kind = column.type.compile(dialect=dialect)+ statements.append((what, text(f"{target} TYPE {kind}")))+ if column.nullable and not found["nullable"]:+ statements.append((what, text(f"{target} DROP NOT NULL")))+ return statements++def _reconcile_schema(connection: object) -> None:"""Bring an existing database up to ``Base.metadata`` (additive only).@@ -495,11 +529,16 @@ def _reconcile_schema(connection: object) -> None:that follows the additive-only convention is picked up automaticallyon the next ``init_db`` call — no per-migration code required here.+ Existing columns are only ever loosened: on PostgreSQL a NOT NULL the+ model dropped is dropped and a VARCHAR the model widened is widened in+ place; on SQLite the tables in ``_SQLITE_REBUILD_FOR_NULLABLE`` are rebuilt+ for nullability, and VARCHAR length is not enforced there.+Limitations (intentional — these need explicit migrations):- * column **removals**, **renames**, or **type changes** are NOT- reconciled (SQLite can't ALTER COLUMN safely anyway);- * **constraint changes** (UNIQUE, CHECK, FK) on existing columns- are NOT reconciled;+ * column **removals**, **renames**, and narrowing or cross-kind **type+ changes** are NOT reconciled (SQLite can't ALTER COLUMN safely anyway);+ * **constraint changes** (UNIQUE, CHECK, FK) and new NOT NULLs on+ existing columns are NOT reconciled;* Postgres extensions / functions (e.g. pgvector) are NOT createdhere — those still belong in Alembic migrations.@@ -566,7 +605,10 @@ def _reconcile_schema(connection: object) -> None:# nullability. We deliberately do NOT enforce FK constraints on# back-filled columns: SQLite can't add an enforced FK after the# fact, and write-time enforcement is sufficient for our purposes.- db_cols = {c["name"] for c in inspector.get_columns(table.name)}+ db_cols = {c["name"]: c for c in inspector.get_columns(table.name)}+ if dialect.name == "postgresql": # type: ignore[attr-defined]+ for what, statement in _loosen_postgres_columns(table, db_cols, dialect):+ _run(what, lambda statement=statement: statement)for column in table.columns:if column.name in db_cols:continuediff --git a/tests/integration/test_postgres_init_db_loosens_columns.py b/tests/integration/test_postgres_init_db_loosens_columns.pynew file mode 100644index 000000000..1be38cd11--- /dev/null+++ b/tests/integration/test_postgres_init_db_loosens_columns.py@@ -0,0 +1,123 @@+"""PostgreSQL regression for issue #3063: ``init_db`` loosens columns the model+loosened, so a database that never runs Alembic still accepts what the code+writes.++The Docker image starts the API with ``init_db`` and no ``alembic upgrade``.+Migration 0094 made ``health_file_metrics.score``, ``max_ccn`` and+``max_nesting`` nullable on PostgreSQL, so a database left at 0093 rejected+the NULL score v0.55 stores for a file health cannot analyse, and the sync+failed. SQLite never hit this: ``init_db`` rebuilds that table there.++Skipped unless a PostgreSQL URL with pgvector is configured::++ REPOWISE_TEST_PG_URL=postgresql+asyncpg://user@localhost:5432/repowise_test \\+ uv run pytest tests/integration/test_postgres_init_db_loosens_columns.py+"""++from __future__ import annotations++import asyncio+import os+from pathlib import Path+from unittest.mock import patch++import pytest+from alembic import command+from alembic.config import Config+from sqlalchemy import inspect, text+from sqlalchemy.ext.asyncio import create_async_engine++from repowise.core.persistence.database import init_db+from repowise.core.persistence.models import Base++PG_URL = os.environ.get("REPOWISE_TEST_PG_URL")++pytestmark = pytest.mark.skipif(+ not PG_URL, reason="REPOWISE_TEST_PG_URL not set: PostgreSQL-only regression"+)++CORE_ROOT = Path(__file__).resolve().parents[2] / "packages" / "core"+++def _columns(table: str) -> dict[str, dict]:+ async def read() -> dict[str, dict]:+ engine = create_async_engine(PG_URL or "")+ async with engine.connect() as conn:+ cols = await conn.run_sync(lambda c: inspect(c).get_columns(table))+ await engine.dispose()+ return {c["name"]: c for c in cols}++ return asyncio.run(read())+++def _execute(*statements: str) -> None:+ async def run() -> None:+ engine = create_async_engine(PG_URL or "")+ async with engine.begin() as conn:+ for statement in statements:+ await conn.execute(text(statement))+ await engine.dispose()++ asyncio.run(run())+++def _init_db() -> None:+ async def run() -> None:+ engine = create_async_engine(PG_URL or "")+ await init_db(engine)+ await engine.dispose()++ asyncio.run(run())+++def empty_pg() -> None:+ # Everything is dropped, so refuse a database not named like a scratch one.+ database = (PG_URL or "").rsplit("/", 1)[-1].split("?")[0]+ if "test" not in database and "scratch" not in database:+ pytest.skip(f"refusing to drop tables in {database!r}: name it *test* or *scratch*")++ async def drop() -> None:+ engine = create_async_engine(PG_URL or "")+ async with engine.begin() as conn:+ await conn.run_sync(Base.metadata.drop_all)+ await conn.execute(text("DROP TABLE IF EXISTS alembic_version"))+ await engine.dispose()++ asyncio.run(drop())+++def test_database_at_0093_accepts_unscored_health_rows(+ empty_pg: None, monkeypatch: pytest.MonkeyPatch+) -> None:+ """A database migrated to 0093 is NOT NULL on the score columns until+ init_db runs, and nullable after it, with no Alembic step."""+ monkeypatch.setenv("DATABASE_URL", PG_URL or "")+ monkeypatch.chdir(CORE_ROOT)+ # env.py's fileConfig() resets stdlib logging, which breaks caplog later.+ with patch("logging.config.fileConfig"):+ command.upgrade(Config("alembic.ini"), "0093")+ before = _columns("health_file_metrics")+ assert not any(before[name]["nullable"] for name in ("score", "max_ccn", "max_nesting"))++ _init_db()++ after = _columns("health_file_metrics")+ assert all(after[name]["nullable"] for name in ("score", "max_ccn", "max_nesting"))+ _init_db() # idempotent once loosened+++def test_init_db_loosens_without_alembic(empty_pg: None) -> None:+ """The stores Docker creates never see Alembic: a NOT NULL the model dropped+ and a VARCHAR the model widened are both brought up to the model."""+ _init_db()+ _execute(+ "UPDATE dead_code_findings SET lines = 0 WHERE lines IS NULL",+ "ALTER TABLE dead_code_findings ALTER COLUMN lines SET NOT NULL",+ "ALTER TABLE git_function_blame ALTER COLUMN symbol_id TYPE VARCHAR(512)",+ )++ _init_db()++ assert _columns("dead_code_findings")["lines"]["nullable"]+ assert _columns("git_function_blame")["symbol_id"]["type"].length is Nonediff --git a/website/self-hosting.md b/website/self-hosting.mdindex 26466e0a8..53ef66305 100644--- a/website/self-hosting.md+++ b/website/self-hosting.md@@ -235,11 +235,13 @@ server {## Database migrations-repowise uses Alembic for PostgreSQL schema management. Migrations run automatically at startup. To run them manually:+On startup the API brings the schema up to the running version on both SQLite and PostgreSQL: it creates missing tables, adds missing columns and indexes, drops NOT NULL from columns that became optional, and widens text columns that got longer. The Docker image needs no separate migration step for an upgrade.++PostgreSQL schema history is also kept as Alembic migrations, which add what startup does not: the pgvector `embedding` column and the full-text search index. On a database that Alembic created, apply them from a source checkout:```bash-cd packages/server-alembic upgrade head+cd packages/core+DATABASE_URL=postgresql://user:pass@host/dbname alembic upgrade head```---