chore(ci): 升级migration验证,新增4项检查 (#451) 同步到main
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 12s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 53s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 27s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 3m22s
CI/CD Pipeline / Validate - Code Quality (pull_request) Successful in 3m42s
CI/CD Pipeline / PR Build Web Image (pull_request) Successful in 36s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 31s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 49s
Preview Deploy / Deploy Preview Environment (pull_request) Failing after 19s
AI Code Review / AI Code Review (pull_request) Successful in 1m0s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 2m23s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m42s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m0s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 12s
CI/CD Pipeline / Production Browser E2E (pull_request) Failing after 1277h35m25s
CI/CD Pipeline / ACR Image Cleanup (pull_request) Failing after 1277h35m27s
CI/CD Pipeline / Staging API Integration Tests (pull_request) Failing after 1277h35m29s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Failing after 1277h35m37s
CI/CD Pipeline / Staging E2E Tests (pull_request) Failing after 1277h35m31s
CI/CD Pipeline / Deploy Production (pull_request) Failing after 1277h35m33s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Failing after 1277h37m39s
CI/CD Pipeline / Build Staging Worker Image (pull_request) Failing after 1277h37m47s
CI/CD Pipeline / Build Staging API Image (pull_request) Failing after 1277h37m51s
CI/CD Pipeline / Build Production Worker Image (pull_request) Failing after 1277h36m39s
CI/CD Pipeline / Build Production Web Image (pull_request) Failing after 1277h36m41s
CI/CD Pipeline / Build Production API Image (pull_request) Failing after 1277h36m43s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Failing after 1277h36m45s
CI/CD Pipeline / Build Staging Web Image (pull_request) Failing after 1278h10m21s

This commit is contained in:
2026-07-25 09:45:45 +08:00
parent f03c9d5453
commit 0cfeb6927f
3 changed files with 332 additions and 38 deletions
Regular → Executable
+181 -38
View File
@@ -1,13 +1,65 @@
#!/bin/bash
# CI Validate: Alembic迁移验证(并行Job 3/3)
# 需要PostgreSQL数据库
# CI Validate: Alembic迁移验证(升级版)
# 检查项:
# 1. migration文件命名规范检查
# 2. migration编号链完整性检查
# 3. upgrade head 升级验证(真实PG执行)
# 4. downgrade -1 回滚验证
# 5. alembic check 检测未生成migration的model变更
#
# 需要PostgreSQL数据库(共享PG或临时容器)
set -eu
echo "=== CI Validate: Alembic迁移验证 ==="
SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")"
# shellcheck source=ci_env.sh
source "${SCRIPT_DIR}/ci_env.sh"
echo "=== CI Validate: Alembic迁移验证(升级版)==="
echo ""
# ============================================================
# 阶段0: 静态检查(不需要数据库,先快速失败)
# ============================================================
echo "📋 阶段0: 静态检查(命名规范 + 链完整性)"
echo ""
STATIC_FAILED=0
echo "0.1 检查 migration 文件命名规范..."
if python3 scripts/ci/check_migration_naming.py alembic/versions; then
echo " ✅ 命名规范检查通过"
else
echo " ❌ 命名规范检查失败"
STATIC_FAILED=1
fi
echo ""
echo "0.2 检查 migration 编号链完整性..."
if python3 scripts/ci/check_migration_chain.py alembic/versions; then
echo " ✅ 编号链完整性检查通过"
else
echo " ❌ 编号链完整性检查失败"
STATIC_FAILED=1
fi
if [ "$STATIC_FAILED" -ne 0 ]; then
echo ""
echo "❌ 静态检查失败,请修复上述问题后重试"
exit 1
fi
echo ""
echo "✅ 静态检查全部通过"
echo ""
# ============================================================
# DooD模式检测:确定宿主机访问地址
# ============================================================
# --- DooD模式检测:确定宿主机访问地址 ---
detect_docker_host() {
local test_port="${1:-5432}"
local test_port="${1:-${CI_LOCAL_PG_PORT}}"
local candidates=()
@@ -59,20 +111,6 @@ except:
return 1
}
# 获取宿主机IP
if [ -S /var/run/docker.sock ]; then
DOCKER_HOST_IP=$(detect_docker_host 5433)
if [ "$DOCKER_HOST_IP" = "127.0.0.1" ]; then
DOCKER_HOST_IP=$(detect_docker_host 22)
fi
echo "检测到DooD模式,宿主机地址: $DOCKER_HOST_IP"
else
DOCKER_HOST_IP="127.0.0.1"
echo "非DooD模式,使用 127.0.0.1"
fi
PG_HOST="$DOCKER_HOST_IP"
echo "PG host: $PG_HOST"
# 指数退避TCP连接检查
wait_tcp_ready() {
local host="$1"
@@ -92,16 +130,39 @@ wait_tcp_ready() {
return 1
}
# 获取宿主机IP
if [ -S /var/run/docker.sock ]; then
DOCKER_HOST_IP=$(detect_docker_host "${CI_SHARED_PG_PORT}")
if [ "$DOCKER_HOST_IP" = "127.0.0.1" ]; then
DOCKER_HOST_IP=$(detect_docker_host 22)
fi
echo "检测到DooD模式,宿主机地址: $DOCKER_HOST_IP"
else
DOCKER_HOST_IP="127.0.0.1"
echo "非DooD模式,使用 127.0.0.1"
fi
PG_HOST="$DOCKER_HOST_IP"
echo "PG host: $PG_HOST"
echo ""
USE_SHARED_PG="${CI_USE_SHARED_PG:-false}"
# ============================================================
# 准备数据库
# ============================================================
echo "🗄️ 阶段1: 准备测试数据库"
echo ""
CI_DB_NAME="ci_migrate_${GITHUB_RUN_ID:-$$}"
if [ "$USE_SHARED_PG" = "true" ]; then
# 使用常驻共享PG实例
echo "使用常驻共享PG实例(CI_USE_SHARED_PG=true)"
SHARED_PG_HOST="$PG_HOST"
SHARED_PG_PORT="5433"
SHARED_PG_USER="postgres"
SHARED_PG_PASSWORD="ci_pg_2026!"
CI_DB_NAME="ci_run_${GITHUB_RUN_ID:-$$}"
SHARED_PG_PORT="${CI_SHARED_PG_PORT}"
SHARED_PG_USER="${CI_SHARED_PG_USER}"
SHARED_PG_PASSWORD="${CI_SHARED_PG_PASSWORD}"
echo "等待共享PG连接就绪..."
wait_tcp_ready "$SHARED_PG_HOST" "$SHARED_PG_PORT" 5
@@ -120,13 +181,10 @@ conn.close()
export DATABASE_URL="postgresql+psycopg://${SHARED_PG_USER}:${SHARED_PG_PASSWORD}@${SHARED_PG_HOST}:${SHARED_PG_PORT}/${CI_DB_NAME}"
echo "✅ 共享PG数据库已创建: $CI_DB_NAME"
# 执行迁移
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m alembic upgrade head
echo "✅ Alembic migrations applied successfully"
# 清理数据库
echo "清理测试数据库: $CI_DB_NAME"
PGPASSWORD="$SHARED_PG_PASSWORD" python3 -c "
cleanup_db() {
echo ""
echo "清理测试数据库: $CI_DB_NAME"
PGPASSWORD="$SHARED_PG_PASSWORD" python3 -c "
import psycopg2
conn = psycopg2.connect(host='$SHARED_PG_HOST', port=$SHARED_PG_PORT, user='$SHARED_PG_USER', password='$SHARED_PG_PASSWORD', dbname='postgres')
conn.autocommit = True
@@ -135,7 +193,8 @@ cur.execute(f'DROP DATABASE IF EXISTS \"$CI_DB_NAME\" WITH (FORCE)')
cur.close()
conn.close()
" 2>/dev/null || echo "WARN: 数据库清理失败"
echo "✅ 共享PG数据库已清理"
echo "✅ 数据库已清理"
}
else
# 使用临时PG容器(默认模式)
echo "使用临时PG容器模式"
@@ -152,9 +211,9 @@ else
--health-timeout 3s \
--health-retries 20 \
postgres:16-alpine
PG_PORT=$(docker port "$PG_CONTAINER" 5432/tcp | cut -d: -f2)
PG_PORT=$(docker port "$PG_CONTAINER" ${CI_LOCAL_PG_PORT}/tcp | cut -d: -f2)
echo "PostgreSQL port: $PG_PORT"
export DATABASE_URL="postgresql+psycopg://postgres:postgres@${PG_HOST}:${PG_PORT}/xiaoxia_saas"
export DATABASE_URL="postgresql+psycopg://${CI_SHARED_PG_USER}:${CI_SHARED_PG_PASSWORD}@${PG_HOST}:${PG_PORT}/${CI_DEFAULT_DB}"
# 等待容器健康
for i in $(seq 1 30); do
@@ -172,12 +231,96 @@ else
wait_tcp_ready "$PG_HOST" "$PG_PORT" 5
echo "TCP connectivity to PostgreSQL confirmed on port $PG_PORT"
# 执行迁移
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m alembic upgrade head
echo "✅ Alembic migrations applied successfully"
cleanup_db() {
docker rm -f "$PG_CONTAINER" 2>/dev/null || true
}
fi
docker rm -f "$PG_CONTAINER" 2>/dev/null || true
trap cleanup_db EXIT
echo ""
# ============================================================
# 阶段2: upgrade head 升级验证
# ============================================================
echo "⬆️ 阶段2: upgrade head 升级验证"
echo ""
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m alembic upgrade head
echo "✅ upgrade head 通过"
echo ""
# ============================================================
# 阶段3: downgrade -1 回滚验证
# ============================================================
echo "⬇️ 阶段3: downgrade -1 回滚验证"
echo ""
# 获取当前head版本号
HEAD_REV=$(PYTHONPATH="$PWD/apps/api:$PWD" python3 -m alembic current 2>&1 | awk '{print $1}' | head -1)
echo "当前版本 (head): $HEAD_REV"
# 检查是否只有1个migration(baseline),downgrade -1会到base
TOTAL_REVS=$(PYTHONPATH="$PWD/apps/api:$PWD" python3 -c "
from alembic.config import Config
from alembic.script import ScriptDirectory
config = Config('alembic.ini')
script = ScriptDirectory.from_config(config)
print(len(list(script.walk_revisions())))
")
echo "总 migration 数量: $TOTAL_REVS"
if [ "$TOTAL_REVS" -le 1 ]; then
echo "⚠️ 只有1个migration,跳过 downgrade 回滚验证(没有可回滚的版本)"
else
echo "执行 downgrade -1..."
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m alembic downgrade -1
echo "✅ downgrade -1 通过"
# 回滚后再升级回去,确保双向都通
echo ""
echo "重新 upgrade head 验证双向一致性..."
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m alembic upgrade head
echo "✅ 重新 upgrade head 通过(双向验证完成)"
fi
echo ""
echo "=== CI Validate: Alembic迁移验证 通过 ✅ ==="
# ============================================================
# 阶段4: alembic check - 检测未生成migration的model变更
# ============================================================
echo "🔍 阶段4: 检查是否有未生成migration的model变更"
echo ""
# alembic check: 没有待生成的migration时退出码0,有变更时退出码1
# 这里只检测,不阻断(警告模式),因为有些场景model变更不需要migration
set +e
CHECK_OUTPUT=$(PYTHONPATH="$PWD/apps/api:$PWD" python3 -m alembic check 2>&1)
CHECK_EXIT=$?
set -e
if [ "$CHECK_EXIT" -eq 0 ]; then
echo "✅ 没有检测到未生成migration的model变更"
else
if echo "$CHECK_OUTPUT" | grep -q "New upgrade operations detected"; then
echo "⚠️ 检测到未生成migration的model变更!"
echo ""
echo "$CHECK_OUTPUT"
echo ""
echo "提示: 如果model变更是有意的且需要生成migration,请运行:"
echo " alembic revision --autogenerate -m \"description\""
echo "如果model变更不涉及数据库schema(如仅索引/约束重命名或纯业务逻辑),请确认后忽略此警告。"
# 暂时不阻断,避免误报
echo "(当前为警告模式,不阻断CI,后续稳定后可升级为阻断)"
else
echo "⚠️ alembic check 执行出错(非阻断)"
echo "$CHECK_OUTPUT"
fi
fi
echo ""
echo "=== CI Validate: Alembic迁移验证 全部通过 ✅ ==="