db-migrations-and-schema-changes

19
1
Source

Workflows and commands for managing Alembic database migrations and schema changes in the letta-cloud core app, including using uv, just, LETTA_PG_URI, and switching between SQLite and Postgres.

Install

mkdir -p .claude/skills/db-migrations-and-schema-changes && curl -L -o skill.zip "https://mcp.directory/api/skills/download/1660" && unzip -o skill.zip -d .claude/skills/db-migrations-and-schema-changes && rm skill.zip

Installs to .claude/skills/db-migrations-and-schema-changes

About this skill

DB migrations and schema changes (letta-cloud core)

Use this skill whenever you need to change the database schema or debug Alembic migrations in apps/core of the letta-cloud repo.

This skill assumes:

  • Working directory: apps/core
  • Migrations: Alembic in apps/core/alembic
  • Python runner: uv
  • Helper: just ready for environment + DB setup

Quick start

  1. Ensure environment is ready:
    • just ready
  2. For Postgres migrations, set:
    • export LETTA_PG_URI=postgresql+pg8000://postgres:postgres@localhost:5432/letta-core
  3. Make your ORM/schema change.
  4. Autogenerate migration:
    • uv run alembic revision --autogenerate -m "<short_message>"
  5. Apply migration:
    • uv run alembic upgrade head

See references/migration-commands.md for exact commands and variants.

Standard workflows

1. Add or modify a column (ORM-first)

  1. Identify the ORM model and table.
  2. Update the SQLAlchemy model in letta/orm/...:
    • Prefer using mixins (e.g. ProjectMixin) when available instead of duplicating columns.
  3. Run just ready if dependencies or environment may have changed.
  4. Ensure LETTA_PG_URI is set if you want the migration to target Postgres.
  5. Autogenerate Alembic revision with uv.
  6. Inspect the generated file under alembic/versions/:
    • Confirm op.add_column / op.alter_column match expectations.
  7. Apply migrations with uv run alembic upgrade head.

Use this pattern for changes like adding project_id columns via ProjectMixin.

2. Data backfill / one-off data migration

  1. Make sure the schema change (if any) is already represented in ORM + Alembic.
  2. Create a new Alembic revision without autogenerate (or edit an autogen file) and add Python logic in upgrade() that:
    • Uses op.get_bind() and SQLAlchemy Core/SQL to backfill data.
  3. Keep downgrade() simple and safe (ideally reversible).
  4. Run against Postgres with LETTA_PG_URI set, using uv run alembic upgrade head.

3. Fixing a bad migration

Typical cases:

  • Migration fails only on SQLite (ALTER constraint limitations).
  • Migration was generated while pointing at SQLite instead of Postgres.

Workflow:

  1. Identify the failing revision in alembic/versions/.
  2. If failure is SQLite-specific, prefer running migrations against Postgres by exporting LETTA_PG_URI and re-running upgrade.
  3. If logic is wrong, create a new migration that fixes the problem rather than editing an applied revision (especially in shared environments).
  4. For purely local/dev history, you can delete and regenerate migrations but only if no one else depends on them.

See references/sqlite-vs-postgres-gotchas.md for SQLite-specific issues.

4. Switching between SQLite and Postgres

Alembic picks the engine based on letta.settings.DatabaseChoice and environment variables.

General rules:

  • For local dev stateful runs, just ready handles baseline migrations.
  • For schema design and production-like migrations, prefer Postgres and set LETTA_PG_URI.

Workflow for Postgres-targeted migration:

  1. export LETTA_PG_URI=postgresql+pg8000://postgres:postgres@localhost:5432/letta-core
  2. From apps/core:
    • uv run alembic upgrade head
    • uv run alembic revision --autogenerate -m "..."

5. Resetting local Postgres for clean migration generation

If your local Postgres database has drifted from main (e.g., applied migrations that no longer exist, or has stale schema), you can reset it to generate a clean migration.

From the repo root (/Users/sarahwooders/repos/letta-cloud):

# 1. Remove postgres data directory
rm -rf ./data/postgres

# 2. Stop the running postgres container
docker stop $(docker ps -q --filter ancestor=ankane/pgvector)

# 3. Restart services (creates fresh postgres)
just start-services

# 4. Wait a moment for postgres to be ready, then apply all migrations
cd apps/core
export LETTA_PG_URI=postgresql+pg8000://postgres:postgres@localhost:5432/letta-core
uv run alembic upgrade head

# 5. Now generate your new migration
uv run alembic revision --autogenerate -m "your migration message"

This ensures the migration is generated against a clean database state matching main, avoiding spurious diffs from local-only schema changes.

Troubleshooting

  • "Target database is not up to date" when autogenerating
    • First run uv run alembic upgrade head (with appropriate engine/URI).
  • SQLite NotImplementedError about ALTER CONSTRAINT
    • Switch to Postgres by setting LETTA_PG_URI and rerun.
  • Autogenerated migration missing expected changes
    • Ensure ORM imports and metadata (Base.metadata) are correct and that the changed model is imported in Alembic env context.
  • Autogenerated migration has unexpected drops/renames
    • Review model changes; consider explicit operations instead of relying on autogenerate. Reset local Postgres (see workflow 5) to get a clean baseline.

References

  • references/migration-commands.md — canonical commands for uv, Alembic, and just.
  • references/sqlite-vs-postgres-gotchas.md — engine-specific pitfalls and how to avoid them.

More by letta-ai

View all →

finding-agents

letta-ai

Find other agents on the same server. Use when the user asks about other agents, wants to migrate memory from another agent, or needs to find an agent by name or tags.

10

creating-skills

letta-ai

Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Letta Code's capabilities with specialized knowledge, workflows, or tool integrations.

20

adding-models

letta-ai

Guide for adding new LLM models to Letta Code. Use when the user wants to add support for a new model, needs to know valid model handles, or wants to update the model configuration. Covers models.json configuration, CI test matrix, and handle validation.

10

acquiring-skills

letta-ai

Guide for safely discovering and installing skills from external repositories. Use when a user asks for something where a specialized skill likely exists (browser testing, PDF processing, document generation, etc.) and you want to bootstrap your understanding rather than starting from scratch.

80

llm-provider-usage-statistics

letta-ai

Reference guide for token counting and prefix caching across LLM providers (OpenAI, Anthropic, Gemini). Use when debugging token counts or optimizing prefix caching.

00

converting-mcps-to-skills

letta-ai

Connect to MCP (Model Context Protocol) servers and create skills for repeated use. Load when a user wants to use an MCP server, connect to external tools via MCP, or when they mention MCP, model context protocol, or specific MCP servers.

00

You might also like

flutter-development

aj-geddes

Build beautiful cross-platform mobile apps with Flutter and Dart. Covers widgets, state management with Provider/BLoC, navigation, API integration, and material design.

219765

drawio-diagrams-enhanced

jgtolentino

Create professional draw.io (diagrams.net) diagrams in XML format (.drawio files) with integrated PMP/PMBOK methodologies, extensive visual asset libraries, and industry-standard professional templates. Use this skill when users ask to create flowcharts, swimlane diagrams, cross-functional flowcharts, org charts, network diagrams, UML diagrams, BPMN, project management diagrams (WBS, Gantt, PERT, RACI), risk matrices, stakeholder maps, or any other visual diagram in draw.io format. This skill includes access to custom shape libraries for icons, clipart, and professional symbols.

172398

godot

bfollington

This skill should be used when working on Godot Engine projects. It provides specialized knowledge of Godot's file formats (.gd, .tscn, .tres), architecture patterns (component-based, signal-driven, resource-based), common pitfalls, validation tools, code templates, and CLI workflows. The `godot` command is available for running the game, validating scripts, importing resources, and exporting builds. Use this skill for tasks involving Godot game development, debugging scene/resource files, implementing game systems, or creating new Godot components.

158268

nano-banana-pro

garg-aayush

Generate and edit images using Google's Nano Banana Pro (Gemini 3 Pro Image) API. Use when the user asks to generate, create, edit, modify, change, alter, or update images. Also use when user references an existing image file and asks to modify it in any way (e.g., "modify this image", "change the background", "replace X with Y"). Supports both text-to-image generation and image-to-image editing with configurable resolution (1K default, 2K, or 4K for high resolution). DO NOT read the image file first - use this skill directly with the --input-image parameter.

192225

ui-ux-pro-max

nextlevelbuilder

"UI/UX design intelligence. 50 styles, 21 palettes, 50 font pairings, 20 charts, 8 stacks (React, Next.js, Vue, Svelte, SwiftUI, React Native, Flutter, Tailwind). Actions: plan, build, create, design, implement, review, fix, improve, optimize, enhance, refactor, check UI/UX code. Projects: website, landing page, dashboard, admin panel, e-commerce, SaaS, portfolio, blog, mobile app, .html, .tsx, .vue, .svelte. Elements: button, modal, navbar, sidebar, card, table, form, chart. Styles: glassmorphism, claymorphism, minimalism, brutalism, neumorphism, bento grid, dark mode, responsive, skeuomorphism, flat design. Topics: color palette, accessibility, animation, layout, typography, font pairing, spacing, hover, shadow, gradient."

151185

fastapi-templates

wshobson

Create production-ready FastAPI projects with async patterns, dependency injection, and comprehensive error handling. Use when building new FastAPI applications or setting up backend API projects.

126166

Stay ahead of the MCP ecosystem

Get weekly updates on new skills and servers.