/py-pit:alembic-migrationsfrom py-pit
Establishes safe, disciplined database migration workflows using Alembic with SQLAlchemy 2.x. Prevents empty migrations, data loss from renames, and broken history.
Ships in py-pit. 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.
None: SKILL.md is the whole skill.
This skill establishes safe, disciplined database migration workflows using Alembic in SQLAlchemy 2.x projects.
Autogenerate output is treated as a draft, never final truth. All migrations must be reviewable, deterministic, and safe for production.
The skill prevents common failures such as:
These rules are always applied when generating or reviewing migrations.
Alembic only detects models that are imported at runtime.
A canonical module must import every ORM model so metadata is complete.
Do not assume app/. Use the project's existing Base definition --
typically at {pkg_name}/db/base.py (see sqlalchemy-models skill).
Example:
# {pkg_name}/models/__init__.py
from {pkg_name}.models.user import User
from {pkg_name}.models.order import Order
Alembic must reference:
from {pkg_name}.db.base import Base
target_metadata = Base.metadata
This prevents empty migrations, the most common Alembic failure.
Alembic cannot detect column renames.
If autogenerate emits:
drop_column
add_column
for the same table in one revision, treat it as a rename.
Replace with:
op.alter_column("table", "old_name", new_column_name="new_name")
This prevents silent data loss.
Never edit a migration that has already been applied.
Correct pattern:
revision A (applied)
revision B (fix)
Never:
edit revision A
Editing applied migrations breaks migration graphs across environments.
Every migration must succeed on a fresh database using:
upgrade head
downgrade base
upgrade head
This guarantees migrations are:
Use this skill when the user wants to:
Correct workflow:
edit models
↓
generate revision
↓
review migration
↓
fix migration
↓
run upgrade
Never:
generate → immediately run
Autogenerate output must be reviewed before execution.
Before generating anything:
alembic/, alembic.ini, and alembic/env.py already
exist. If they do, modify the existing configuration rather than
reinitializing. Never run alembic init if alembic/ already exists.Base is defined. If the project used the
sqlalchemy-models skill, it will be at {pkg_name}/db/base.py.{pkg_name}/models/__init__.py). This is the module env.py must import.{pkg_name}/{pkg_name}/ -- never assume app/.If no alembic/ directory exists, initialize:
alembic init alembic
Project layout after initialization:
project/
alembic/
env.py
versions/
alembic.ini
Leave sqlalchemy.url in alembic.ini blank or as a placeholder.
Provide the URL at runtime via the application's settings system.
Example in env.py:
from {pkg_name}.core.config import settings
DATABASE_URL = settings.database_url
This pattern defers to the settings-config skill and avoids embedding credentials in version-controlled files.
Provide a minimal complete env.py. Replace {pkg_name} with the
actual package name found in Step 0.
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool
from {pkg_name}.core.config import settings
from {pkg_name}.db.base import Base
import {pkg_name}.models # noqa: F401 — ensures all models are imported
config = context.config
config.set_main_option("sqlalchemy.url", settings.database_url)
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def run_migrations_offline() -> None:
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
Naming conventions: The naming convention is defined on Base in
db/base.py (see sqlalchemy-models skill) and is picked up
automatically by Alembic through target_metadata = Base.metadata.
Async note: Alembic migrations run synchronously even in async applications.
Create a migration revision:
alembic revision --autogenerate -m "add users table"
Migration files appear in:
alembic/versions/
Review the migration carefully before running it.
Check for:
Autogenerate is a starting point, not a final migration.
Apply migrations:
alembic upgrade head
Inspect state:
alembic current
View revision history:
alembic history
Carefully review migrations involving:
These operations require deliberate review.
Avoid:
This skill typically follows:
sqlalchemy-models — establishes Base, model package structure, and
the import module that env.py must referencesettings-config — establishes the settings object and
database_url that env.py uses for the connection stringAfter applying this skill the project will have:
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
16 skills in this plugin
/background-jobs-boundariesGuardrails for background work in FastAPI and Python services. Prevent misuse of in-process background execution (e.g. FastAPI BackgroundTasks)./click-cliDesign, generate, and improve Python CLIs using the Click library. Focus on idiomatic architecture, command groups, modular layouts, CLI UX conventions, and avoiding common Click anti-patterns./click-cli-linterAudit and improve existing Python Click CLIs by identifying architecture problems, anti-patterns, and CLI UX issues, then proposing minimal, high-value fixes./code-qualityOpinionated code health stack for Python repos using Ruff, pre-commit, and Markdown linting. Prefers consolidation over redundant tooling, preserves working repos unless migration is requested, and provides a simple adoption path for both new and existing projects./dockerize-serviceGenerate a local-development Docker Compose setup for an existing Python project. Produces a multi-stage Dockerfile, Compose file, env config, and dockerignore based on detected project signals./fastapi-errorsOpinionated error architecture for FastAPI services. Enforces a single internal exception hierarchy, constructor-based messages, consistent API error responses, and centralized logging for unexpected failures.Generated from the plugin's indexed repository (robhowley/py-pit-skills) — no native marketplace selected
npx claudepluginhub p/robhowley-py-pitOther plugins' skills for the same job