#!/bin/bash
set -e

DATA_DIR="/mnt/data1/database/pgsql/pgdata_perf"
CONTAINER="postgres-perf-test"

echo "=========================================="
echo " 性能测试 PostgreSQL 容器部署"
echo "=========================================="

if [ -d "$DATA_DIR" ] && [ "$(ls -A $DATA_DIR 2>/dev/null)" ]; then
    echo "⚠️  数据目录已存在"
    read -p "清空重建？(y/N): " confirm
    if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
        docker stop $CONTAINER 2>/dev/null || true
        docker rm $CONTAINER 2>/dev/null || true
        sudo rm -rf "$DATA_DIR"
    fi
fi

sudo mkdir -p "$DATA_DIR"
sudo chown -R 999:999 "$DATA_DIR"

# 第一步：用默认配置启动，让 PG 完成初始化
echo "启动容器（默认配置初始化）..."
docker compose -f docker-compose-perf.yml up -d

echo "等待初始化完成..."
for i in $(seq 1 30); do
    if docker exec $CONTAINER pg_isready -U erp_pgsql -d junhong_cmp_test > /dev/null 2>&1; then
        echo "✅ 初始化完成"
        break
    fi
    [ $i -eq 30 ] && echo "❌ 超时" && docker logs $CONTAINER && exit 1
    sleep 2
done

# 第二步：把优化后的配置覆盖到数据目录
echo "注入优化配置..."
docker cp postgresql-perf.conf $CONTAINER:/var/lib/postgresql/data/postgresql.conf

# 第三步：重启让新配置生效
echo "重启容器..."
docker restart $CONTAINER

echo "等待重启完成..."
for i in $(seq 1 30); do
    if docker exec $CONTAINER pg_isready -U erp_pgsql -d junhong_cmp_test > /dev/null 2>&1; then
        echo "✅ 重启完成"
        break
    fi
    [ $i -eq 30 ] && echo "❌ 超时" && docker logs --tail 20 $CONTAINER && exit 1
    sleep 2
done

# 第四步：启用扩展
echo "启用扩展..."
docker exec $CONTAINER psql -U erp_pgsql -d junhong_cmp_test -c "CREATE EXTENSION IF NOT EXISTS pg_stat_statements;" 2>/dev/null || echo "pg_stat_statements 需要 shared_preload_libraries，跳过"
docker exec $CONTAINER psql -U erp_pgsql -d junhong_cmp_test -c "CREATE EXTENSION IF NOT EXISTS pg_trgm;"

# 验证
echo ""
echo "=========================================="
echo " 配置验证"
echo "=========================================="
docker exec $CONTAINER psql -U erp_pgsql -d junhong_cmp_test -c "
SELECT name, setting, unit
FROM pg_settings
WHERE name IN (
    'shared_buffers', 'effective_cache_size', 'work_mem',
    'random_page_cost', 'effective_io_concurrency',
    'max_parallel_workers_per_gather', 'maintenance_work_mem',
    'max_connections', 'autovacuum_vacuum_scale_factor'
)
ORDER BY name;
"

echo ""
echo "=========================================="
echo " 部署完成"
echo "=========================================="
echo "  Port:     16289"
echo "  Database: junhong_cmp_test"
echo "  User:     erp_pgsql / erp_2025"
echo ""
