alembic

Configures Alembic migrations on SQLAlchemy 2.x across SQLite, PostgreSQL, and MySQL.

1|1|Updated May 24, 2026
One-click install
npx skills add https://github.com/bm629/agent-skills --skill alembic-bm629
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: alembic
Source: https://github.com/bm629/agent-skills/tree/main/skills/alembic
Command: npx skills add https://github.com/bm629/agent-skills --skill alembic-bm629

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires alembic, sqlalchemy, and includes references (resource) components.

What problem does it solve? Managing schema changes across multiple database dialects is error-prone: SQLite cannot run most ALTER statements, Alembic autogenerate silently misreads renames as drop-and-add (destroying data), and teams lack a versioned, reversible migration history. This Skill wires Alembic to an existing SQLAlchemy 2.x Base.metadata and enforces the practices that keep one migration history correct on SQLite, PostgreSQL, and MySQL. ## Core Features & Use Cases - Environment wiring: Connects env.py and alembic.ini to your models' Base.metadata, sources the DB URL from the environment, and enables render_as_batch plus a naming_convention so constraints survive SQLite table recreation. - Safe revision workflow: Covers revision, autogenerate, mandatory review of generated scripts (renames, server_defaults, CHECK constraints are known blind spots), and true-inverse downgrade functions. - Multi-dialect batch migrations: Writes alter operations in batch_alter_table (move-and-copy) style so the same script emits plain ALTER on PostgreSQL/MySQL and table recreation on SQLite. - Branching and adoption: Resolves multiple heads with alembic merge and adopts Alembic on an existing create_all-built database via alembic stamp baseline. - Use Case: You shipped an app using Base.metadata.create_all() and now need to add a column that must deploy to PostgreSQL in production and SQLite in tests. This Skill stamps a baseline on the existing database, autogenerates the change, rewrites it in batch style, and applies it with a reversible upgrade/downgrade pair. ## Quick Start Use the alembic skill to wire Alembic to my SQLAlchemy models and write a reviewed batch-style migration that adds a priority column to the jobs table on both SQLite and PostgreSQL.

Frequently Asked Questions about alembic

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write Alembic migrations that work on SQLite and PostgreSQL?

Write alter operations inside op.batch_alter_table blocks and set render_as_batch=True in env.py. SQLite then uses move-and-copy table recreation while PostgreSQL and MySQL emit ordinary ALTER statements from the same code.

How do I connect Alembic autogenerate to my SQLAlchemy models?

Set target_metadata = Base.metadata in env.py and import the module defining your models so every table registers on the metadata. Also source the database URL from the environment with config.set_main_option rather than hardcoding it in alembic.ini.

Why does Alembic autogenerate miss my schema changes?

Autogenerate has documented blind spots: renames appear as drop-plus-add, most server_default changes are missed, CHECK and some named constraints are undetected, and tables not imported into target_metadata are invisible. Always review and hand-edit generated scripts before applying them.

Can I adopt Alembic on a database already created with create_all?

Yes. Author a baseline migration matching the current schema, then run alembic stamp head on the existing database to record it as applied without re-executing CREATE TABLE statements. All later changes become normal revisions applied with upgrade.

How do I fix the multiple heads error in Alembic?

Run alembic heads to list the divergent revision branches, then join them with alembic merge -m "message" rev_a rev_b. The merge revision records both parents and applies no schema change, after which alembic upgrade head works again.

Does Alembic work with async SQLAlchemy engines?

Yes, but migrations are commonly run synchronously even for async apps by pointing Alembic at a sync driver URL. If an async engine is required, env.py uses async_engine_from_config and awaits connection.run_sync(do_run_migrations) to bridge into Alembic's sync context.