feat(ingestion): add Phase 1 database and application foundation

Add the scoped PostgreSQL/pgvector schema, independent ingestion settings and ports, embedding profile checks, and trusted administrator bootstrap.

Include repository contracts and guidance, organized provider-free and PostgreSQL tests, deployment setup, and byte-preserving migration attributes.

Validation: 163 tests passed; all 48 PostgreSQL cases passed with pgvector Python 0.5.0 and 0.3.6. No production schema or historical metadata was changed.
This commit is contained in:
amirhosein mazaheri
2026-10-05 16:38:28 +03:30
parent cb54a35c90
commit baf58da562
50 changed files with 4628 additions and 3 deletions
+61
View File
@@ -0,0 +1,61 @@
"""Alembic schema lifecycle and full embedding-profile compatibility."""
import json
import pytest
from sqlalchemy import text
from tests.support.database_rows import seed
from dataclasses import replace
from alembic import command
from app.infrastructure.database import CompatibilityError, check_compatibility
def test_empty_migration_tables_and_pinned_profile(migrated_db, settings):
engine, config = migrated_db
with engine.connect() as connection:
check_compatibility(connection, settings.embedding_profile)
tables = connection.execute(text("""
SELECT tablename FROM pg_tables WHERE schemaname=current_schema()
""")).scalars().all()
assert set(tables) == {
"alembic_version", "organizations", "users", "organization_settings",
"documents", "ingestion_jobs", "document_chunks", "audit_events",
}
for changed in (
replace(settings.embedding_profile, model="different-model"),
replace(settings.embedding_profile, dimensions=4),
replace(settings.embedding_profile, endpoint="https://other.invalid"),
replace(settings.embedding_profile, provider="other-provider"),
replace(settings.embedding_profile, preprocessing_version="v2"),
):
with pytest.raises(CompatibilityError):
check_compatibility(connection, changed)
config.attributes["ingestion_settings"] = settings.model_copy(
update={"embedding_dimensions": 4, "embedding_model": "changed"}
)
with engine.begin() as connection:
config.attributes["connection"] = connection
with pytest.raises(CompatibilityError):
command.upgrade(config, "head")
check_compatibility(connection, settings.embedding_profile)
def test_document_profile_mismatch(migrated_db, settings):
engine, _ = migrated_db
with engine.begin() as connection:
_, _, doc = seed(connection)
with pytest.raises(CompatibilityError):
check_compatibility(connection, settings.embedding_profile)
connection.execute(text("""
UPDATE documents SET processing_profile=CAST(:profile AS jsonb) WHERE id=:doc
"""), {"doc": doc, "profile": json.dumps(settings.processing_profile())})
check_compatibility(connection, settings.embedding_profile)
def test_initial_migration_round_trip(migrated_db, settings):
engine, config = migrated_db
with engine.begin() as connection:
config.attributes["connection"] = connection
command.downgrade(config, "base")
assert connection.execute(text("SELECT to_regclass('document_chunks')")).scalar_one() is None
command.upgrade(config, "head")
check_compatibility(connection, settings.embedding_profile)