/alembic-sqlalchemy:alembic-sqlalchemyfrom alembic-sqlalchemy
Best-practice, token-efficient workflow for Alembic migrations and SQLAlchemy 2.x ORM code. Use this skill whenever a task touches a database schema or its migrations: adding or changing a model, column, index, constraint or enum; running or fixing alembic revision, upgrade, downgrade, autogenerate, heads, merge, stamp or check; env.py, alembic.ini or [tool.alembic] config; data backfills; zero-downtime PostgreSQL changes; SQLite batch mode; SQLAlchemy models (DeclarativeBase, Mapped, mapped_column, relationship), sessions, async engines, connection pooling, or N+1 and lazy-load problems. Trigger even when the user only says "add a field", "update the model", "the migration failed", "two heads", or "why is this query slow" and never names Alembic or SQLAlchemy.
Ships in alembic-sqlalchemy. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
A wrong migration corrupts every environment it reaches, and versions/ grows without bound. This skill keeps schema changes safe and keeps the agent from reading what it does not need.
versions/:
bash <skill-dir>/scripts/migration_status.sh # --no-db when no database is reachable
Output: heads, the last five revisions in graph order, the database's current revision, pending revisions, and model/database drift.cat, ls or glob the whole versions/ directory. Ordering lives in the down_revision graph, not the listing, and old files never change.alembic check, alembic upgrade <from>:<to> --sql, the linter, and the tests in references/testing-ci.md.<skill-dir> is this skill's base directory, announced when the skill loads.
alembic revision, never by hand and never hand-numbered. Ids are random 12-hex and unique by construction; order comes from down_revision. Readable ordering goes in the filename via file_template. → references/revision-workflow.mdalembic heads and alembic check. → references/revision-workflow.mdsa.table() / sa.column() or op.execute(sa.text(...)). Schema and data changes live in separate revisions. → references/data-migrations.mdnaming_convention on MetaData. Without it downgrades and SQLite batch mode break. → references/env-config.mdautocommit_block(), lock and statement timeouts, no in-place renames. → references/postgres-zero-downtime.mdDeclarativeBase, Mapped[...], mapped_column(), select(); relationships default to lazy="raise"; eager loading is chosen per query. → references/sqlalchemy-orm.mdexpire_on_commit=False; pool sized deliberately; NullPool behind transaction-mode PgBouncer. → references/sqlalchemy-orm.mdcompare_server_default, include_object, empty-revision abort, transaction_per_migration, URL from the environment, date-prefixed file_template. → references/env-config.mdMapped[...] types, nullability and server_default are what autogenerate compares, so make them truthful.alembic revision --autogenerate -m "<imperative, 40 chars or fewer>". If no file appears, models and database already match; check that the model module is imported by the metadata's base module.python <skill-dir>/scripts/lint_revision.py --dialect postgresql migrations/versions/<new_file>.py
alembic upgrade head, then alembic check (must be clean), then the migration tests. Render with --sql when the exact DDL matters for review.alembic revision -m "backfill ..." without autogenerate, then references/data-migrations.md: lightweight table constructs, idempotent statements, batches committed inside autocommit_block(), --depends-on the schema revision. Large backfills belong in a job, with the revision documenting that.
references/revision-workflow.md, sections "More than one head" and "A failed upgrade". Never renumber, never edit an applied revision.
references/sqlalchemy-orm.md. Check relationships for lazy="raise", loader options per query, session scope, and the SQLAlchemy 2.1 checklist if the project is upgrading.
Lint every changed file under versions/, read only those files, render alembic upgrade <prev>:<head> --sql for the range, and walk the review procedure in references/autogenerate-review.md. Report findings with line numbers.
End migration tasks with this, so the reader does not need the transcript:
Migration: <path> (rev <id>, down_revision <id>)
Upgrade ops: <one line from the linter summary>
Risk flags: <locks | backfill | irreversible | enum | none>
Deploy order: <migrate-then-deploy | deploy-then-migrate | expand now, contract in a later release>
Verified: alembic upgrade head <ok/fail>, alembic check <ok/fail>, tests <ok/fail/not run>
| Read when | File |
|---|---|
| Creating, ordering, merging, repairing revisions; numbering questions | references/revision-workflow.md |
| After any autogenerate; reviewing a migration | references/autogenerate-review.md |
| env.py, alembic.ini / pyproject.toml, naming convention, async env | references/env-config.md |
| PostgreSQL on a live system: locks, backfills, indexes, enums, types | references/postgres-zero-downtime.md |
| SQLite batch mode, MySQL non-transactional DDL | references/dialect-notes.md |
| Data migrations, raw SQL in revisions | references/data-migrations.md |
| pytest-alembic, testcontainers, CI gates, running migrations in production | references/testing-ci.md |
| Models, queries, loading, sessions, async, pooling, 2.1 upgrade | references/sqlalchemy-orm.md |
Scripts: scripts/migration_status.sh (compact status, no file reads) and scripts/lint_revision.py (static checks plus an op summary; no database, no imports). Both are safe to run at any time.
references/autogenerate-review.md (opens in a new tab)references/data-migrations.md (opens in a new tab)references/dialect-notes.md (opens in a new tab)references/env-config.md (opens in a new tab)references/postgres-zero-downtime.md (opens in a new tab)references/revision-workflow.md (opens in a new tab)references/sqlalchemy-orm.md (opens in a new tab)references/testing-ci.md (opens in a new tab)scripts/lint_revision.py (opens in a new tab)scripts/migration_status.sh (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub amiable-dev/alembic-sqlalchemy --plugin alembic-sqlalchemy