Skip to content
OpenSmartRoute
Skillv1.0.0

alembic

Manage database migrations with Alembic. Use when a user asks to version database schemas, create migration scripts, handle schema changes in production, or manage SQLAlchemy model migrations.

by terminalskills(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from terminalskills/skills (skills/alembic/SKILL.md). Install upstream with npx skills add terminalskills/skills --skill alembic. Copyright stays with the author (Apache-2.0).

Alembic

Overview

Alembic is the migration tool for SQLAlchemy. It tracks database schema changes as versioned Python scripts — like Git for your database. Supports autogeneration from model changes, branching, and data migrations.

Instructions

Step 1: Setup

pip install alembic
alembic init alembic
# alembic/env.py — Configure with async SQLAlchemy
from alembic import context
from sqlalchemy.ext.asyncio import create_async_engine
from models import Base
import asyncio

config = context.config
target_metadata = Base.metadata

def run_migrations_online():
    connectable = create_async_engine(config.get_main_option("sqlalchemy.url"))

    async def do_run():
        async with connectable.connect() as connection:
            await connection.run_sync(do_migrations)

    def do_migrations(connection):
        context.configure(connection=connection, target_metadata=target_metadata)
        with context.begin_transaction():
            context.run_migrations()

    asyncio.run(do_run())

run_migrations_online()

Step 2: Create Migrations

# Auto-generate from model changes
alembic revision --autogenerate -m "add projects table"

# Create empty migration (for data migrations)
alembic revision -m "backfill user roles"
# alembic/versions/001_add_projects.py — Generated migration
def upgrade():
    op.create_table('projects',
        sa.Column('id', sa.String(36), primary_key=True),
        sa.Column('name', sa.String(100), nullable=False),
        sa.Column('owner_id', sa.String(36), sa.ForeignKey('users.id')),
        sa.Column('created_at', sa.DateTime, server_default=sa.func.now()),
    )
    op.create_index('ix_projects_owner_id', 'projects', ['owner_id'])

def downgrade():
    op.drop_index('ix_projects_owner_id')
    op.drop_table('projects')

Step 3: Data Migrations

# alembic/versions/002_backfill_roles.py — Data migration
from alembic import op
import sqlalchemy as sa

def upgrade():
    # Add column
    op.add_column('users', sa.Column('role', sa.String(20), server_default='member'))

    # Backfill existing rows
    conn = op.get_bind()
    conn.execute(sa.text("UPDATE users SET role = 'admin' WHERE email LIKE '%@mycompany.com'"))

def downgrade():
    op.drop_column('users', 'role')

Step 4: Commands

alembic upgrade head          # apply all pending migrations
alembic downgrade -1          # rollback one migration
alembic history               # show migration history
alembic current               # show current revision
alembic upgrade +1            # apply next migration only

Guidelines

  • Always review autogenerated migrations — they may miss renames (detected as drop+create).
  • Run migrations in CI before deploying — catch schema issues early.
  • Data migrations should be idempotent — safe to run multiple times.
  • Use op.batch_alter_table() for SQLite (which doesn't support ALTER TABLE well).
  • Never edit applied migrations — create new ones instead.

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/terminalskills-skills-alembic/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

terminalskills-skills-alembic.ocm.jsonjson
{
  "ocm": "1",
  "id": "terminalskills-skills-alembic",
  "kind": "skill",
  "name": "alembic",
  "description": "Manage database migrations with Alembic. Use when a user asks to version database schemas, create migration scripts, handle schema changes in production, or manage SQLAlchemy model migrations.",
  "publisher": "terminalskills",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "general"
    ],
    "tags": [
      "skill-md",
      "alembic",
      "migrations",
      "sqlalchemy",
      "database",
      "schema",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "Manage database migrations with Alembic. Use when a user asks to version database schemas, create migration scripts, handle schema changes in production, or manage SQLAlchemy model migrations."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/terminalskills/skills",
      "path": "skills/alembic/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/terminalskills/skills/blob/HEAD/skills/alembic/SKILL.md",
      "key": "terminalskills/skills/skills/alembic/SKILL.md"
    },
    "compatibility": "Python 3.8+, PostgreSQL, MySQL, SQLite",
    "license": "Apache-2.0"
  },
  "instructions": "# Alembic\n\n## Overview\n\nAlembic is the migration tool for SQLAlchemy. It tracks database schema changes as versioned Python scripts — like Git for your database. Supports autogeneration from model changes, branching, and data migrations.\n\n## Instructions\n\n### Step 1: Setup\n\n```bash\npip install alembic\nalembic init alembic\n```\n\n```python\n# alembic/env.py — Configure with async SQLAlchemy\nfrom alembic import context\nfrom sqlalchemy.ext.asyncio import create_async_engine\nfrom models import Base\nimport asyncio\n\nconfig = context.config\ntarget_metadata = Base.metadata\n\ndef run_migrations_online():\n ",
  "cost": {
    "context_tokens": 756
  }
}

Fetch it by URL: GET /api/v1/registry/terminalskills-skills-alembic/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.