fix: 修复数据库迁移事务中断问题,补充 k8s-monitor 构建流水线
All checks were successful
Build and Deploy Log Center / build-and-deploy (push) Successful in 2m28s

- database.py: 使用 IF NOT EXISTS 避免 PostgreSQL 事务中断导致后续迁移 SQL 不执行
- deploy.yaml: 补充 k8s-pod-monitor 镜像构建和 CronJob 部署步骤

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
zyc 2026-02-24 10:30:36 +08:00
parent 0d4b2d634c
commit 229d86e158
2 changed files with 19 additions and 7 deletions

View File

@ -49,6 +49,16 @@ jobs:
build-args: | build-args: |
VITE_API_BASE_URL=https://qiyuan-log-center-api.airlabs.art VITE_API_BASE_URL=https://qiyuan-log-center-api.airlabs.art
# Build K8s Monitor Image
- name: Build and Push K8s Monitor
uses: docker/build-push-action@v4
with:
context: ./k8s-monitor
file: ./k8s-monitor/Dockerfile
push: true
provenance: false
tags: ${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/k8s-pod-monitor:latest
- name: Setup Kubectl - name: Setup Kubectl
run: | run: |
curl -LO "https://files.m.daocloud.io/dl.k8s.io/release/v1.28.2/bin/linux/amd64/kubectl" curl -LO "https://files.m.daocloud.io/dl.k8s.io/release/v1.28.2/bin/linux/amd64/kubectl"
@ -68,12 +78,14 @@ jobs:
# Replace image placeholders # Replace image placeholders
sed -i "s|\${CI_REGISTRY_IMAGE}/log-center-api:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-api:latest|g" k8s/api-deployment-prod.yaml sed -i "s|\${CI_REGISTRY_IMAGE}/log-center-api:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-api:latest|g" k8s/api-deployment-prod.yaml
sed -i "s|\${CI_REGISTRY_IMAGE}/log-center-web:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-web:latest|g" k8s/web-deployment-prod.yaml sed -i "s|\${CI_REGISTRY_IMAGE}/log-center-web:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/log-center-web:latest|g" k8s/web-deployment-prod.yaml
sed -i "s|\${CI_REGISTRY_IMAGE}/k8s-pod-monitor:latest|${{ secrets.SWR_SERVER }}/${{ secrets.SWR_ORG }}/k8s-pod-monitor:latest|g" k8s/monitor-cronjob.yaml
# Apply configurations # Apply configurations
kubectl apply -f k8s/api-deployment-prod.yaml kubectl apply -f k8s/api-deployment-prod.yaml
kubectl apply -f k8s/web-deployment-prod.yaml kubectl apply -f k8s/web-deployment-prod.yaml
kubectl apply -f k8s/ingress.yaml kubectl apply -f k8s/ingress.yaml
kubectl apply -f k8s/monitor-cronjob.yaml
# Restart deployments # Restart deployments
kubectl rollout restart deployment/log-center-api kubectl rollout restart deployment/log-center-api
kubectl rollout restart deployment/log-center-web kubectl rollout restart deployment/log-center-web

View File

@ -25,19 +25,19 @@ async def init_db():
# Migrate: add new columns to existing repairtask table # Migrate: add new columns to existing repairtask table
migrations = [ migrations = [
"ALTER TABLE repairtask ADD COLUMN repair_round INTEGER DEFAULT 1", "ALTER TABLE repairtask ADD COLUMN IF NOT EXISTS repair_round INTEGER DEFAULT 1",
"ALTER TABLE repairtask ADD COLUMN failure_reason TEXT", "ALTER TABLE repairtask ADD COLUMN IF NOT EXISTS failure_reason TEXT",
# Log source support # Log source support
"ALTER TABLE errorlog ADD COLUMN source VARCHAR(20) DEFAULT 'runtime'", "ALTER TABLE errorlog ADD COLUMN IF NOT EXISTS source VARCHAR(20) DEFAULT 'runtime'",
"ALTER TABLE errorlog ALTER COLUMN file_path DROP NOT NULL", "ALTER TABLE errorlog ALTER COLUMN file_path DROP NOT NULL",
"ALTER TABLE errorlog ALTER COLUMN line_number DROP NOT NULL", "ALTER TABLE errorlog ALTER COLUMN line_number DROP NOT NULL",
"CREATE INDEX ix_errorlog_source ON errorlog (source)", "CREATE INDEX IF NOT EXISTS ix_errorlog_source ON errorlog (source)",
] ]
for sql in migrations: for sql in migrations:
try: try:
await conn.execute(text(sql)) await conn.execute(text(sql))
except Exception: except Exception:
pass # Column already exists pass # Already applied
async def get_session() -> AsyncSession: async def get_session() -> AsyncSession:
async_session = sessionmaker( async_session = sessionmaker(