Back to Skills

sqlalchemy-alembic-expert-best-practices-code-review

SQLAlchemy ORM and Alembic migration best practices for building safe, performant database schemas. This skill should be used when writing, reviewing, or refactoring SQLAlchemy models, Alembic migrations, or database query patterns. Triggers on tasks involving SQLAlchemy ORM, Alembic migrations, database schema changes, or query optimization.

8starsUpdated 8/20/2026

Security Assessment

Safe(96/100)
Security Score96/100

About sqlalchemy-alembic-expert-best-practices-code-review

This skill is a curated set of SQLAlchemy ORM and Alembic migration best practices for building safe, performant database schemas. It solves a high-stakes problem: schema migrations that block reads and writes, cause table locks, or produce redundant and inefficient indexes in production databases (primarily PostgreSQL). It is meant to be consulted when writing, reviewing, or refactoring SQLAlchemy models, Alembic migrations, or query patterns.

The guidance is organized into eight rules across four priority tiers. Index management rules require creating indexes concurrently with autocommit blocks (postgresql_concurrently=True) and verifying that query patterns are actually backed by indexes. Constraint-safety rules split risky operations into non-blocking steps: create a unique index concurrently before attaching the unique constraint, add foreign keys and check constraints with NOT VALID first and validate them separately, and change column types through a multi-step add-backfill-swap-drop sequence instead of a locking ALTER. Optimization rules limit non-unique indexes to at most three columns and avoid indexes already covered by a composite index. Each rule file provides an impact level, a bad example, and a corrected implementation.

It targets backend and platform engineers, database-focused developers, and code reviewers who need reproducible, opinionated rules for zero-downtime migrations. This is a read-only, defensive code-review reference; while its examples contain SQL schema operations including column drops, they are illustrative migration best practices, not destructive automation, and there is no external network access or credential handling.

FAQ

What database does this guidance target?

Primarily PostgreSQL, using features such as postgresql_concurrently=True and NOT VALID constraint validation, with some rules (like safe column type changes) also tagged for MySQL. The focus is safe, non-blocking Alembic migrations.

How does it recommend adding an index without downtime?

Always create indexes concurrently inside an autocommit block using postgresql_concurrently=True, so index operations do not block reads and writes. It also advises verifying that your actual query patterns are covered by an appropriate index.

What is the safe way to change a column type?

Use a multi-step approach rather than a direct ALTER that rewrites the table: add a new column, write to both during a transition, backfill, move reads over, stop writing to the old column, then drop it. This avoids table locks and downtime.

How are foreign keys and check constraints added safely?

Add them with NOT VALID first so the operation does not scan and lock the whole table, then validate the constraint in a separate step. The same split approach applies to unique constraints, which are backed by a concurrently created unique index first.

Is there a limit on index width?

Yes. Non-unique indexes should be limited to a maximum of three columns for efficiency and reduced storage, choosing the most selective columns, and you should avoid single-column indexes already covered by a composite index.

All Files

9 files
rules/limit-non-unique-index.md0.9 KB
View
rules/unique-constraint.md1.1 KB
View
SKILL.md2.8 KB
View
rules/change-column-type.md1.6 KB
View
rules/ensure-index-not-covered.md1.2 KB
View
rules/only-concurrent-indexes.md1.0 KB
View
rules/split-foreign-key.md1.2 KB
View
rules/split-check-constraint.md1.0 KB
View
rules/verify-query-patterns-are-indexed.md1.3 KB
View

Install sqlalchemy-alembic-expert-best-practices-code-review

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill