Skip to content
Back to skills

Alembic Knowledge Patch

ASecurity

Alembic

  • 25 stars
  • 0 votes
  • 0 copies
  • 5 views
  • Added September 5, 2026
developmentpythonsqlexpressdatabasebackend

Works with

  • cli

Security analysis

A100/100

Pro scans all 6 files and shows the line behind each finding

Scanned December 1, 2026

npx -y skills add Nevaberry/nevaberry-plugins --skill alembic-knowledge-patch --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Alembic Knowledge Patch?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Alembic Knowledge Patch
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/nevaberry-alembic-knowledge-patch-nevaberry-plugins/badge)](https://www.skillsdirectory.com/skills/nevaberry-alembic-knowledge-patch-nevaberry-plugins)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: alembic-knowledge-patch
description: Alembic
version: "1.20.1"
license: MIT
metadata:
  author: Nevaberry
---


# Alembic Migration Guidance

Load this skill when configuring migration environments, adapting revision
scripts, reviewing autogenerate output, checking migration heads, or implementing
CLI and migration extensions. Use the topic references for detailed behavior,
backend conditions, examples, and version-specific requirements.

## Reference index

| Reference | Topics |
| --- | --- |
| [Compatibility and configuration](references/compatibility-and-configuration.md) | Runtime requirements, yanked packages, TOML and INI configuration, paths, revision templates, timestamps, post-write hooks |
| [Autogenerate and rendering](references/autogenerate-and-rendering.md) | CHECK comparison, reflection, indexes, defaults, computed expressions, type rendering, rewriters |
| [Operations and DDL](references/operations-and-ddl.md) | Removed keywords, conditional and inline DDL, batch recreation, backend operations, SQL execution, transactions |
| [Revisions, CLI, and extensions](references/revisions-cli-and-extensions.md) | Heads, IDs, ranges, dependencies, revision environments, version tables, custom commands, plugins |

## Breaking changes and deprecations

### Runtime and installation requirements

Alembic 1.17.0 requires Python 3.10 or later. Alembic 1.20.0 requires
SQLAlchemy 2.0.0 or later. Building Alembic from source requires setuptools
77.0.3 or later for PEP 639 license metadata, starting in 1.16.0.

The 1.15.0 wheel omitted migration templates and was yanked; use 1.15.1 or later
in the 1.15 series. The yanked 1.5.0 package also failed to install its templates
directory; a pin in that series should use 1.5.1 or a later 1.5.x release.

### Removed operation keyword aliases

Alembic 1.5.0 removes these legacy keyword aliases. Update keyword calls in
existing revisions with the following replacements.

| Operation | Old keyword → replacement |
| --- | --- |
| Constraint operations | `name` → `constraint_name` |
| `drop_constraint()` | `type` → `type_` |
| `create_primary_key()` | `cols` → `columns` |
| `create_unique_constraint()` | `source` → `table_name`; `local_cols` → `columns` |
| `create_check_constraint()` | `source` → `table_name` |
| `create_foreign_key()` | `source` → `source_table`; `referent` → `referent_table` |
| Index operations | `name` → `index_name` |
| `drop_index()` | `tablename` → `table_name` |
| `create_table()`, `drop_table()` | `name` → `table_name` |
| `alter_column()` | Rename keyword `name` → `new_column_name` |

The `referent_table` replacement also applies in batch mode.

### Custom comparison callbacks

Since 1.4.0, type comparison uses generated DDL and stops calling the type-level
`compare_against_backend` hook. Move custom comparisons to
`context.configure(compare_type=compare_custom_types)` in `env.py`, with
`compare_custom_types` as the environment's comparison callback.

Since 1.11.0, the rendered metadata default passed to a custom
`compare_server_default` comparator lacks Alembic's extra SQL quoting. Adjust
callbacks and third-party dialect implementations that depended on those quotes.

### Deprecated parsing, targets, and plugin entry points

- Set `path_separator = os` in `[alembic]` to split both `version_locations`
  and `prepend_sys_path` on `os.pathsep`, preserving Windows drive-letter colons.
  Omitting it uses deprecated parsing: `version_locations` uses
  `version_path_separator` when present; `prepend_sys_path` splits on spaces,
  commas, and colons.
- An unqualified relative target such as `alembic downgrade -1` emits a
  deprecation warning when multiple heads make it ambiguous.
- Point each `alembic.plugins` entry at a single plugin module. Alembic 1.20.1
  loads that form successfully and emits a deprecation warning for entries
  pointing to a collection of modules.

### CHECK comparison is opt-in

In 1.19.2, name-based CHECK comparison became opt-in under
`alembic.ext.checkconstraint_byname`. The previous
`alembic.autogenerate.checkconstraint_byname` name remains accepted for explicit
inclusion or `~` exclusion. Use a client-side naming convention that names all
constraints, including checks from `Boolean` and `Enum` with
`create_constraint=True`, which now participate in name matching.

```python
context.configure(
    connection=connection,
    target_metadata=target_metadata,
    autogenerate_plugins=[
        "alembic.autogenerate.*",
        "alembic.ext.checkconstraint_byname",
    ],
)
```

## Configuration and revision generation

### TOML source configuration

Use `alembic init -t pyproject migrations` for TOML migration paths and
post-write hooks. `%(here)s` expands to the directory containing
`pyproject.toml`. Keep database and logging settings in `alembic.ini`, or supply
both in `env.py` to use TOML alone.

```toml
[tool.alembic]
script_location = "%(here)s/migrations"
version_locations = ["%(here)s/migrations/versions"]
prepend_sys_path = ["%(here)s"]
```

Since 1.16.3, `alembic init -t pyproject_async migrations` scaffolds an async
environment with TOML source configuration. TOML booleans for
`recursive_version_locations` and `sourceless` require 1.16.4 or later;
integer `truncate_slug_length` requires 1.16.5 or later.

### Revision directories and hooks

Alembic creates subdirectories named in `file_template`. Enable recursive
discovery so later migration commands find the generated files.

```ini
[alembic]
file_template = %%(year)d/%%(month).2d/%%(day).2d_%%(rev)s_%%(slug)s
recursive_version_locations = true
```

A `module` post-write hook runs a tool with `python -m` using Alembic's
interpreter, starting in 1.16.3. For a `console_scripts` hook that needs a
particular working directory, set `<hook>.cwd`.

## Checking and manipulating revisions

Since 1.17.1, `alembic current --check-heads` exits nonzero if the database is
missing any head revision. The Python call
`alembic.command.current(config, check_heads=True)` raises `DatabaseNotAtHead`
in that case.

Since 1.18.5, `ScriptDirectory.get_heads(consider_depends_on=True)` excludes
heads referenced by another revision's `depends_on`, matching the revisions
expected in `alembic_version` after applying all upgrades.

Use `alembic merge --splice -m "merge branches" a1b2c3 d4e5f6` to merge when
one or more inputs are non-head revisions, starting in 1.18.5. The Python
equivalent is `command.merge(config, ["a1b2c3", "d4e5f6"], splice=True)`.

Alembic rejects custom revision IDs containing `-`, `@`, `+`, or `:` because
those characters are part of revision target syntax; `:` separates range
endpoints. Use an ID such as `REV_1` with `--rev-id`. An abbreviated ID must be
an unambiguous prefix of at least four characters; pass a shorter custom ID in
full. See the revisions reference for when these restrictions were introduced.

## Reviewing generated migrations

### Constraint names and type-generated checks

Autogenerate wraps reflected constraint and index names in `op.f(...)`,
preserving database names even with a `%(constraint_name)s` convention.
In 1.20.0, every generated `op.drop_constraint(None, ...)` gets a console warning
and a preceding comment. The drop requires a concrete name: name the constraint
in the model before generation or resolve its name in the migration.

With SQLAlchemy 2.1, autogenerated `op.create_table()` calls rely on types
such as `Boolean(create_constraint=True)` and
`Enum(..., create_constraint=True)` to emit their CHECK constraints, starting
in Alembic 1.20.1. Earlier Alembic versions could also render an explicit
`sa.CheckConstraint()` for the same check, causing duplicate-constraint errors.

Since 1.20.1, autogenerate preserves dialect options such as
`postgresql_not_valid` on foreign keys and CHECK constraints inside
`op.create_table()`, and on standalone `op.create_check_constraint()` calls.
For `op.create_table()`, SQLAlchemy must also be at least 2.0.55 in the 2.0
series or 2.1.5 in the 2.1 series.

### Rewriters and expressions

A rewriter can set `AlterColumnOp.modified_name` to render a column rename,
starting in 1.15.2. `Rewriter.chain()` supports more than two rewriters and
ordinary `process_revision_directives` callbacks, starting in 1.13.1;
register the resulting chain in `env.py` to run the processors in order.

Autogenerated `ExecuteSQLOp` rendering accepts a plain SQL string; passing a
SQLAlchemy expression such as `sa.text(...)` raises `NotImplementedError`.
The operation honors `alembic_module_prefix` when inserted by a custom rewriter.

Autogenerate skips SQLite expression indexes and warns; write their changes
explicitly. On backends with computed column reflection and SQLAlchemy 1.3.16
or later, it warns when a `Computed` expression differs from the database;
write the column recreation explicitly. PostgreSQL reflected casts such as
`::regconfig` and `::text` are stripped before comparison in 1.20.1, suppressing
the warning for otherwise equivalent computed expressions.

## Column DDL and batch recreation

On supporting backends, `add_column(if_not_exists=True)`,
`drop_column(if_exists=True)`, and `drop_constraint(if_exists=True)` emit
conditional DDL. Autogenerate renders these flags when a `Rewriter` sets them
on the corresponding operation.

Since 1.18.2, `op.add_column(..., inline_references=True)` emits the foreign key
reference within `ADD COLUMN` on PostgreSQL, Oracle, MySQL 5.7+, and MariaDB
10.5+. Since 1.18.5, it also includes options such as `ON DELETE` and `ON UPDATE`.
Since 1.18.4, `add_column()` requires `inline_primary_key=True` alongside
`Column(primary_key=True)` to emit an inline `PRIMARY KEY`, subject to backend
rules for adding primary keys.

Batch recreation carries over named CHECK constraints and skips unnamed ones.
Explicitly drop a named check before dropping or renaming its referenced column.
In 1.20.0, recreation warns when it omits an unnamed CHECK from a reflected table.
Supply checks to keep in `table_args`; including any `CheckConstraint` there
suppresses the warning.

In 1.20.0, batch recreation emits each type-generated CHECK once and resolves
naming conventions against the original table name. With `%(constraint_name)s`,
give the type a name, such as `sa.Boolean(create_constraint=True, name="active_bool")`,
to avoid a warning and an unnamed CHECK. `batch_alter_table(naming_convention=...)`
applies its convention only to constraints reflected without a name; existing
names stay intact, including with `%(constraint_name)s`.

For PostgreSQL concurrent index drops, the `autocommit_block()` example in the
operations reference requires driver support for `AUTOCOMMIT` and commits the
current transaction before entering the block. With `transactional_ddl=False`,
close each transaction a migration opens before returning; leaving one open
raises `CommandError`.

Files in this skill

  • SKILL.md7.8 KB
  • coverage.json718 B
  • references/autogenerate-and-rendering.md3.9 KB
  • references/compatibility-and-configuration.md2.6 KB
  • references/operations-and-ddl.md2.6 KB
  • references/revisions-cli-and-extensions.md3.3 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.