Alembic: autogenerate, the migration graph, and what SQLite batch mode really does
A database migrations tool for SQLAlchemy.
At a glance
- What is it?
- Alembic is the migration tool written by the author of SQLAlchemy, and its value rests on autogenerate and a dependency graph rather than on a single linear chain of scripts. The parts that need reading twice are the offline SQL mode and the batch move-and-copy workflow.
- Who is it for?
- Adopt Alembic if your application already speaks SQLAlchemy 2.0 and your schema changes more than once a month, since autogenerate and the revision graph pay for themselves there. Do not adopt it for a project that talks to its database through a raw DBAPI, or for a SQLite-only toy where batch mode is all you will ever use.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 2 days ago.
- What is it written in?
- Mainly Python, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 24, 2026, and from our analysis. They are not legal advice.
Star history
- sqlalchemy/alembic4,486 ★ · +75
| Repository | 2026-09-23 | 2026-11-24 |
|---|---|---|
| sqlalchemy/alembic | 4.4k | 4.5k |
75 Stars in the last 62 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
Autogenerate diffs live database inspection against your Python models
The core idea is a comparison, not a generator. Pass `--autogenerate` and Alembic asks SQLAlchemy's schema inspection layer what the database currently contains, loads the state of your models as expressed in Python, and renders the difference between the two as Python directives in a new script. Table and column level changes come out of this, and constraints and indexes follow.
What lands in the file is a starting point, not a finished migration. The script is written for a human to edit, adding extra directives and data movement by hand. That framing matters more than it first appears, because the feature is named as though it produces migrations and it does not. It produces a diff of what the inspection layer can see.
The audience is an application team whose schema lives in Python classes and whose production database is not edited by hand. If your DDL arrives from a DBA team as reviewable SQL text, the same comparison still runs, but the output target changes and so does the workflow, which is covered later.
UUID revisions and down_revision markers turn the script chain into a graph
Each migration file gets a UUID identifier rather than an incrementing integer, and the link to its parent is a human-editable marker inside the file itself. Alembic treats the set of files as a directed acyclic graph, so a revision can depend on any arbitrary set of other revisions, or on none at all. Branches, multiple roots and mergepoints all fall out of that structure, and the commands exist to create each of them automatically.
This is the piece that distinguishes Alembic from a counter. A counter forces one path through history, which means two teams merging branches either serialize or hand-edit numbers. A graph lets both branches exist and later get merged at a revision that names both parents.
The cost of that freedom is that the graph is only as honest as the hand-written markers. The README calls the configuration open ended and transparent, and it is, but the transparency cuts both ways: the tool shows you the DAG you wrote, not the one you meant.
Transactional DDL covers PostgreSQL and SQL Server and leaves MySQL and SQLite outside
The default templates put every migration inside a transaction. For databases that support transactional DDL, which the README names as PostgreSQL and Microsoft SQL Server, a failed migration can be undone without manual cleanup. That is a real property of those two engines, not of Alembic, and it is easy to read the README's phrasing as though the tool were providing it.
On an engine without transactional DDL, a half-applied migration stays half-applied. The script aborts, the connection drops, and whatever the engine already committed is committed.
SQLite gets its own separate answer rather than a transaction. It cannot alter most things in place at all, so a rename or a column type change becomes a copy of the whole table. That is what batch mode automates, and it is the reason the README frames the SQLite support as making the most of a bad situation rather than as a solved problem.
Batch mode turns a rename into a move and copy
In batch migrations, several changes aimed at one table are collected and then applied as a single subsequent move-and-copy workflow. The new table shape is built, rows are transferred, and the old table is dropped in one pass. The README is explicit that the workflow is not SQLite-only: you can use it on any database if you want to recreate a table in the background on a busy system.
That second use is the interesting one. The surrounding script stays readable because the individual operations are still expressed as rename or column change operations.
The direction of travel is the reverse of what SQLite forces on you. SQLite users adopt batch mode because they have no choice. Larger databases adopt it deliberately, which means the same feature serves a user who cannot avoid it and a user who is optimising for it, and the documentation does not distinguish the two cases.
Offline mode writes SQL to a file and forbids in-memory SELECTs
Alembic's commands are oriented so that a series of migrations can run into a textual output file as easily as they run against a database. The stated reason is organisational: in a corporate environment, direct DDL privileges on a production database are rare, and DBAs want SQL scripts they can read before anything executes.
The catch sits two sentences later in the same README section. Other operations that rely on in-memory SELECTs of rows must not be invoked in this mode, because there is no connection to select from. The tool offers helper constructs such as `bulk_insert()` for data-oriented work that fits a script-only run.
So the section titled full support for SQL script migrations is describing a mode with a documented hole in it. Autogenerate is the obvious casualty, since generating a comparison requires reading the database. Any migration you want to review as SQL has to be written with that restriction in mind from the start.
Installing means matching Python, SQLAlchemy and the console script in pyproject.toml
The README gives no install command. It points at https://alembic.sqlalchemy.org/ for documentation and status, and the packaging metadata in pyproject.toml carries the rest: the floor is Python 3.10, the ORM dependency is SQLAlchemy 2.0 or later, and the command line entry point is `alembic`, bound to `alembic.config:main`. The declared runtime dependencies also include Mako for template rendering and typing-extensions.
Those four lines tell you most of what a first install needs to get right.
requires-python = ">=3.10"
dependencies = [
"SQLAlchemy>=2.0",
"Mako",
"typing-extensions>=4.12",
]The console script is what you invoke once it is installed, and it resolves to the config main function rather than to a shell wrapper of your own.
[project.scripts]
alembic = "alembic.config:main"For a first real use, the sequence the README describes is: generate an environment from the templates, customise the emitted scripts that establish connectivity, let `--autogenerate` write candidate migrations into a new script, then edit that file to produce a finished migration. The generated environment is code you own from that point, which is why the README spends as much space on how templates are customised as on the commands themselves.
Why autogenerate cannot finish a migration, stated by the project itself
Two claims in the README pull against each other. Autogenerate is said to eliminate the initial grunt work of generating new migration directives from an altered schema, and in the same sentence the document says that real world migrations are far more complex than what can be automatically determined.
The second claim is the operative one, and the numbers behind it are worth naming plainly. Alembic's own goal list counts three capabilities it says it can detect: tables, columns, and then constraints and indexes with the word as well attached, which reads like a promise of equal coverage rather than a note that detection there is weaker. The consequence is concrete. A migration that moves data, splits a column, or rewrites a value in a constraint is invisible to the diff, so the file Alembic hands you can be complete and wrong at the same time. Reviewing the generated script is not an optional habit here; the tool has no way to flag the directives it could not infer.
Maintenance is current rather than abandoned: the last push landed on 2026-10-30, releases 1.19.2, 1.20.0 and 1.20.1 landed between 2026-09-04 and 2026-10-22, the repository is not archived, and the project ships under the MIT license.
Against a single linear chain, and against raw SQL migration files
The realistic comparison is not another ORM migration tool but the two things Alembic replaces. The first is a counter-based script chain: numbered files applied in order, where a second branch is a merge problem for the team rather than a data structure for the tool. Alembic's UUID revisions and the parent markers inside each file make branching a first-class operation instead of something you fight.
The second is hand-written SQL applied by a DBA. That approach gives you total control over every statement and no diffing at all, and it is still the right answer when the schema owner is the database team and the Python models are a downstream copy rather than the source of truth. Alembic wins there only because it can inspect and compare; if nobody trusts the models, the comparison is noise.
The README makes the point that the ALTER constructs build on SQLAlchemy's DDLElement base and can be used by any application or script, so you can take the vocabulary without taking the tool.
Editorial conclusion
Adopt Alembic if your application already speaks SQLAlchemy 2.0 and your schema changes more than once a month, since autogenerate and the revision graph pay for themselves there. Do not adopt it for a project that talks to its database through a raw DBAPI, or for a SQLite-only toy where batch mode is all you will ever use. Before anything else, run one autogenerate against a database you can afford to discard, then read the generated script line by line against what you actually meant to change, and confirm that the revision markers in your versions directory form the graph you think they form.
Frequently asked questions
What is Alembic used for?
It emits ALTER statements to change table structure, and it holds migration scripts that upgrade a database to a new version and optionally downgrade it by reversing the same steps. The scripts run in sequence, or in a dependency graph you define.
How do I use Alembic with SQLAlchemy?
Run Alembic with the `--autogenerate` option so it inspects the current database state through SQLAlchemy's schema inspection and compares it to the state your models declare. It writes the difference into a new migration script as Python directives for you to edit.
How do I install Alembic for Python?
The packaging metadata requires Python 3.10 or later and SQLAlchemy 2.0 or later, with Mako and typing-extensions also declared as dependencies. Installing the package puts an `alembic` console script on your path, bound to `alembic.config:main`. The README points at https://alembic.sqlalchemy.org/ for documentation.
How do I use Alembic in Python?
An environment is generated from templates that deposit scripts defining how database connectivity is established and how migration scripts are invoked. Each new migration file is then generated from a template in that series and is yours to customise afterwards.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/sqlalchemy-alembic)Star history chart for your README
[](https://hysenlabs.com/projects/sqlalchemy-alembic)Was this useful?