Files
jiu/.gitea/workflows/reset-db.yml
T
wangjia 8adb94a9a4
Deploy / deploy (push) Successful in 1m12s
fix(deploy): 修复多 shop seed 互相 TRUNCATE 问题 + 补 utf8mb4 charset
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-24 13:41:34 +08:00

74 lines
2.6 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Reset DB
on:
workflow_dispatch:
inputs:
shops:
description: '写入的门店(逗号分隔,如 S001,S002,S003'
required: true
default: 'S001,S002,S003'
jobs:
reset:
runs-on: mac
steps:
- uses: actions/checkout@v4
- name: Setup SSH
run: |
mkdir -p ~/.ssh
printf '%s' "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/ec2_deploy.pem
chmod 600 ~/.ssh/ec2_deploy.pem
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
- name: Reset database & reseed
env:
EC2_HOST: ${{ secrets.EC2_HOST }}
EC2_USER: ${{ secrets.EC2_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
SHOPS: ${{ github.event.inputs.shops }}
run: |
# 1. 清库重建
ssh -i ~/.ssh/ec2_deploy.pem ${EC2_USER}@${EC2_HOST} \
"docker exec jiu_mysql mysql -uroot -p${DB_PASSWORD} -e 'DROP DATABASE IF EXISTS jiu_db; CREATE DATABASE jiu_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;'"
echo "✓ Database reset"
# 2. 重启 jiu-serverAutoMigrate 重建表结构
ssh -i ~/.ssh/ec2_deploy.pem ${EC2_USER}@${EC2_HOST} \
"sudo systemctl restart jiu"
echo "✓ jiu-server restarted, waiting for AutoMigrate..."
# 3. 等待健康检查通过
for i in $(seq 1 30); do
ssh -i ~/.ssh/ec2_deploy.pem ${EC2_USER}@${EC2_HOST} \
"curl -sf http://localhost:8080/health" && break
sleep 2
done
echo "✓ Health check passed"
# 4. 写入 seed 数据(第一个 shop 全量,后续跳过 TRUNCATE 行避免互相清空)
FIRST=true
for SHOP in $(echo "$SHOPS" | tr ',' ' '); do
FILE="backend/seeds/${SHOP}.sql"
if [ ! -f "$FILE" ]; then
echo "⚠️ $FILE not found, skipping"
continue
fi
echo "→ Seeding $SHOP ..."
if [ "$FIRST" = "true" ]; then
ssh -i ~/.ssh/ec2_deploy.pem ${EC2_USER}@${EC2_HOST} \
"docker exec -i jiu_mysql mysql -uroot -p${DB_PASSWORD} --default-character-set=utf8mb4 jiu_db" \
< "$FILE"
FIRST=false
else
grep -v "^TRUNCATE" "$FILE" | \
ssh -i ~/.ssh/ec2_deploy.pem ${EC2_USER}@${EC2_HOST} \
"docker exec -i jiu_mysql mysql -uroot -p${DB_PASSWORD} --default-character-set=utf8mb4 jiu_db"
fi
echo "✓ $SHOP done"
done
- name: Cleanup SSH key
if: always()
run: rm -f ~/.ssh/ec2_deploy.pem