Files
wxsales/demo/deploy_remote.sh

40 lines
1.5 KiB
Bash
Raw Permalink 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.
#!/bin/bash
set -eu
# ============================================
# 远程服务器端部署脚本(由 deploy.sh 调用)
# 在远程服务器上执行数据库迁移和数据预置
# ============================================
DEMO_DIR="/opt/wxsales/demo"
# 查找 psql 路径
PSQL=""
for p in /usr/bin/psql /usr/local/bin/psql /usr/pgsql-17/bin/psql /usr/local/pg17/bin/psql; do
if [ -x "$p" ]; then PSQL="$p"; break; fi
done
if [ -z "$PSQL" ]; then
echo "[ERROR] 未找到 psql"
exit 1
fi
# 远程环境:PG 15 端口 5432socket 在 /run/postgresql,用 postgres 用户连接
PG_SOCKET="/run/postgresql"
echo "[INFO] 使用 psql: $PSQL (socket: $PG_SOCKET)"
echo "[INFO] 执行 schema.sql..."
# 执行 schema.sql(幂等,包含迁移语句)— 用 su postgres 通过 socket 连接
su - postgres -c "$PSQL -h $PG_SOCKET -p 5432 -d wxchat_sales -f $DEMO_DIR/db/schema.sql" 2>&1 || {
echo "[WARN] schema.sql 执行有警告,尝试继续..."
}
# 重置序列
su - postgres -c "$PSQL -h $PG_SOCKET -p 5432 -d wxchat_sales -c \"SELECT setval('product_id_seq', COALESCE((SELECT max(id) FROM product), 1), true);\"" 2>/dev/null || true
su - postgres -c "$PSQL -h $PG_SOCKET -p 5432 -d wxchat_sales -c \"SELECT setval('salesperson_id_seq', COALESCE((SELECT max(id) FROM salesperson), 1), true);\"" 2>/dev/null || true
# 验证
PRODUCT_COUNT=$(su - postgres -c "$PSQL -h $PG_SOCKET -p 5432 -d wxchat_sales -t -c 'SELECT count(*) FROM product;'" 2>/dev/null | xargs || echo "0")
echo "[OK] 产品数量: $PRODUCT_COUNT"
echo "[OK] 数据库迁移完成"