Skip to content

Database migrations

Pagelore's tables live in Postgres and are created and upgraded with Alembic. The app refuses to start until they exist, with a message that says what to run. It also refuses to start if Postgres can't be reached, and prints the URL with the password hidden.

Run them locally

From src/models/db_schemes/pagelore:

cd src/models/db_schemes/pagelore
cp alembic.ini.example alembic.ini
uv run alembic upgrade head

In alembic.ini, either:

  • set sqlalchemy.url, e.g. postgresql+psycopg://admin:admin@localhost:5432/pagelore; or
  • leave it empty, and the URL is built from the POSTGRES_* environment variables. They must be exported in the shell: Alembic does not read src/.env.

Run alembic upgrade head again after pulling a version with new migrations.

In Docker

The image migrates on every start. Its entrypoint runs alembic -c alembic.ini.example upgrade head (the URL comes from POSTGRES_*), then starts the app. A database that is already at head is left as it is.

Several replicas

alembic/env.py holds a Postgres advisory lock for the whole upgrade. When replicas start together (Kubernetes, docker compose --scale), the first one migrates and the others wait, then find the database at head. A failed migration is rolled back as a whole and the lock released.

To migrate once per deploy instead, for example in a Kubernetes Job or a Helm pre-upgrade hook, run the same image with this command, which replaces the entrypoint:

command: ["alembic", "-c", "alembic.ini.example", "upgrade", "head"]
workingDir: /app/src/models/db_schemes/pagelore

The migrations

Migration Adds
3eb119259721 The collections table: each collection's backend, model, dimension and distance
6def980d4ad6 The projects, documents and chunks tables
685150931b29 The collection each document was indexed into
b7c41e2d9a05 Full-text search: each chunk's tsvector and its GIN index, and each project's language
c91f3a6d2e48 Document sources, metadata, external ids and versions
d4e8a17b5c63 Parent chunks (small-to-big)
e5a2c94f1b37 The LLM-written chunk context
f7c2b9d41a86 API keys (Authentication)
a3d9e6c27f14 Background jobs: the jobs table, and the job that processed each document (Background jobs)

Writing a migration

After changing a table in src/models/db_schemes/pagelore/schemes/:

cd src/models/db_schemes/pagelore
uv run alembic revision --autogenerate -m "Add ..."

Read the generated file before committing it: autogenerate misses some changes (server defaults, some index types, data migrations). Then run alembic upgrade head, and the integration tests, which migrate a test database from scratch.