feat: 使用 Alembic 实现数据库结构与代码同步

并且添加了一些管理数据库的快捷命令,实现和 Django 类似的开发体验:
- uv run task makemigrations : 创建数据库变更文件
- uv run task migrate : 应用数据库变更
更多请查看 pyproject.toml 。
目前仅针对 SQLite 数据库进行适配。
This commit is contained in:
2026-05-24 13:52:57 +08:00
parent f3442a1b61
commit c30d65f9ae
6 changed files with 1160 additions and 169 deletions
+36 -5
View File
@@ -6,19 +6,33 @@ readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"aiofiles>=25.1.0",
"aiosmtplib>=5.1.0",
"alembic>=1.18.4",
"argon2-cffi>=25.1.0",
"fastapi>=0.136.1",
"httpx>=0.28.1",
"loguru>=0.7.3",
"jinja2>=3.1.6",
"openai>=2.38.0",
"passlib[bcrypt]>=1.7.4",
"pydantic>=2.13.4",
"python-jose[cryptography]>=3.5.0",
"python-multipart>=0.0.29",
"pyyaml>=6.0.3",
"rich>=15.0.0",
"sqlalchemy>=2.0.49",
"sqlmodel>=0.0.38",
"typer>=0.25.0",
"uvicorn>=0.46.0",
"typer>=0.25.1",
"uvicorn>=0.47.0",
]
[dependency-groups]
dev = [
"mypy>=1.20.2",
"ruff>=0.15.12",
"mypy>=2.1.0",
"ruff>=0.15.14",
"taskipy>=1.14.1",
"types-aiofiles>=25.1.0.20260518",
"types-passlib>=1.7.7.20260211",
"types-python-jose>=3.5.0.20260408",
]
[project.scripts]
@@ -38,6 +52,7 @@ artifacts = ["src/nyahome/static/**"]
[tool.ruff]
preview = true
line-length = 120
target-version = "py313"
[tool.ruff.lint]
select = [
@@ -74,8 +89,24 @@ ignore = [
"RUF029",
"C90",
"COM812",
"E501",
]
fixable = ["ALL"]
[tool.mypy]
files = ["src"]
strict = true
warn_return_any = true
disable_error_code = ["list-item", "attr-defined", "type-arg"]
[tool.taskipy.tasks]
# 生成迁移
makemigrations = "alembic revision --autogenerate -m 'auto'"
# 执行迁移
migrate = "alembic upgrade head"
# 回滚
rollback = "alembic downgrade -1"
# 查看数据库版本
current = "alembic current"