Skip to main content

Build

Cognee Migrations

CogneeOfficialCertified

Use when dealing with cognee database migrations — understanding when they run automatically, checking or repairing migration state with cognee-cli upgrade/downgrade/stamp/current, a write blocked by a failed migration, authoring a new Alembic (relational schema) revision or a graph/vector data migration, or moving data between systems (relational DB import, memory export/import).

  • New
  • v1.0.3
  • Reviewed Sep 30
  • No reviews yet

Capabilities

  • They run by themselves

    A fresh database is built by running the whole chain (no stamping)

  • Check and repair

  • A write is blocked

    If a dataset's data migration failed, writes to that dataset are…

  • Pitfalls

    Lockstep Guard" fails otherwise

Getting started

Full instructions

Example command

A new Alembic revision

uv run alembic revision -m "add foo to data"

Instructions: SKILL.mdv1.0.3

Database migrations

cognee has two migration chains, run together:

Chain Changes Lives in Revision stored in
Relational schema (Alembic) Tables and columns of the relational DB (users, datasets, ACLs, pipeline runs, …) cognee/alembic/ (alembic.ini is in cognee/) alembic_version table
Graph/vector data Cross-store data rewrites (re-keying node ids, adding graph columns) across graph DB, vector DB and relational ledger cognee/modules/migrations/ (registry.py, versions/) Per dataset: dataset_database.migration_revision (access control on). Globally: global_database_version.global_migration_revision (access control off)

Use it

They run by themselves

run_migrations() applies the relational chain first, then the data chain.
It runs:

  • at API server startup (and in the Docker entrypoint.sh before gunicorn);
  • on the first write in an SDK or CLI process (remember, add, cognify,
    improve, memory imports), once per process;
  • when you call await cognee.run_migrations().

A fresh database is built by running the whole chain (no stamping). At
head, the first run in each process still does a no-op Alembic upgrade plus
a scan of the per-database revision rows; later calls in the same process
are skipped by an in-memory flag. ENABLE_AUTO_MIGRATIONS=false
turns off all automatic runs; then run cognee-cli upgrade yourself.

Concurrent processes are serialized by a migration lock: a Postgres advisory
lock (works across hosts) or a file lock next to the SQLite DB (one host
only).

Check and repair

bash
cognee-cli current                   # stamped revision per database, and the last failure
cognee-cli history                   # the data chain, newest first
cognee-cli upgrade                   # relational to head, then data chain to head
cognee-cli upgrade <slug>            # data chain up to and including <slug>
cognee-cli upgrade --alembic <rev>   # pin the relational target
cognee-cli downgrade <slug|base> [--dataset UUID ...] [--alembic REV] [--force]
cognee-cli stamp <head|base|slug> [--dataset UUID ...] [--force]
  • The positional revision is always a data-chain slug; relational
    targets go through --alembic.
  • downgrade rewrites data and asks for confirmation. It only reverts
    spans where every migration defines a down(), and leaves the relational
    schema alone unless you pass --alembic.
  • stamp changes only the stored data-chain revision, without running
    anything. Use stamp base --dataset <id> and then upgrade when a
    database's data drifted from its stamp (for example after restoring a
    backup); the chain is idempotent and converges it.
  • upgrade runs even with ENABLE_AUTO_MIGRATIONS=false.

A write is blocked

If a dataset's data migration failed, writes to that dataset are refused
until it succeeds (with access control off, any failure blocks all writes).
The server still starts. Run cognee-cli current to see the error, fix the
cause, then cognee-cli upgrade. A failed run is retried on the next start
or write.

Moving data between systems (not schema migrations)

Goal Use
Turn an existing relational database into a graph migrate_relational_database(graph_db, schema) (cognee/tasks/ingestion/migrate_relational_database.py), with the source DB set by MIGRATION_DB_PROVIDER / _PATH / _NAME / _HOST / _PORT / _USERNAME / _PASSWORD. Examples: examples/demos/ingestion_and_migration/
Back up a dataset, or move it to another cognee instance A COGX archive, see below
Export a dataset's graph for other tools await cognee.export(dataset, format=...): "json", "graphml" or "cypher" write a file (one way: cognee can't import them back); "pydantic" (default) returns typed DataPoint objects in memory
Import from another memory system (Mem0, Zep/Graphiti, Letta, LangMem) Build a MemorySource (cognee/modules/migration/sources/) and pass it to await cognee.remember(source, dataset_name=...)

There is no tool that moves a whole deployment from one database backend to
another.

COGX archives

COGX (Cognee eXchange, cognee/modules/migration/cogx.py) is cognee's
portable memory format and the only export format cognee can import back.
An archive is a directory with a manifest.json (COGX version, source
system, the dataset's data-migration revision) and one JSONL file per record
kind (documents, episodes, entities, facts, memories,
memory_blocks), plus nodes.jsonl with the raw graph nodes. The Mem0,
Zep, Letta and LangMem importers also translate into COGX records first.

Since COGX 0.2 every record also carries an optional OMP header, the fields
proposed to the Open Memory Protocol exchange group: origin (first system
and id, kept across copies), content_hash (sha256 of the body, for dedupe;
distinct from an integrity checksum), subject, semantic_kind (fact,
decision, commitment, procedure, preference, observation, other),
valid_from/valid_to with invalidated_by, evidence[] (role, ref,
confidence), confidence (outcome, verified_at, verified_by, score),
version, content_type (the body's format), tags, a record checksum
stamped by the writer, and provenance as contributors[] (role, actor,
kind, at) with the accountable human and ai_involvement. The
cognee exporter fills what it can read from the graph and declares the rest
in the manifest's coverage map. The loader keeps the header as one
omp_header JSON in a data item's external metadata, on Entity nodes and on
fact edges, so a re-export preserves it; a chunk re-exports with its
document's header. A record with no origin originates at its own
external_system/external_id, so a Mem0 memory stays Mem0's.
0.1 archives read unchanged.

Use it to back up and restore a dataset, or to copy one to another cognee
instance:

python
from cognee.migration import COGXArchiveSource

await cognee.export("my_dataset", format="cogx", destination="backup_cogx")
await cognee.remember(COGXArchiveSource("backup_cogx"), dataset_name="my_dataset")
  • A restore defaults to mode="preserve": the archived graph is written
    back as-is, with no LLM calls. mode="hybrid" also re-cognifies the raw
    content; mode="re-derive" ignores the archived graph and extracts again
    (costs LLM tokens).
  • cognee.push() / cognee-cli push does the same to Cognee Cloud: it
    exports to COGX, packs it as a .cogx.tar.gz and uploads it, and the
    receiving instance restores it (preserve mode unless you pass mode=).
  • export(..., include_permissions=True) also writes permissions.json
    with the dataset owner and ACL grants, including password hashes, so
    the restore recreates working accounts. Treat that archive as a secret.
  • An archive written by a newer major COGX version is rejected with a
    ValueError; upgrade cognee on the importing side.

Pitfalls

  • Never regenerate cognee/alembic/frozen_schema.py. It is the
    certified base schema the initial revision builds from, pinned by
    cognee/tests/unit/test_frozen_schema_seal.py. Schema changes ship as
    new revisions at head.
  • Never hand-type an Alembic revision id. Hand-typed patterns
    (a1b2c3d4e5f6, …) already collided with a downstream chain that vendors
    this one (b2c3d4e5f6a7). Generate ids with alembic revision.
  • Never rename, remove, or reorder a data-chain entry. The slug is what
    deployed databases store; an unknown stored slug disables the chain for
    that database.
  • A model change and its migration land together. CI's "Migration/Model
    Lockstep Guard" fails otherwise.
  • The relational schema cannot be downgraded below the data-bookkeeping
    revisions
    unless the data chain goes to base in the same call.
  • alembic.ini is always the packaged one; COGNEE_ALEMBIC_PATH or
    --alembic-path only changes the scripts directory (for vendored chains).

How it works

run_migrations() (cognee/modules/migrations/startup.py) takes the
migration lock, decides fresh vs existing (a users or alembic_version
table exists), runs Alembic in-process on a worker thread, then walks the
data chain per database with runner.run_database_migrations, stamping
after every step. After the chain, it syncs vector-adapter storage when the
recorded cognee_version differs from the library's
(versions/adapter_storage_migration.py, not a chain entry).

  • Relational: cognee/alembic.ini, cognee/alembic/env.py,
    cognee/alembic/versions/, cognee/alembic/frozen_schema.py
  • Data chain: cognee/modules/migrations/ (README.md is the authoring
    contract; registry.py, migration.py, runner.py, startup.py,
    versions/)
  • CLI: cognee/cli/commands/migrate_command.py

Extending it

A new Alembic revision

bash
cd cognee                              # the directory with alembic.ini
uv run alembic revision -m "add foo to data"

The DB URL comes from the live relational engine, so your normal .env
settings apply. Follow the recent revisions (for example
versions/e7f9a1c3d5b8_add_data_dataset_created_index.py):

  • Idempotent and guarded: inspect first (sa.inspect(op.get_bind()))
    and skip when the table is missing or the column/index already exists.
  • Branch on dialect (conn.dialect.name == "postgresql") for
    Postgres-only SQL. For Postgres enums use postgresql.ENUM(..., create_type=False) and create the type up front with checkfirst.
  • SQLite cannot drop or alter columns in place: use
    op.batch_alter_table(...).
  • Indexes: plain CREATE INDEX IF NOT EXISTS inside the migration
    transaction, not CONCURRENTLY (which releases the version-row lock and
    lets concurrent workers into the same build). Repair invalid Postgres
    indexes via pg_index.indisvalid.
  • A new model module outside the usual import path must be imported in
    cognee/alembic/env.py, or autogenerate will not see it.

A new data migration

Read cognee/modules/migrations/README.md first. In short:

  1. Write a module in cognee/modules/migrations/versions/ with
    async def migrate(context) (and optionally async def downgrade(context));
    step 2 registers them as up= / down=.
  2. Append Migration(slug=..., cognee_version=..., up=..., down_revision=<previous slug>, down=...)
    to MIGRATIONS in registry.py. The chain is validated at import
    (linear, unique slugs).
  3. Make it idempotent and cheap on empty stores, and crash-safe: re-key
    derived stores (vectors, ledger) first and rename in the graph last.
  4. Freeze private copies of any logic you depend on; never import live
    models that may change later.

Tests

  • cognee/tests/e2e/migrations/test_migration_model_lockstep.py (+
    schema_baseline.json): the lockstep CI job.
  • cognee/tests/unit/test_run_migrations.py: single Alembic head, startup
    behaviour.
  • cognee/tests/unit/test_frozen_schema_seal.py: frozen schema fingerprint.
  • cognee/tests/unit/modules/migrations/: data-chain unit tests.
  • cognee/tests/migrations/test_migration_lockstep.py: data chain against
    real stores (seed, down, up, verify).

Explore more skills

Browse all
This skill doesn’t include stateful context yet, instructions only. Learn about stateful skills.

Security

Loading security scan...

Reviews (0)

More from Cognee

View source →
self-hosted-companionChat with a companion that knows your notes folder (a journal, an Obsidian vault, any .md or .txt files) and every earlier chat, using cognee memory with local databases. Use when someone wants to ask questions about their own notes, or to…0 installsgoogle-workspace-memoryRemember a user's Google Calendar, Gmail and Drive in one cognee memory and answer questions across them, by default preparing their client meetings of the coming week (which orders are late or at risk, why, and what to say). Use when…0 installscompany-brain-follow-up-agentTurn your latest Granola call into next steps (owner, team, due date, and the Linear issue that already tracks each one) and post them to Slack, using cognee memory of your calls, Linear issues and Gmail inbox. Use when someone asks for…0 installsdocstring-criticReview a docstring-author patch in the Cognee pilot. Independently verify truthfulness and promotional tone, preserve supported positive framing, and write the claim ledger. Pass 2; corrects claims without expanding coverage.0 installscognee-permissionsUse when working with cognee's users, permissions, and multi-tenancy — creating users, tenants and roles, sharing datasets (read/write/delete/share grants), acting as a specific user in the SDK or authenticating over HTTP (login, API…0 installsdocstring-authorWrite or improve Python docstrings on the ten files in pilot_files.md, making supported Cognee benefits and connections between capabilities explicit. Pass 1 of the docstring pilot; a separate critic verifies the patch and writes the claim…0 installs

Frequently asked questions about cognee-migrations

What does the cognee-migrations skill do?

Use when dealing with cognee database migrations — understanding when they run automatically, checking or repairing migration state with cognee-cli upgrade/downgrade/stamp/current, a write blocked by a failed migration, authoring a new Alembic (relational schema) revision or a graph/vector data migration, or moving data between systems (relational DB import, memory export/import). It's a reusable SKILL.md instruction set that loads into your AI coding assistant on demand, no prompt engineering, no copy-pasting every session.

How do I install the cognee-migrations skill?

Run `npx @skills-hub-ai/cli install cognee-migrations` from your terminal. The CLI writes the SKILL.md to the correct location for your AI tool (e.g. ~/.claude/skills/cognee-migrations/ for Claude Code or ~/.cursor/skills/ for Cursor with --target cursor) and adds it to your project's .skills.json lockfile.

Which AI tools does cognee-migrations work with?

cognee-migrations runs in Claude Code. It follows the open Agent Skills standard (SKILL.md), so the same skill works in every supported tool without modification.

Is the cognee-migrations skill free?

Yes. Every skill on skills-hub.ai is free and open-source. There are no premium tiers, paywalls, or usage limits. You only pay for whatever AI assistant you're already using.

How do I use cognee-migrations after installing it?

In Claude Code, type `/cognee-migrations` (or whatever slash command the skill registers) and the AI follows the skill's instructions immediately. You can also reference it by name in natural language, your AI loads the skill into context when relevant.

Can I share the cognee-migrations skill with my team?

Yes. Commit your project's .skills.json lockfile and teammates run `npx @skills-hub-ai/cli install` (no args) to install every skill at the exact version you pinned. Organization-scoped installs work via skills-hub.ai organizations.

Badge for your README

Show that this skill is on Skills Hub. The badge links back to this page and shows the live install count.

Install on Skills Hub badge preview
Markdown
[![Install on Skills Hub](https://skills-hub.ai/api/badges/skill/cognee-migrations)](https://skills-hub.ai/skills/cognee-migrations)
HTML
<a href="https://skills-hub.ai/skills/cognee-migrations"><img src="https://skills-hub.ai/api/badges/skill/cognee-migrations" alt="Install on Skills Hub"></a>

More options in the badge docs.