75 lines
2.4 KiB
Bash
75 lines
2.4 KiB
Bash
#!/usr/bin/env bash
|
||
# 执行 mint_orders 表迁移:添加 material_type 和 event 字段
|
||
# 使用方式:bash scripts/apply_mint_orders_migration.sh
|
||
|
||
set -euo pipefail
|
||
|
||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
cd "$SCRIPT_DIR/.."
|
||
|
||
# 数据库配置(从环境变量读取,或使用默认值)
|
||
DB_HOST="${DB_HOST:-localhost}"
|
||
DB_PORT="${DB_PORT:-5432}"
|
||
DB_USER="${DB_USER:-haihuizhu}"
|
||
DB_PASSWORD="${DB_PASSWORD:-admin}"
|
||
DB_NAME="${DB_NAME:-top-fans}"
|
||
|
||
GREEN='\033[0;32m'
|
||
YELLOW='\033[1;33m'
|
||
RED='\033[0;31m'
|
||
BLUE='\033[0;34m'
|
||
CYAN='\033[0;36m'
|
||
NC='\033[0m'
|
||
|
||
echo -e "${BLUE}========================================${NC}"
|
||
echo -e "${BLUE} 执行 mint_orders 表迁移${NC}"
|
||
echo -e "${BLUE} 添加 material_type 和 event 字段${NC}"
|
||
echo -e "${BLUE}========================================${NC}"
|
||
echo " 数据库: ${DB_NAME}@${DB_HOST}:${DB_PORT}"
|
||
echo " 用户: ${DB_USER}"
|
||
echo ""
|
||
|
||
# 检查 PostgreSQL 客户端
|
||
if ! command -v psql >/dev/null 2>&1; then
|
||
echo -e "${RED}❌ 未找到 psql 命令,请安装 PostgreSQL 客户端${NC}"
|
||
exit 1
|
||
fi
|
||
|
||
# 执行迁移
|
||
echo -e "${YELLOW}[1/1] 执行数据库迁移...${NC}"
|
||
|
||
export PGPASSWORD="${DB_PASSWORD}"
|
||
|
||
if psql -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" -f scripts/migrations/add_material_type_and_event_to_mint_orders.sql > /tmp/mint_orders_migration.log 2>&1; then
|
||
echo -e "${GREEN}✓ 数据库迁移成功${NC}"
|
||
echo ""
|
||
echo -e "${BLUE}========================================${NC}"
|
||
echo -e "${BLUE} 迁移完成${NC}"
|
||
echo -e "${BLUE}========================================${NC}"
|
||
echo ""
|
||
echo -e "${GREEN}已添加字段:${NC}"
|
||
echo " - material_type VARCHAR(50) NULL (素材类型)"
|
||
echo " - event VARCHAR(100) NULL (藏品事件)"
|
||
echo ""
|
||
echo -e "${CYAN}验证字段:${NC}"
|
||
psql -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" -c "
|
||
SELECT
|
||
column_name,
|
||
data_type,
|
||
character_maximum_length,
|
||
is_nullable
|
||
FROM information_schema.columns
|
||
WHERE table_name = 'mint_orders'
|
||
AND column_name IN ('material_type', 'event')
|
||
ORDER BY column_name;
|
||
"
|
||
echo ""
|
||
else
|
||
echo -e "${RED}❌ 数据库迁移失败${NC}"
|
||
echo -e "${YELLOW}错误日志:${NC}"
|
||
cat /tmp/mint_orders_migration.log
|
||
exit 1
|
||
fi
|
||
|
||
unset PGPASSWORD
|