Skip to main content

alembic-migration

Create, review, and apply database schema changes with Alembic. Use whenever a SQLAlchemy model is added or changed, a column/index/constraint needs to change, or a data backfill is required — anything that alters the PostgreSQL schema.

跳到安装

来源信息

仓库
vstorm-co/full-stack-ai-agent-template
最近来源活动
2026年6月22日 14:41
检测到的 SKILL.md 语言
英语
星标
1,898
分支
375

安装方式

默认使用会先检查来源的 Prompt;你也可以切换为直接命令,或下载本地副本。

检查来源文件

决定是否安装前,请先阅读 SKILL.md,以及 SkillsMP 当前展示的配套文件。

正在显示 SKILL.md

SKILL.md
来源说明 · 只读预览
name
alembic-migration
description
Create, review, and apply database schema changes with Alembic. Use whenever a SQLAlchemy model is added or changed, a column/index/constraint needs to change, or a data backfill is required — anything that alters the PostgreSQL schema.
# Alembic Migrations This project uses **async SQLAlchemy 2.0 + Alembic** on PostgreSQL. Migrations live in `backend/alembic/versions/` and are numbered (`0001_…`, `0002_…`). ## Workflow 1. **Change the model first** in `backend/app/db/models/` (`Mapped[...]` + `mapped_column()`, `__repr__`, relationships with `ondelete="CASCADE"`). Make sure the model is imported in `backend/app/db/models/__init__.py` so autogenerate sees it. 2. **Autogenerate** the migration: ```bash cd backend && uv run alembic revision --autogenerate -m "add <thing>" # or: make db-migrate ``` 3. **ALWAYS review the generated file.** Autogenerate is a draft, not the truth: - Confirm `upgrade()` matches your intent and `downgrade()` actually reverses it. - Check the `down_revision` chains onto the current head (`uv run alembic heads`). - Watch for dropped columns/tables you didn't intend, server defaults, enum changes, and JSON/array types. - Name the revision file with the next sequential prefix to match the existing convention. 4. **Apply and verify:** ```bash cd backend && uv run alembic upgrade head # or: make db-upgrade uv run alembic current # confirm at head ``` Then round-trip once to prove `downgrade()` works: `alembic downgrade -1 && alembic upgrade head`. ## Data migrations / backfills For backfills, add explicit `op.execute(...)` or a small data-loop in `upgrade()` (see the existing `*_backfill_*.py` migrations for the pattern). Keep schema and data changes in separate, well-named revisions when practical. ## Rules - Never edit a migration that has already been applied in shared environments — add a new one. - `make dev` / `make bootstrap` run `alembic upgrade head` automatically; you don't need a separate step in dev. - Keep models and migrations in sync — a model change without a migration will pass tests (sessions are mocked) but break on real Postgres.
在 GitHub 查看