使用 Alembic 实现数据迁移管理

发表于 2025/12/21 GMT+8 21:01:15

最后更新于 2025/12/23 GMT+8 19:35:58

初次使用

安装依赖

uv add alembic

初始化 Alembic 迁移工具

alembic init alembic

创建初始迁移

alembic revision --autogenerate -m "Initial migration"

database.py 中创建迁移函数 run_migrations()

def run_migrations():
    try:
        logger.info("Running database migrations...")
        # Assuming alembic.ini is in the current working directory or parent directory
        # We can try to locate it
        alembic_ini_path = "alembic.ini"
        if not os.path.exists(alembic_ini_path):
             # Try looking one level up if we are running from app/
             alembic_ini_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "alembic.ini")

        if not os.path.exists(alembic_ini_path):
            logger.warning("alembic.ini not found, skipping migrations.")
            return

        alembic_cfg = Config(alembic_ini_path)
        # Ensure script_location is absolute or correct relative to CWD
        # If alembic.ini has script_location = alembic, it expects alembic folder in CWD.
        # If we are in api/, it works.

        command.upgrade(alembic_cfg, "head")
        logger.info("Database migrations completed.")
    except Exception as e:
        logger.error(f"Error running migrations: {e}")
        # Re-raise to fail startup if migration fails
        raise e

修改了 app.py,在应用启动(lifespan)时自动调用 run_migrations()

当修改数据字段时

当修改了数据模型(例如在 models.py 中添加了新字段)并准备发布新版本时

  1. 生成迁移脚本(在开发环境中)
alembic revision --autogenerate -m "描述您的更改(例如:添加用户手机号字段)"
  1. 检查迁移脚本

确认 upgrade()downgrade() 函数中的变更

文章信息
基于 Git 提交记录的文章历史
发布时间

2025/12/21

更新时间

2025/12/23

修订次数2 次提交
贡献者
qwas