Linux command
alembic 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Initialize Alembic
alembic init [alembic]
Create a new migration
alembic revision -m "[Add users table]"
Auto-generate a migration
alembic revision --autogenerate -m "[Add email column]"
Upgrade to the latest
alembic upgrade head
Upgrade to a specific revision
alembic upgrade [revision_id]
Downgrade by one revision
alembic downgrade -1
Show current revision
alembic current
Display migration history
alembic history
说明
Alembic is a database migration tool for SQLAlchemy, Python's popular ORM. It manages incremental, reversible changes to database schemas through version-controlled migration scripts. Migrations are Python files stored in a versions directory. Each migration has an upgrade() function to apply changes and a downgrade() function to reverse them. Alembic tracks the current database state in a special table, allowing it to determine which migrations need to run. The --autogenerate feature compares SQLAlchemy model definitions against the current database schema to automatically generate migration scripts. While convenient, generated migrations should be reviewed as autogenerate cannot detect all types of changes. Configuration is stored in alembic.ini, which specifies the database URL, migration script location, and other settings. The env.py script in the alembic directory handles migration environment setup and can be customized for complex scenarios.
参数
- init _directory_
- Initialize a new Alembic environment in the specified directory.
- revision _-m message_ _--autogenerate_
- Create a new migration revision file.
- upgrade _revision_
- Upgrade database to a target revision (use 'head' for latest).
- downgrade _revision_
- Downgrade database to a target revision (use '-1' for one step back).
- current
- Show the current revision of the database.
- history
- List revision history.
- heads
- Show all current head revisions.
- branches
- Show all branch points.
- stamp _revision_
- Set the revision table to a specific version without running migrations.
- show _revision_
- Display details of a specific revision.
- merge _revisions_ _-m message_
- Merge multiple branch heads into one.
- -c, --config _file_
- Path to alembic.ini configuration file.
- -n, --name _name_
- Name of config section to use.
- -x _key=value_
- Pass additional arguments to env.py.
- --autogenerate
- Auto-generate migration by comparing models to database.
- --sql
- Output SQL instead of applying migrations.
FAQ
What is the alembic command used for?
Alembic is a database migration tool for SQLAlchemy, Python's popular ORM. It manages incremental, reversible changes to database schemas through version-controlled migration scripts. Migrations are Python files stored in a versions directory. Each migration has an upgrade() function to apply changes and a downgrade() function to reverse them. Alembic tracks the current database state in a special table, allowing it to determine which migrations need to run. The --autogenerate feature compares SQLAlchemy model definitions against the current database schema to automatically generate migration scripts. While convenient, generated migrations should be reviewed as autogenerate cannot detect all types of changes. Configuration is stored in alembic.ini, which specifies the database URL, migration script location, and other settings. The env.py script in the alembic directory handles migration environment setup and can be customized for complex scenarios.
How do I run a basic alembic example?
Run `alembic init [alembic]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does init _directory_ do in alembic?
Initialize a new Alembic environment in the specified directory.