py-migrate
Skill3 starsChanged 8 months agoMITView on GitHub
Use when creating or modifying database schema in Python projects using Alembic with SQLAlchemy 2.0 and PostgreSQL
In this repository36 other files it gives its agents.
- adrSkill
- analyticsSkill
- api-contractSkill
- api-designSkill
- ci-pipelineSkill
- claude-mdSkill
- code-qualitySkill
- data-modelSkill
- db-migrateSkill
- debugSkill
- deploySkill
- dep-updateSkill
- disk-cleanupSkill
- docker-buildSkill
- eng-leadSkill
- event-drivenSkill
- fullstack-healthcheckSkill
- go-featureSkill
- go-integration-testSkill
- go-refactorSkill
- go-scaffoldSkill
- incident-responseSkill
- interactive-clarifySkill
- observabilitySkill
- onboardingSkill
- product-specSkill
- py-featureSkill
- py-integration-testSkill
- py-refactorSkill
- py-scaffoldSkill
- react-featureSkill
- react-refactorSkill
- react-scaffoldSkill
- review-codeSkill
- securitySkill
- system-designSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 149
Not reviewing autogenerated migrations — always read the SQL
- Very specific: line 157
REQUIRED: Update CLAUDE.md if new migration commands are added (claude-md)
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Database Migrations (Alembic + SQLAlchemy)
Overview
Safe, reversible PostgreSQL schema changes using Alembic with SQLAlchemy 2.0 async. Models drive migrations via autogenerate.
Core principle: Models are the source of truth. Autogenerate from models, review every migration, test up/down cycle.
When to Use
- Adding or modifying SQLAlchemy models
- Creating new tables, columns, indexes, constraints
- Data migrations (backfills, transforms)
- Enum type changes
SQLAlchemy 2.0 Models
# src/infrastructure/postgres/models.py
from sqlalchemy import String, text
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from datetime import datetime
from uuid import uuid4
class Base(DeclarativeBase):
pass
class UserModel(Base):
__tablename__ = "users"
id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid4)
email: Mapped[str] = mapped_column(String, unique=True, nullable=False)
name: Mapped[str] = mapped_column(String, nullable=False)
phone: Mapped[str | None] = mapped_column(String, nullable=True)
metadata_: Mapped[dict] = mapped_column("metadata", JSONB, server_default=text("'{}'::jsonb"))
created_at: Mapped[datetime] = mapped_column(server_default=text("NOW()"))
updated_at: Mapped[datetime] = mapped_column(server_default=text("NOW()"))Alembic Workflow
# Create migration from model changes
alembic revision --autogenerate -m "add users table"
# Apply migrations
alembic upgrade head
# Rollback one step
alembic downgrade -1
# Show current revision
alembic current
# Show migration history
alembic historyAsync env.py Configuration
# alembic/env.py
from sqlalchemy.ext.asyncio import async_engine_from_config
import asyncio
def run_migrations_online() -> None:
connectable = async_engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
)
async def do_run():
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()
asyncio.run(do_run())Makefile Targets
migrate:
alembic upgrade head
migrate-down:
alembic downgrade -1
migrate-create:
alembic revision --autogenerate -m "$(msg)"
migrate-history:
alembic historySafe DDL Patterns
Same zero-downtime rules as Go migrations:
| Operation | Safe Approach |
| Add column | nullable=True first → backfill → add constraint |
| Drop column | Remove from model → deploy → migration to drop |
| Rename column | Add new → copy → update code → drop old |
| Add index | op.create_index(..., if_not_exists=True) |
Enum Handling
# In migration
import sqlalchemy as sa
user_role = sa.Enum('admin', 'member', 'viewer', name='user_role')
def upgrade() -> None:
user_role.create(op.get_bind(), checkfirst=True)
op.add_column('users', sa.Column('role', user_role, nullable=False, server_default='member'))
def downgrade() -> None:
op.drop_column('users', 'role')
user_role.drop(op.get_bind(), checkfirst=True)Data Migrations
def upgrade() -> None:
# Schema change
op.add_column('users', sa.Column('full_name', sa.String))
# Data migration
op.execute("UPDATE users SET full_name = first_name || ' ' || last_name")
# Then enforce constraint
op.alter_column('users', 'full_name', nullable=False)Verification
alembic upgrade head && alembic downgrade -1 && alembic upgrade headCommon Mistakes
- Not reviewing autogenerated migrations — always read the SQL
- Missing
Mapped[]type annotations — breaks autogenerate detection - Using
nullable=Falsewithoutserver_defaulton existing tables - Forgetting async engine config in
env.py - Enum types not dropped in downgrade
Chains
- REQUIRED: Update CLAUDE.md if new migration commands are added (
claude-md)
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

