build(migration): push之前校验migration multiple head

This commit is contained in:
sophie committed 2026-06-23 13:37:30 +08:00
1 parent 1bc938155a
commit 2aa8609c17
3 files changed
+53 -8

No files matched your search

+16 -2
View File
@@ -5,11 +5,12 @@ ROOT_DIR="$(git rev-parse --show-toplevel)"
# shellcheck source=_common.sh
source "${ROOT_DIR}/.githooks/_common.sh"
if hook_should_skip; then
if [[ "${SKIP_GIT_HOOKS:-}" == "1" ]]; then
exit 0
fi
zero="0000000000000000000000000000000000000000"
need_migration_check=0
need_frontend_build=0
while read -r local_ref local_sha remote_ref remote_sha; do
@@ -33,12 +34,25 @@ while read -r local_ref local_sha remote_ref remote_sha; do
changed_files="$(git diff --name-only --diff-filter=ACMR "${remote_sha}" "${local_sha}" || true)"
fi
if echo "${changed_files}" | grep -qE '^backend/migrations/versions/.*\.py$'; then
need_migration_check=1
fi
if frontend_paths_in "${changed_files}"; then
need_frontend_build=1
break
fi
done
if [[ "${need_migration_check}" -eq 1 ]]; then
if ! run_migration_heads_check "${ROOT_DIR}"; then
exit 1
fi
fi
if [[ "${SKIP_FRONTEND_HOOKS:-}" == "1" ]]; then
exit 0
fi
if [[ "${need_frontend_build}" -eq 0 ]]; then
exit 0
fi
@@ -0,0 +1,30 @@
"""Merge FAQ stale fields and shipping schema alignment migration heads.
Purpose:
Keep Alembic's revision graph linear after two independent backend schema
fixes were added from different migration branches.
Dependencies:
Alembic revision metadata for c6d7e8f9a0b1 and f1e2d3c4b5a6.
Flow:
The deployment command ``flask db upgrade`` targets ``head``; this
merge-only revision gives Alembic a single head without executing DDL.
Revision ID: f9a0b1c2d3e4
Revises: c6d7e8f9a0b1, f1e2d3c4b5a6
Create Date: 2026-06-23 12:20:00.000000
"""
revision = "f9a0b1c2d3e4"
down_revision = ("c6d7e8f9a0b1", "f1e2d3c4b5a6")
branch_labels = None
depends_on = None
def upgrade():
"""Unify the two migration heads; no database changes are required."""
pass
def downgrade():
"""Keep downgrade as a no-op because this revision only changes topology."""
pass
+7 -6
View File
@@ -16,7 +16,7 @@
|------|------|--------|----------|
| **pre-commit** | `git commit` | 已安装 Hook 的本机 | models 须带 migration;migration 须单 head;暂存 `frontend/` 时跑 `vue-tsc` |
| **commit-msg** | `git commit` | 已安装 Hook 的本机 | 提交信息须符合 [COMMIT_CONVENTION](./convention/COMMIT_CONVENTION.md)(commitlint) |
| **pre-push** | `git push` | 已安装 Hook 的本机 | 本次推送含 `frontend/` 变更时跑 `npm run build` |
| **pre-push** | `git push` | 已安装 Hook 的本机 | 本次推送含 migration 变更时校验单 head;含 `frontend/` 变更时跑 `npm run build` |
| **Jenkins CI** | push 触发流水线 | 全员(不装 Hook 也跑) | `npm ci && npm run build`(`scripts/ci_frontend_build.sh`) |
- Hook **不会**随 `git clone` 自动启用,需每人配置 `core.hooksPath=.githooks`。
@@ -74,7 +74,7 @@ chmod +x scripts/install_git_hooks.sh
[hook-install] Installed Git hooks successfully.
[hook-install] pre-commit: models 变更需带 migration;migration 须单 head;frontend 暂存时跑 vue-tsc
[hook-install] commit-msg: 校验提交信息格式(Conventional Commits)
[hook-install] pre-push: 推送含 frontend 变更时跑 npm run build
[hook-install] pre-push: 推送含 migration 变更时校验单 head;推送含 frontend 变更时跑 npm run build
[hook-install] core.hooksPath=.githooks
```
@@ -160,8 +160,9 @@ echo "feat(auth): add SMS login support" | npx commitlint
| 条件 | 行为 |
|------|------|
| 本次 push 的提交 diff 含 `backend/migrations/versions/*.py` | 运行 `scripts/check_migration_heads.py`,Alembic **必须仅 1 个 head** |
| 本次 push 的提交 diff 含 `frontend/` | 运行 `npm run build`(`vue-tsc -b && vite build`) |
| 无 `frontend/` 变更 | 直接通过 |
| 无 migration / `frontend/` 变更 | 直接通过 |
### 5.4 Jenkins CI
@@ -183,7 +184,7 @@ echo "feat(auth): add SMS login support" | npx commitlint
### 7.1 只跳过前端检查
migration 规则在 pre-commit 中仍会执行。
migration 规则在 pre-commit / pre-push 中仍会执行。
**Git Bash:**
@@ -213,7 +214,7 @@ git push --no-verify
| 变量 | 默认值 | 生效条件 |
|------|--------|----------|
| `SKIP_FRONTEND_HOOKS` | 未设置 | 设为 `1` 时跳过前端 vue-tsc / build |
| `SKIP_FRONTEND_HOOKS` | 未设置 | 设为 `1` 时仅跳过前端 vue-tsc / build;若本次 push 含 migration,仍会检查 migration |
| `SKIP_GIT_HOOKS` | 未设置 | 设为 `1` 时跳过前端检查(与上一项在前端逻辑等价) |
仓库内**没有**预置这些变量的配置文件,仅在单次命令前临时设置。
@@ -246,7 +247,7 @@ git push --no-verify
| `package.json` | 根目录 tooling 依赖(commitlint) |
| `scripts/install_git_hooks.sh` | 一键安装 |
| `scripts/ci_frontend_build.sh` | Jenkins / 本地 CI 同款 build |
| `scripts/check_migration_heads.py` | 离线校验 Alembic 仅 1 个 head(pre-commit 调用) |
| `scripts/check_migration_heads.py` | 离线校验 Alembic 仅 1 个 head(pre-commit / pre-push 调用) |
| `Jenkinsfile` | `Frontend Build (CI)` 阶段 |
| `docs/新人环境搭建与配置文档.md` | 新人环境 §4.2 简要说明 |