CI - Continuous Integration / Run Tests (push) Failing after 0s
CI - Continuous Integration / Security Scan (push) Failing after 0s
Complete CI/CD Pipeline / Code Quality & Security (push) Failing after 0s
CI - Continuous Integration / Build and Verify (push) Has been skipped
Complete CI/CD Pipeline / Run Tests (push) Has been skipped
Complete CI/CD Pipeline / Build Docker Image (push) Has been skipped
Complete CI/CD Pipeline / Container Security Scan (push) Has been skipped
Complete CI/CD Pipeline / Deploy to Development (GitOps) (push) Has been skipped
Complete CI/CD Pipeline / Deploy to Production (GitOps) (push) Has been skipped
Security Scanning / SAST (Bandit + Semgrep) (push) Failing after 1s
Complete CI/CD Pipeline / Post-deployment Verification (push) Failing after 0s
Complete CI/CD Pipeline / Notify Results (push) Successful in 0s
Security Scanning / Container Scanning (push) Failing after 0s
Security Scanning / Dependency Scanning (Safety + Trivy) (push) Failing after 2s
Security Scanning / Secret Scanning (push) Failing after 0s
Test Matrix / Lint (push) Failing after 1s
Security Scanning / License Compliance (push) Failing after 3s
Test Matrix / Test (${{ matrix.python-version }}, ${{ matrix.test-type }}) (macos-latest, 3.11, unit) (push) Has been skipped
Test Matrix / Test (${{ matrix.python-version }}, ${{ matrix.test-type }}) (ubuntu-latest, 3.10, integration) (push) Has been skipped
Test Matrix / Test (${{ matrix.python-version }}, ${{ matrix.test-type }}) (ubuntu-latest, 3.10, unit) (push) Has been skipped
Test Matrix / Test (${{ matrix.python-version }}, ${{ matrix.test-type }}) (ubuntu-latest, 3.11, integration) (push) Has been skipped
Test Matrix / Test (${{ matrix.python-version }}, ${{ matrix.test-type }}) (ubuntu-latest, 3.11, unit) (push) Has been skipped
Test Matrix / Test (${{ matrix.python-version }}, ${{ matrix.test-type }}) (ubuntu-latest, 3.12, integration) (push) Has been skipped
Test Matrix / Test (${{ matrix.python-version }}, ${{ matrix.test-type }}) (ubuntu-latest, 3.12, unit) (push) Has been skipped
Test Matrix / E2E Tests (push) Has been skipped
Test Matrix / Coverage Summary (push) Has been skipped
Test Matrix / Security Scan (push) Failing after 3s
Security Scanning / Security Summary (push) Failing after 2s
TODO to Issue / Create Issues from TODOs (push) Failing after 0s
Test Matrix / Build Docker Image (push) Has been skipped
Test Matrix / All Tests Passed (push) Failing after 0s
Created 700+ lines of test coverage for risk assessment endpoints: - 25 test cases covering all CRUD operations - Access control tests (guardian, psychologist, administrator) - Ollama integration mocking - Fallback mechanisms - Pagination and filtering - Error handling Current status: 4/25 tests passing (16%) Changes: - Create tests/unit/test_api_risk_assessments.py with 25 test cases - Update test helpers for PostgreSQL model compatibility - Fix mock patches for Ollama client - Update create_access_token call signature - Fix model field names (user_id, oss_file_key, text_content, risk_keywords) Remaining issues to fix: 1. Async/sync mismatch: ChunkedIteratorResult can't be awaited 2. Check constraint violations: sentiment values need validation 3. Need to align test database with async API code Dependencies fixed: - Downgrade bcrypt 5.0.0 → 4.3.0 for passlib compatibility - Install asyncpg for PostgreSQL async driver - Install psycopg2-binary for PostgreSQL sync tests Related to Issue #37 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
204 lines
7.3 KiB
Bash
Executable File
204 lines
7.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# =============================================================================
|
|
# PostgreSQL 快速设置脚本
|
|
# =============================================================================
|
|
# 用于留守儿童心理风险检测平台的本地开发环境
|
|
|
|
set -e
|
|
|
|
echo "🐘 PostgreSQL 快速设置脚本"
|
|
echo "================================"
|
|
echo ""
|
|
|
|
# 检测操作系统
|
|
OS="$(uname -s)"
|
|
case "${OS}" in
|
|
Linux*) MACHINE=Linux;;
|
|
Darwin*) MACHINE=Mac;;
|
|
*) MACHINE="UNKNOWN:${OS}"
|
|
esac
|
|
|
|
echo "📌 检测到操作系统: ${MACHINE}"
|
|
echo ""
|
|
|
|
# 选择安装方式
|
|
echo "选择 PostgreSQL 安装方式:"
|
|
echo " 1) Docker (推荐) - 快速、隔离、易于清理"
|
|
echo " 2) 本地安装 (Homebrew/apt) - 持久化、系统级"
|
|
echo " 3) 使用现有 PostgreSQL"
|
|
echo ""
|
|
read -p "请选择 (1/2/3): " choice
|
|
|
|
case $choice in
|
|
1)
|
|
echo ""
|
|
echo "🐳 使用 Docker 安装 PostgreSQL..."
|
|
echo ""
|
|
|
|
# 检查 Docker 是否安装
|
|
if ! command -v docker &> /dev/null; then
|
|
echo "❌ Docker 未安装,请先安装 Docker:"
|
|
echo " macOS: https://www.docker.com/products/docker-desktop"
|
|
echo " Linux: sudo apt-get install docker.io"
|
|
exit 1
|
|
fi
|
|
|
|
# 停止并删除旧容器(如果存在)
|
|
echo "🔍 检查是否存在旧容器..."
|
|
docker stop ljwx-postgres 2>/dev/null || true
|
|
docker rm ljwx-postgres 2>/dev/null || true
|
|
|
|
# 启动 PostgreSQL 容器
|
|
echo "🚀 启动 PostgreSQL 15 容器..."
|
|
docker run -d \
|
|
--name ljwx-postgres \
|
|
-p 5432:5432 \
|
|
-e POSTGRES_PASSWORD=postgres \
|
|
-e POSTGRES_DB=ljwx_children \
|
|
-v ljwx-postgres-data:/var/lib/postgresql/data \
|
|
postgres:15
|
|
|
|
echo "⏳ 等待 PostgreSQL 启动..."
|
|
sleep 5
|
|
|
|
# 创建测试数据库
|
|
echo "📊 创建测试数据库..."
|
|
docker exec ljwx-postgres psql -U postgres -c "CREATE DATABASE ljwx_children_test;" 2>/dev/null || echo " 测试数据库已存在"
|
|
|
|
echo ""
|
|
echo "✅ PostgreSQL 安装成功!"
|
|
echo ""
|
|
echo "📋 连接信息:"
|
|
echo " 主机: localhost"
|
|
echo " 端口: 5432"
|
|
echo " 用户: postgres"
|
|
echo " 密码: postgres"
|
|
echo " 开发数据库: ljwx_children"
|
|
echo " 测试数据库: ljwx_children_test"
|
|
echo ""
|
|
echo "🔗 连接字符串:"
|
|
echo " 开发: postgresql+asyncpg://postgres:postgres@localhost:5432/ljwx_children"
|
|
echo " 测试: postgresql://postgres:postgres@localhost:5432/ljwx_children_test"
|
|
echo ""
|
|
echo "🛠️ 管理命令:"
|
|
echo " 启动: docker start ljwx-postgres"
|
|
echo " 停止: docker stop ljwx-postgres"
|
|
echo " 日志: docker logs -f ljwx-postgres"
|
|
echo " 进入: docker exec -it ljwx-postgres psql -U postgres -d ljwx_children"
|
|
echo " 删除: docker stop ljwx-postgres && docker rm ljwx-postgres"
|
|
;;
|
|
|
|
2)
|
|
echo ""
|
|
echo "💻 本地安装 PostgreSQL..."
|
|
echo ""
|
|
|
|
if [ "$MACHINE" = "Mac" ]; then
|
|
# macOS 使用 Homebrew
|
|
if ! command -v brew &> /dev/null; then
|
|
echo "❌ Homebrew 未安装,请先安装: /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
|
|
exit 1
|
|
fi
|
|
|
|
echo "📦 使用 Homebrew 安装 PostgreSQL 15..."
|
|
brew install postgresql@15
|
|
|
|
echo "🚀 启动 PostgreSQL 服务..."
|
|
brew services start postgresql@15
|
|
|
|
echo "⏳ 等待服务启动..."
|
|
sleep 3
|
|
|
|
# 创建数据库
|
|
echo "📊 创建数据库..."
|
|
createdb ljwx_children 2>/dev/null || echo " 开发数据库已存在"
|
|
createdb ljwx_children_test 2>/dev/null || echo " 测试数据库已存在"
|
|
|
|
elif [ "$MACHINE" = "Linux" ]; then
|
|
# Linux 使用 apt
|
|
echo "📦 使用 apt 安装 PostgreSQL..."
|
|
sudo apt-get update
|
|
sudo apt-get install -y postgresql postgresql-contrib
|
|
|
|
echo "🚀 启动 PostgreSQL 服务..."
|
|
sudo systemctl start postgresql
|
|
sudo systemctl enable postgresql
|
|
|
|
echo "📊 创建用户和数据库..."
|
|
sudo -u postgres psql -c "CREATE USER postgres WITH PASSWORD 'postgres';" 2>/dev/null || echo " 用户已存在"
|
|
sudo -u postgres psql -c "ALTER USER postgres WITH SUPERUSER;" 2>/dev/null || true
|
|
sudo -u postgres createdb ljwx_children 2>/dev/null || echo " 开发数据库已存在"
|
|
sudo -u postgres createdb ljwx_children_test 2>/dev/null || echo " 测试数据库已存在"
|
|
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE ljwx_children TO postgres;" 2>/dev/null || true
|
|
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE ljwx_children_test TO postgres;" 2>/dev/null || true
|
|
fi
|
|
|
|
echo ""
|
|
echo "✅ PostgreSQL 安装成功!"
|
|
echo ""
|
|
echo "📋 连接信息:"
|
|
echo " 主机: localhost"
|
|
echo " 端口: 5432"
|
|
echo " 用户: postgres"
|
|
echo " 密码: postgres"
|
|
echo " 开发数据库: ljwx_children"
|
|
echo " 测试数据库: ljwx_children_test"
|
|
;;
|
|
|
|
3)
|
|
echo ""
|
|
echo "🔌 使用现有 PostgreSQL..."
|
|
echo ""
|
|
read -p "请输入主机 (默认 localhost): " PG_HOST
|
|
PG_HOST=${PG_HOST:-localhost}
|
|
|
|
read -p "请输入端口 (默认 5432): " PG_PORT
|
|
PG_PORT=${PG_PORT:-5432}
|
|
|
|
read -p "请输入用户名 (默认 postgres): " PG_USER
|
|
PG_USER=${PG_USER:-postgres}
|
|
|
|
read -sp "请输入密码: " PG_PASS
|
|
echo ""
|
|
|
|
# 测试连接
|
|
echo "🔍 测试数据库连接..."
|
|
if PGPASSWORD=$PG_PASS psql -h $PG_HOST -p $PG_PORT -U $PG_USER -c "SELECT version();" &> /dev/null; then
|
|
echo "✅ 连接成功!"
|
|
|
|
# 创建数据库
|
|
echo "📊 创建数据库..."
|
|
PGPASSWORD=$PG_PASS psql -h $PG_HOST -p $PG_PORT -U $PG_USER -c "CREATE DATABASE ljwx_children;" 2>/dev/null || echo " 开发数据库已存在"
|
|
PGPASSWORD=$PG_PASS psql -h $PG_HOST -p $PG_PORT -U $PG_USER -c "CREATE DATABASE ljwx_children_test;" 2>/dev/null || echo " 测试数据库已存在"
|
|
|
|
echo ""
|
|
echo "🔗 连接字符串:"
|
|
echo " 开发: postgresql+asyncpg://$PG_USER:$PG_PASS@$PG_HOST:$PG_PORT/ljwx_children"
|
|
echo " 测试: postgresql://$PG_USER:$PG_PASS@$PG_HOST:$PG_PORT/ljwx_children_test"
|
|
else
|
|
echo "❌ 连接失败,请检查配置"
|
|
exit 1
|
|
fi
|
|
;;
|
|
|
|
*)
|
|
echo "❌ 无效选择"
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
echo ""
|
|
echo "🎉 PostgreSQL 设置完成!"
|
|
echo ""
|
|
echo "📝 下一步:"
|
|
echo " 1. 复制环境变量配置: cp .env.example .env"
|
|
echo " 2. 编辑 .env 文件,确认 DATABASE_URL 正确"
|
|
echo " 3. 运行数据库迁移: alembic upgrade head"
|
|
echo " 4. 启动应用: python -m uvicorn backend.main:app --reload"
|
|
echo ""
|
|
echo "🧪 运行测试:"
|
|
echo " source venv/bin/activate"
|
|
echo " python -m pytest tests/unit/test_api_risk_assessments.py -v"
|
|
echo ""
|