Skip to content

Upgrades and migrations

This page explains how a Spongram server is upgraded: how versions are reported, how the SQL migration journal works, what to do the first time an older database meets it, in which order the two images are updated, and what a rollback does and does not undo. It applies to the server; the desktop edition has its own SQLite migration runner.

Versions

What Where it is defined How to read it on a running system
Spongram release __version__ in spongram_proxy/__init__.py The version field of GET /healthz; the start log line spongram <version> ready (…)
Graphiti image Tag of the spongram-graphiti image (GRAPHITI_TAG, default 1.1.3-spongram); the image label graphiti.core.version docker service inspect or docker inspect on the running container
Database schema Rows of the table spongram_schema_migrations SELECT name, applied_at FROM spongram_schema_migrations ORDER BY name;

After any deployment, compare the version reported by /healthz with the release you built. An orchestrator that rolled the update back keeps serving the previous version, and the only visible sign is that field.

The routine upgrade

  1. Read the release notes and back up Postgres and Neo4j (Backup and restore).
  2. Rebuild the spongram image from the new sources. Rebuild the spongram-graphiti image only if the release changes it.
  3. Deploy:
    • Compose: docker compose build && docker compose up -d. Compose stops the old container before starting the new one, so expect a short interruption.
    • Swarm: push the image and redeploy the stack. The service updates start-first: the new container must answer /readyz before the old one stops (Docker Swarm with Portainer).
    • Helm: helm upgrade. The migration Job runs first as a hook, then the Deployment rolls (AWS).
  4. Check /healthz (version), /readyz, and the migration lines in the log.

Pending migrations are applied by the spongram container when it starts, before the HTTP server. The log shows:

==> applying migrations
{"migration": "<file>.sql", "state": "applied"}
==> migrations applied

The migration journal

Migrations are the *.sql files of db/migrations/, copied into the image at /app/db/migrations and applied in name order. The table spongram_schema_migrations records each applied file with the SHA-256 digest of its content.

Rule Why
Each file runs once Older releases replayed every file at every start. Historical data changes must not run again: a replay could reset a setting an administrator had changed since
Each file runs in its own transaction, together with its journal row A failure leaves the database at the previous file, never half-way through one
A Postgres advisory lock is held for the whole run Two containers starting together cannot both apply migrations
Each file waits at most 5 seconds for its locks, then is retried, up to 8 times with increasing pauses During a start-first update the previous container is still serving. PostgreSQL queues new lock requests behind a waiting ALTER TABLE; an unbounded wait would freeze the live service behind the migration. A retry logs {"migration": "<file>", "state": "lock_busy", "attempt": n}
A recorded file whose content changed stops the start See below

Never edit a released migration

At every start the runner compares the digest of each recorded file with the file in the image. A difference, even in a comment or in whitespace, stops the start with:

==> migrations refused: An applied migration changed: <file>.sql. Nothing was changed; …

If you maintain your own migrations, correct a mistake with a new file. If you see this message after an upgrade, the image does not contain the files the database was migrated with: you built from modified sources, or from a different branch. Deploy the matching sources.

There are no down migrations. Schema changes are forward only.

First start on a database without a journal

The journal was introduced in release 1.18. A database created by an earlier release has the Spongram tables but no journal. The runner then refuses to guess what was applied:

==> migrations refused: Existing unjournalled schema: explicit --adopt-schema required (0033, 0045 or legacy). Nothing was changed; …

The refusal happens before any write. Tell the runner where the database comes from with SPONGRAM_ADOPT_SCHEMA (or --adopt-schema on the migration command):

Value For a database What happens
0033 last run by a 1.17 release The live schema is compared with the published baseline db/schema_baseline_0033.json. If identical, the journal is created with files 0001 to 0033 recorded as applied, without running them. Later files are then applied normally
legacy last run by a release older than 1.17 Files 0001 to 0033 are replayed one last time, as every start of those releases did, then the result is compared with the same baseline
0045 created by a preview of the Cloud control plane at that schema Same as 0033, against db/schema_baseline_0045.json

The comparison covers every spongram_* table: columns with type, nullability and default, constraints, indexes, triggers, row-level-security flags and policies. For all three values the replay, the comparison and the creation of the journal share one transaction. A refusal leaves the database exactly as it was:

==> migrations refused: Schema does not match the selected baseline; no migration applied. …

A mismatch means the schema was changed outside the migrations (a manual ALTER, an index added by hand). Return to the previous image, which still works since nothing was changed, and reconcile the schema on a copy of the database. Do not create or fill the journal by hand: the runner would then trust a schema it never checked.

SPONGRAM_ADOPT_SCHEMA is read only while the journal does not exist. It may stay set afterwards. A new, empty database needs no adoption.

Check the schema first, read-only

For 0033 and 0045 you can run the comparison without changing anything, against the database of the release still in service. scripts/schema_check.py prints a self-contained program; run that program anywhere SPONGRAM_DATABASE_URL and the asyncpg package are available, which is the case inside any spongram container:

uv run python scripts/schema_check.py 0033 | docker compose exec -T spongram python3 -

It opens a read-only session and prints IDENTICAL (exit code 0), or one line per difference (exit code 1), or JOURNAL ALREADY PRESENT when there is nothing to adopt. legacy cannot be checked this way, because its comparison happens after a replay.

A separate migration role and job

With the Cloud control plane the API does not run DDL. The overlays and the Helm chart set SPONGRAM_RUN_MIGRATIONS=false, the API connects with a role that owns no table, and migrations are run by a separate command with the schema-owner role:

python -m spongram_proxy.cloud.install migrate --runtime-role spongram_app
Item Detail
Connection SPONGRAM_MIGRATION_DATABASE_URL (a DSN or an aws-secretsmanager:// reference)
--runtime-role Role the API connects as, default spongram_app. The command refuses to run if that role does not exist, is a superuser, has BYPASSRLS, or is the role the command itself is connected as
--adopt-schema 0033, 0045 or legacy, once, as above
--migrations Directory of the files, default /app/db/migrations
After the journal Grants USAGE on the schema, SELECT, INSERT, UPDATE, DELETE on all tables and USAGE, SELECT on all sequences to the runtime role, and revokes its access to spongram_schema_migrations

Run it to completion before starting the new API. The Helm chart does this with a pre-install,pre-upgrade hook.

Two more subcommands exist:

  • diagnose prints the PostgreSQL version, the attributes of the connected role and row counts of a few tables as JSON. It exports no memory content, key or URL.
  • prepare-upgrade --secret-output <file> [--adopt-inference] prepares an existing server for the Cloud control plane. It writes the encryption key the installation effectively uses (the explicit SPONGRAM_ENCRYPTION_KEY, or the key derived from the admin token when none was set) to a new file with mode 0600, after checking that stored values decrypt with it. With --adopt-inference it marks the inference configuration already in the database as the configured one. Load that key into the new deployment before it starts.

On failure these commands print only the error class, because database errors can contain connection strings.

Enabling the Cloud control plane on an existing server

Plan a maintenance window. Stop every container of the previous release before starting one that enforces access rights; do not let the two overlap, even though routine updates on Swarm are start-first. Once brains are managed by the control plane, do not return to an image or a configuration that ignores those rights: the service refuses to start with SPONGRAM_CLOUD_CONTROL_ENABLED off in that state.

Order between the two images

The spongram image and the spongram-graphiti image are versioned separately.

  • A newer Graphiti image requires a Spongram release that knows it. Deploy spongram first. Spongram defines what clients see of the engine: it lists the tools and arguments it forwards, refuses the rest, rewrites the initialize instructions and normalises the engine's responses. An older Spongram in front of a newer engine would expose tools and arguments that bypass the ingestion queue and the per-brain isolation checks.
  • The reverse is safe. A newer Spongram works with the Graphiti image of the previous release.
  • Roll the Graphiti image by hand, never together with the rest through a stack webhook. The procedure is in Docker Swarm with Portainer.

When a release changes both images, the sequence is: deploy spongram, verify, then update graphiti-mcp, then align the stored stack definition.

Rolling back

Situation What to do
The new container never became ready Swarm rolls back by itself (failure_action: rollback). A refused migration changed nothing: the previous release keeps working on the same database
The new release serves but misbehaves, no migration was applied Redeploy the previous image tag
The new release applied migrations An image rollback does not revert them. Either move forward with a fix, or restore the Postgres backup taken before the upgrade and redeploy the previous image; memories written in between are then lost from Postgres and must be reconciled with the graph
A feature flag is the cause Turn the flag off (Feature flags); no rollback is needed
The Graphiti image Returning to an image built on an older Graphiti base is not guaranteed to be a plain image change: the newer engine can write properties on facts that the older one cannot read. Take a Neo4j backup before upgrading the engine, and rehearse the return on a copy

Database images are pinned by digest. Upgrading Postgres or Neo4j is a separate, deliberate operation: back up, change the digest, apply.

What upgrades never do: re-embed

The embedding dimension of an instance is fixed when the instance is created, and the graph's vectors have that dimension. An upgrade does not re-embed anything and does not need to. The consequences for planning:

  • Do not change the embedder or its dimension as part of an upgrade. It is a separate operation that requires purging every brain and ingesting the content again (Inference providers).
  • Moving a brain between deployments with export and import works only between instances of the same dimension; the import rejects a mismatch.
  • A restore must bring back Postgres and Neo4j from the same point in time, with the same embedder configured.