Alembic Patterns
Scannednpx machina-cli add skill ruslan-korneev/python-backend-claude-plugins/alembic-patterns --openclawAlembic Patterns
Alembic migration patterns with automatic handling of problematic types.
Triggers
Use this skill when the user:
- Creates an Alembic migration
- Gets errors during migration
- Works with enum, array, nullable changes
Main principle: Automatic fixing
Alembic does not always correctly generate downgrade. The plugin automatically:
- Adds enum deletion in downgrade
- Warns about nullable changes without data migration
- Checks the order of FK operations
Enum types
More details: ${CLAUDE_PLUGIN_ROOT}/skills/alembic-patterns/references/enum-handling.md
Creating table with enum
status_type = sa.Enum("pending", "active", "inactive", name="status_type")
def upgrade() -> None:
op.create_table(
"orders",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("status", status_type, nullable=False),
sa.PrimaryKeyConstraint("id"),
)
def downgrade() -> None:
op.drop_table("orders")
status_type.drop(op.get_bind(), checkfirst=False) # IMPORTANT!
Adding column with enum
def upgrade() -> None:
status_type = sa.Enum("pending", "active", name="status_type")
status_type.create(op.get_bind(), checkfirst=True)
op.add_column("orders", sa.Column("status", status_type))
def downgrade() -> None:
op.drop_column("orders", "status")
# Delete only if enum is not used elsewhere
# sa.Enum(name="status_type").drop(op.get_bind(), checkfirst=True)
Nullable changes
# Changing NULL -> NOT NULL
def upgrade() -> None:
# First update data
op.execute("UPDATE users SET name = 'Unknown' WHERE name IS NULL")
# Then change constraint
op.alter_column("users", "name", nullable=False)
def downgrade() -> None:
op.alter_column("users", "name", nullable=True)
Foreign Keys
# Correct order in downgrade
def downgrade() -> None:
# First delete FK
op.drop_constraint("fk_orders_user_id", "orders", type_="foreignkey")
# Then table
op.drop_table("users")
Array types
# Changing array element type
def upgrade() -> None:
op.drop_column("users", "tags")
op.add_column("users", sa.Column("tags", sa.ARRAY(sa.String(100))))
def downgrade() -> None:
op.drop_column("users", "tags")
op.add_column("users", sa.Column("tags", sa.ARRAY(sa.String(50))))
Data migrations
def upgrade() -> None:
# Safe batch update for large tables
connection = op.get_bind()
connection.execute(
sa.text("""
UPDATE users
SET status = 'active'
WHERE status IS NULL
""")
)
Commands
# Create migration
alembic revision --autogenerate -m "description"
# Apply
alembic upgrade head
# Rollback
alembic downgrade -1
# Check current version
alembic current
# History
alembic history
Plugin commands
/migrate:create <message>— create migration with auto-fix/migrate:check [revision]— check migration for problems- Agent
migration-reviewer— full migration analysis
Source
git clone https://github.com/ruslan-korneev/python-backend-claude-plugins/blob/master/plugins/fastapi/skills/alembic-patterns/SKILL.mdView on GitHub Overview
Automates Alembic migration patterns to safely handle tricky types like enums, arrays, and nullable changes. This reduces downgrade errors by auto-fixing downgrade steps, warning about risky changes, and validating foreign key operation order.
How This Skill Works
The plugin's core principle is automatic fixing: downgrades are adjusted to drop enums, warn about nullable changes without data migrations, and ensure FK operations occur in safe order. It provides concrete examples for creating tables with enums, adding enum columns, and evolving array types.
When to Use It
- You are creating an Alembic migration
- You encounter errors during migration
- You work with enum types (creating or altering)
- You modify array types
- You change nullable constraints (nullable to not nullable)
Quick Start
- Step 1: alembic revision --autogenerate -m 'description'
- Step 2: alembic upgrade head
- Step 3: Run /migrate:check or Agent migration-reviewer to verify and fix issues
Best Practices
- Rely on automatic downgrade fixes for enums, FK order, and nullable changes.
- Always drop enum types in downgrade after creating or altering them.
- Plan nullable changes with data migrations when needed and warn about risky changes.
- Test both upgrade and downgrade paths on representative data.
- Use plugin commands (/migrate:check, migration-reviewer) to validate migrations early.
Example Use Cases
- Creating a table with an enum column (status_type) and a safe downgrade that drops the enum
- Adding an enum column to an existing table, with proper enum handling on downgrade
- Nullable changes: update NULLs before enforcing NOT NULL and provide a downgrade that reverts to nullable
- Downgrading foreign keys: drop the FK constraint before dropping the referenced table
- Changing an array column's element type (e.g., from String(50) to String(100))