#!/bin/bash # ============================================ # 数据库完整重建脚本 # 删除所有表并重新初始化(包含所有迁移) # # 更新日志: # 2026-01-23: 添加 all_tables.sql 整合文件,--simple 模式 # 2026-01-23: 添加 --drop-only, --init-only, --list-migrations 选项 # 2026-01-22: 添加 extract_tables 迁移支持 # ============================================ set -e # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # 数据库配置(根据实际情况修改) DB_HOST=${DB_HOST:-localhost} DB_PORT=${DB_PORT:-5432} DB_NAME=${DB_NAME:-lingyue_zhibao} DB_USER=${DB_USER:-postgres} DB_PASSWORD=${DB_PASSWORD:-postgres} log_info() { echo -e "${GREEN}[INFO]${NC} $1"; } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } log_error() { echo -e "${RED}[ERROR]${NC} $1"; } log_title() { echo -e "\n${BLUE}========== $1 ==========${NC}\n"; } log_step() { echo -e "${CYAN}[STEP]${NC} $1"; } # 获取脚本所在目录 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" SQL_DIR="${SCRIPT_DIR}" MIGRATIONS_DIR="${PROJECT_ROOT}/database/migrations" # 删除所有表 drop_all_tables() { log_title "删除所有表" if [ "$FORCE" != "yes" ]; then log_warn "这将删除数据库 ${DB_NAME} 中的所有表和数据!" read -p "是否继续?(yes/no): " confirm if [ "$confirm" != "yes" ]; then log_info "操作已取消" exit 0 fi fi log_info "开始删除表..." PGPASSWORD=${DB_PASSWORD} psql -h ${DB_HOST} -p ${DB_PORT} -U ${DB_USER} -d ${DB_NAME} <&1 | grep -v "^NOTICE:" || true log_info "${desc} 完成" else log_warn "文件不存在: ${file}" fi } # 列出迁移脚本 list_migrations() { log_title "迁移脚本列表" if [ -d "$MIGRATIONS_DIR" ]; then local count=0 echo "" printf " %-5s %-60s %s\n" "序号" "文件名" "大小" echo " -------------------------------------------------------------------------" for migration in $(ls -1 "${MIGRATIONS_DIR}"/*.sql 2>/dev/null | sort); do if [ -f "$migration" ]; then count=$((count + 1)) local size=$(du -h "$migration" | cut -f1) printf " %-5s %-60s %s\n" "$count" "$(basename $migration)" "$size" fi done echo "" log_info "共 ${count} 个迁移脚本" else log_warn "迁移目录不存在: ${MIGRATIONS_DIR}" fi } # 简单模式初始化(使用 all_tables.sql 单文件) init_simple() { log_title "初始化数据库表(简单模式)" log_info "使用 all_tables.sql 单文件初始化所有表" execute_sql "${SQL_DIR}/all_tables.sql" "全部表结构 (21个表)" } # 初始化所有表(分文件模式) init_all_tables() { log_title "初始化数据库表" local step=1 # 1. 基础表(users, documents 等核心表) log_step "[$step/6] 基础表" execute_sql "${SQL_DIR}/init.sql" "基础表 (users, documents, elements, etc.)" step=$((step + 1)) # 2. 图谱表(graph_nodes, graph_relations - 先于 supplement_tables) log_step "[$step/6] 图谱表" execute_sql "${SQL_DIR}/graph_tables.sql" "图谱表 (graph_nodes, graph_relations)" step=$((step + 1)) # 3. 补充表(rules, data_sources, text_storage 等,不含 templates) log_step "[$step/6] 补充表" execute_sql "${SQL_DIR}/supplement_tables.sql" "补充表 (rules, data_sources, text_storage)" step=$((step + 1)) # 4. RAG 表(text_chunks, vector_embeddings - 可能需要 pgvector) log_step "[$step/6] RAG 表" execute_sql "${SQL_DIR}/rag_tables_compatible.sql" "RAG 表 (text_chunks, vector_embeddings)" step=$((step + 1)) # 5. 模板系统表(templates, source_files, variables, generations) log_step "[$step/6] 模板系统表" execute_sql "${SQL_DIR}/template_tables.sql" "模板系统表 (templates, source_files, variables, generations)" step=$((step + 1)) # 6. 执行迁移文件(按文件名排序) log_step "[$step/6] 迁移脚本" log_title "执行迁移脚本" if [ -d "$MIGRATIONS_DIR" ]; then local migration_count=0 local total_migrations=$(ls -1 "${MIGRATIONS_DIR}"/*.sql 2>/dev/null | wc -l) for migration in $(ls -1 "${MIGRATIONS_DIR}"/*.sql 2>/dev/null | sort); do if [ -f "$migration" ]; then migration_count=$((migration_count + 1)) execute_sql "$migration" "[$migration_count/$total_migrations] $(basename $migration)" fi done log_info "共执行 ${migration_count} 个迁移脚本" else log_warn "迁移目录不存在: ${MIGRATIONS_DIR}" fi } # 验证表创建 verify_tables() { log_title "验证表创建" PGPASSWORD=${DB_PASSWORD} psql -h ${DB_HOST} -p ${DB_PORT} -U ${DB_USER} -d ${DB_NAME} <